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+ }
0 commit comments