Skip to content

Commit ead6111

Browse files
feat: implement hvac system task
1 parent 05a6a21 commit ead6111

File tree

2 files changed

+283
-0
lines changed

2 files changed

+283
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright (c) 2023 Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
#include "HvacSystem.h"
10+
#include "../../../../utils/ArrayStream.h"
11+
12+
HvacSystem::HvacSystem(const int period, HvacSystemContext* const context): AbstractFsm(period), context(context) {
13+
this->changeState(new VON(context));
14+
}
15+
16+
HvacSystem::~HvacSystem() {
17+
delete this->context;
18+
}
19+
20+
/*
21+
##############################################################################
22+
------------------State of HvacSystem FSM ---------------
23+
##############################################################################
24+
*/
25+
26+
/*
27+
----- VON State -----
28+
*/
29+
30+
HvacSystem::VON::VON(HvacSystemContext* const context): context(context) {
31+
// act
32+
this->context->ventilationSystem->turn(PowerStatus::ON);
33+
this->context->heatingModule->turn(PowerStatus::OFF);
34+
this->context->coolingModule->turn(PowerStatus::OFF);
35+
// signal to gateway
36+
this->context->eventList->add(new ActuatorStateEvent(this->context->ventilationSystem,
37+
PowerStatus::ON,
38+
this->context->ventilationSystem->getSpeedPercentage()));
39+
this->context->eventList->add(new ActuatorStateEvent(this->context->heatingModule, PowerStatus::OFF));
40+
this->context->eventList->add(new ActuatorStateEvent(this->context->coolingModule, PowerStatus::OFF));
41+
}
42+
43+
void HvacSystem::VON::run(Fsm* const parentFsm) {
44+
if(*this->context->currentCommand != nullptr) {
45+
switch((*this->context->currentCommand)->getCommandType()) {
46+
case CommandType::VENTILATION: {
47+
VentilationCommand* command = static_cast<VentilationCommand*>(*this->context->currentCommand);
48+
if(command->getRoom().getId() == this->context->room.getId()) {
49+
Percentage intensityPercentage = command->getIntensityPercentage();
50+
if(intensityPercentage.get() == 0) {
51+
parentFsm->changeState(new VOFF(this->context));
52+
} else {
53+
this->context->ventilationSystem->setSpeedPercentage(intensityPercentage);
54+
this->context->eventList->add(new ActuatorStateEvent(this->context->ventilationSystem,
55+
PowerStatus::ON,
56+
this->context->ventilationSystem->getSpeedPercentage()));
57+
}
58+
}
59+
} break;
60+
61+
case CommandType::HEATING: {
62+
HeatingCommand* command = static_cast<HeatingCommand*>(*this->context->currentCommand);
63+
if(command->getRoom().getId() == this->context->room.getId()) {
64+
parentFsm->changeState(new HON(this->context));
65+
}
66+
} break;
67+
68+
case CommandType::COOLING: {
69+
CoolingCommand* command = static_cast<CoolingCommand*>(*this->context->currentCommand);
70+
if(command->getRoom().getId() == this->context->room.getId()) {
71+
parentFsm->changeState(new CON(this->context));
72+
}
73+
} break;
74+
75+
default:
76+
break;
77+
}
78+
}
79+
}
80+
81+
/*
82+
----- VOFF State -----
83+
*/
84+
85+
HvacSystem::VOFF::VOFF(HvacSystemContext* const context): context(context) {
86+
this->context->ventilationSystem->turn(PowerStatus::OFF);
87+
this->context->eventList->add(new ActuatorStateEvent(
88+
this->context->ventilationSystem, PowerStatus::OFF, this->context->ventilationSystem->getSpeedPercentage()));
89+
}
90+
91+
void HvacSystem::VOFF::run(Fsm* const parentFsm) {
92+
if(*this->context->currentCommand != nullptr) {
93+
switch((*this->context->currentCommand)->getCommandType()) {
94+
case CommandType::HEATING: {
95+
HeatingCommand* command = static_cast<HeatingCommand*>(*this->context->currentCommand);
96+
if(command->getRoom().getId() == this->context->room.getId()) {
97+
parentFsm->changeState(new HON(this->context));
98+
}
99+
} break;
100+
101+
case CommandType::COOLING: {
102+
CoolingCommand* command = static_cast<CoolingCommand*>(*this->context->currentCommand);
103+
if(command->getRoom().getId() == this->context->room.getId()) {
104+
parentFsm->changeState(new CON(this->context));
105+
}
106+
} break;
107+
108+
case CommandType::VENTILATION: {
109+
VentilationCommand* command = static_cast<VentilationCommand*>(*this->context->currentCommand);
110+
if(command->getRoom().getId() == this->context->room.getId()) {
111+
Percentage intensityPercentage = command->getIntensityPercentage();
112+
if(intensityPercentage.get() != 0) {
113+
this->context->ventilationSystem->setSpeedPercentage(intensityPercentage);
114+
parentFsm->changeState(new VON(this->context));
115+
}
116+
}
117+
} break;
118+
119+
default:
120+
break;
121+
}
122+
}
123+
}
124+
125+
/*
126+
----- HON State -----
127+
*/
128+
129+
HvacSystem::HON::HON(HvacSystemContext* const context): context(context) {
130+
if(this->context->ventilationSystem->getStatus() == PowerStatus::OFF) {
131+
this->context->ventilationSystem->turn(PowerStatus::ON);
132+
this->context->eventList->add(new ActuatorStateEvent(this->context->ventilationSystem,
133+
PowerStatus::ON,
134+
this->context->ventilationSystem->getSpeedPercentage()));
135+
}
136+
this->context->heatingModule->turn(PowerStatus::ON);
137+
this->context->eventList->add(new ActuatorStateEvent(this->context->heatingModule, PowerStatus::ON));
138+
}
139+
140+
void HvacSystem::HON::run(Fsm* const parentFsm) {
141+
if(*this->context->currentCommand != nullptr) {
142+
switch((*this->context->currentCommand)->getCommandType()) {
143+
case CommandType::HEATING: {
144+
HeatingCommand* heatingCommand = static_cast<HeatingCommand*>(*this->context->currentCommand);
145+
if(heatingCommand->getRoom().getId() == this->context->room.getId() && heatingCommand->getStatus() == PowerStatus::OFF) {
146+
parentFsm->changeState(new VON(this->context));
147+
}
148+
} break;
149+
150+
case CommandType::VENTILATION: {
151+
VentilationCommand* ventilationCommand = static_cast<VentilationCommand*>(*this->context->currentCommand);
152+
if(ventilationCommand->getRoom().getId() == this->context->room.getId()) {
153+
Percentage intensityPercentage = ventilationCommand->getIntensityPercentage();
154+
this->context->ventilationSystem->setSpeedPercentage(intensityPercentage);
155+
this->context->eventList->add(new ActuatorStateEvent(this->context->ventilationSystem,
156+
PowerStatus::ON,
157+
this->context->ventilationSystem->getSpeedPercentage()));
158+
}
159+
} break;
160+
}
161+
}
162+
}
163+
164+
/*
165+
----- CON State -----
166+
*/
167+
168+
HvacSystem::CON::CON(HvacSystemContext* const context): context(context) {
169+
if(this->context->ventilationSystem->getStatus() == PowerStatus::OFF) {
170+
this->context->ventilationSystem->turn(PowerStatus::ON);
171+
this->context->eventList->add(new ActuatorStateEvent(this->context->ventilationSystem,
172+
PowerStatus::ON,
173+
this->context->ventilationSystem->getSpeedPercentage()));
174+
}
175+
this->context->coolingModule->turn(PowerStatus::ON);
176+
this->context->eventList->add(new ActuatorStateEvent(this->context->coolingModule, PowerStatus::ON));
177+
}
178+
179+
void HvacSystem::CON::run(Fsm* const parentFsm) {
180+
if(*this->context->currentCommand != nullptr) {
181+
switch((*this->context->currentCommand)->getCommandType()) {
182+
case CommandType::COOLING: {
183+
CoolingCommand* coolingCommand = static_cast<CoolingCommand*>(*this->context->currentCommand);
184+
if(coolingCommand->getRoom().getId() == this->context->room.getId() && coolingCommand->getStatus() == PowerStatus::OFF) {
185+
parentFsm->changeState(new VON(this->context));
186+
}
187+
} break;
188+
189+
case CommandType::VENTILATION: {
190+
VentilationCommand* ventilationCommand = static_cast<VentilationCommand*>(*this->context->currentCommand);
191+
if(ventilationCommand->getRoom().getId() == this->context->room.getId()) {
192+
Percentage intensityPercentage = ventilationCommand->getIntensityPercentage();
193+
this->context->ventilationSystem->setSpeedPercentage(intensityPercentage);
194+
this->context->eventList->add(new ActuatorStateEvent(this->context->ventilationSystem,
195+
PowerStatus::ON,
196+
this->context->ventilationSystem->getSpeedPercentage()));
197+
}
198+
} break;
199+
}
200+
}
201+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2023 Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
#ifndef __HVAC_SYSTEM__
10+
#define __HVAC_SYSTEM__
11+
12+
#include "../AbstractFsm.h"
13+
#include "../../../model/event/Event.h"
14+
#include "../../../model/command/Command.h"
15+
#include "../../../../utils/List.h"
16+
#include "../../baseio/Cooling.h"
17+
#include "../../baseio/Heating.h"
18+
#include "../../baseio/Ventilation.h"
19+
20+
struct HvacSystemContext {
21+
List<Event*>* const eventList;
22+
Command** currentCommand;
23+
Room room;
24+
Ventilation* ventilationSystem;
25+
Heating* heatingModule;
26+
Cooling* coolingModule;
27+
};
28+
29+
/*
30+
Hvac System task.
31+
Its main objective is to control the hvac system of a room.
32+
*/
33+
class HvacSystem: public AbstractFsm {
34+
public:
35+
/*
36+
Constructor.
37+
38+
@param period the period of the fsm.
39+
@param context the local context of the fsm.
40+
*/
41+
HvacSystem(int period, HvacSystemContext* context);
42+
~HvacSystem();
43+
44+
private:
45+
HvacSystemContext* const context;
46+
47+
class VOFF: public State {
48+
public:
49+
VOFF(HvacSystemContext* context);
50+
void run(Fsm* parentFsm);
51+
private:
52+
HvacSystemContext* const context;
53+
};
54+
55+
class VON: public State {
56+
public:
57+
VON(HvacSystemContext* context);
58+
void run(Fsm* parentFsm);
59+
private:
60+
HvacSystemContext* const context;
61+
};
62+
63+
class HON: public State {
64+
public:
65+
HON(HvacSystemContext* context);
66+
void run(Fsm* parentFsm);
67+
private:
68+
HvacSystemContext* const context;
69+
};
70+
71+
class CON: public State {
72+
public:
73+
CON(HvacSystemContext* context);
74+
void run(Fsm* parentFsm);
75+
private:
76+
HvacSystemContext* const context;
77+
};
78+
79+
};
80+
81+
82+
#endif

0 commit comments

Comments
 (0)