This repository was archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightSwitch.cpp
364 lines (291 loc) · 9.71 KB
/
LightSwitch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "Arduino.h"
#include "HiveStorage.h"
#include "LightSwitch.h"
#include "SensorModule.h"
#include "aJson.h"
#include "AppContext.h"
#include "MemoryFree.h"
#include "HiveUtils.h"
const char LightSwitch::_moduleType[12] = "LightSwitch";
// TODO: add PULLUP or PULLDOWN resistor mode param in constructor
LightSwitch::LightSwitch(AppContext *context, const byte zone, byte moduleId, int storagePointer, boolean loadSettings, int8_t switchPin, int8_t lightPin) :
SensorModule(storagePointer, moduleId, zone),
_switchPin(switchPin),
_lightPin(lightPin),
_context(context) {
_stateChanged = true;
_resetSettings();
// DEBUG
debugPrint(F("LS: Init LightSwitch"));
pinMode(_switchPin, INPUT);
if (loadSettings) {
_loadSettings();
} else {
// If there's no load flag then we haven't saved anything yet, so init the storage
_saveSettings();
}
pinMode(_lightPin, OUTPUT);
_previousSwitchState = _readSwitchState();
if (!_moduleState) {
digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
} else {
switch (_lightMode) {
case 1:
// If there's a manual "on" override
digitalWrite(_lightPin, LIGHTSWITCH_RELAY_ON);
break;
case 2:
// If there's a manual "off" override
digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
break;
case 0:
// If there's auto switch mode
_previousSwitchState ? digitalWrite(_lightPin, LIGHTSWITCH_RELAY_ON) : digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
break;
}
_previousLightState = _readLightState();
}
// DEBUG
debugPrint(F("LS: Finished LightSwitch init"));
}
void LightSwitch::_resetSettings() {
_debounceTime = 20;
_debounceCounter = 0;
_previousSwitchState = 0;
_previousLightState = 0;
_moduleState = 1;
_lightMode = 0;
_switchCount = 0;
}
byte LightSwitch::getStorageSize() {
return sizeof(config_t);
}
void LightSwitch::_loadSettings() {
boolean isLoaded = false;
//DEBUG
debugPrint(F("LS: Loading module settings"));
config_t settings;
// Try to read storage and validate
if (readStorage(_storagePointer, settings) > 0) {
if (_validateSettings(&settings)) {
isLoaded = true;
}
}
if (isLoaded) {
_lightMode = settings.lightMode;
_moduleState = settings.moduleState;
} else {
// If unable to read then reset settings to default
// and try write them back to fix the storage
// (suppose the settings file was corrupt in some way)
_resetSettings();
_saveSettings();
}
_stateChanged = true;
}
void LightSwitch::_saveSettings() {
//DEBUG
debugPrint(F("LS: Saving module settings"));
config_t settings;
settings.lightMode = _lightMode;
settings.moduleState = _moduleState;
writeStorage(_storagePointer, settings);
}
byte LightSwitch::_readSwitchState () {
return digitalRead(_switchPin);
}
byte LightSwitch::_readLightState () {
// Relay on/off states may be inversed depending on physical connections
// So we take it into account and return simple values
// 1 if the relay is on, 0 - if it's off
if (digitalRead(_lightPin) == LIGHTSWITCH_RELAY_ON) {
return 1;
} else {
return 0;
}
}
void LightSwitch::_turnLightOff() {
if (_lightMode != 2) {
_lightMode = 2;
// Refresh the light state information
_previousLightState = _readLightState();
_stateChanged = true;
_saveSettings();
}
}
void LightSwitch::_turnLightOn() {
if (_lightMode != 1) {
_lightMode = 1;
// Refresh the light state information
_previousLightState = _readLightState();
_stateChanged = true;
_saveSettings();
}
}
void LightSwitch::_turnLightAuto() {
_lightMode = 0;
// Check if the light state differs from switch state
// to accomodate it in the loopDo method
_switchState = _readSwitchState();
_previousLightState = _readLightState();
if (_switchState != _previousLightState) {
_previousSwitchState = !_switchState;
}
_stateChanged = true;
_saveSettings();
}
void LightSwitch::_pushNotify() {
_context->pushNotify(moduleId);
}
void LightSwitch::getJSONSettings() {
// generate json settings only if something's changed
// TODO: mark properties with read-only and read-write types
if (_stateChanged) {
aJsonObject *moduleItem = aJson.getArrayItem(*(_context->moduleCollection), moduleId-1);
aJsonObject *moduleItemProperty = aJson.getObjectItem(moduleItem, "moduleType");
// If we have an empty JSON settings structure
// then fill it with values
if (strcmp(moduleItemProperty->valuestring, _moduleType) != 0) {
// TODO: Add prefixes to param names to mark readonly fields
aJson.addStringToObject(moduleItem, "moduleType", _moduleType);
aJson.addNumberToObject(moduleItem, "moduleState", _moduleState);
aJson.addNumberToObject(moduleItem, "zoneId", _moduleZone);
aJson.addNumberToObject(moduleItem, "switchState", _readSwitchState());
aJson.addNumberToObject(moduleItem, "lightState", _readLightState());
aJson.addNumberToObject(moduleItem, "lightMode", _lightMode);
} else {
// If we have an already initialized settings JSON structure
// then just replace the values
moduleItemProperty = aJson.getObjectItem(moduleItem, "moduleState");
moduleItemProperty->valuebool = _moduleState;
moduleItemProperty = aJson.getObjectItem(moduleItem, "switchState");
moduleItemProperty->valueint = _readSwitchState();
moduleItemProperty = aJson.getObjectItem(moduleItem, "lightState");
moduleItemProperty->valueint = _readLightState();
moduleItemProperty = aJson.getObjectItem(moduleItem, "lightMode");
moduleItemProperty->valueint = _lightMode;
}
_stateChanged = false;
}
}
// TODO: if error, return settings object with error item
boolean LightSwitch::_validateSettings(config_t *settings) {
if ((settings->lightMode < 0) || (settings->lightMode > 2)) {
return false;
}
if ((settings->moduleState < 0) || (settings->moduleState > 1)) {
return false;
}
return true;
}
boolean LightSwitch::setJSONSettings(aJsonObject *moduleItem) {
config_t settings;
aJsonObject *moduleItemProperty;
// Initialiaze to invalid values so the validation works
int8_t newModuleState = -1;
int8_t newLightMode = -1;
// Check for module type first
moduleItemProperty = aJson.getObjectItem(moduleItem, "moduleType");
if (strcmp(moduleItemProperty->valuestring, _moduleType) != 0) {
return false;
}
moduleItemProperty = aJson.getObjectItem(moduleItem, "moduleState");
newModuleState = moduleItemProperty->valuebool;
moduleItemProperty = aJson.getObjectItem(moduleItem, "lightMode");
newLightMode = moduleItemProperty->valueint;
settings.lightMode = newLightMode;
settings.moduleState = newModuleState;
if (!_validateSettings(&settings)) {
return false;
}
if (_moduleState != newModuleState) {
newModuleState ? turnModuleOn() : turnModuleOff();
}
if (_lightMode != newLightMode) {
switch (newLightMode) {
case 0:
_turnLightAuto();
break;
case 1:
_turnLightOn();
break;
case 2:
_turnLightOff();
break;
}
}
return true;
}
void LightSwitch::turnModuleOff() {
if (_moduleState) {
digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
_moduleState = false;
_stateChanged = true;
_saveSettings();
}
}
void LightSwitch::turnModuleOn() {
if (!_moduleState) {
_switchState = digitalRead(_switchPin);
// Check current operation mode (auto or manual override)
if (_lightMode == 0) {
_switchState ? digitalWrite(_lightPin, LIGHTSWITCH_RELAY_ON) : digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
} else {
_lightMode == 1 ? digitalWrite(_lightPin, LIGHTSWITCH_RELAY_ON) : digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
}
_moduleState = true;
_stateChanged = true;
_saveSettings();
}
}
void LightSwitch::loopDo() {
// If the module is on now
if (_moduleState) {
// If the module is in the override mode, user can bring it back
// to the auto mode by double-flipping the wall switch.
// So we first check if we have a double flip.
if (_switchCount >= 2) {
// Set module mode to auto
_lightMode = 0;
_turnLightAuto();
_switchCount = 0;
} else if (_lightMode == 1 && _previousLightState == 0) {
// If we have manual "on" override mode
digitalWrite(_lightPin, LIGHTSWITCH_RELAY_ON);
_previousLightState = 1;
} else if (_lightMode == 2 && _previousLightState == 1) {
// If we have manual "off" override mode
digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
_previousLightState = 0;
}
// Let's debounce the switch
_switchState = _readSwitchState();
if (_previousSwitchState != _switchState) {
_debounceCounter = millis();
_previousSwitchState = _switchState;
}
if ((_debounceCounter > 0) && (millis() - _debounceCounter) > _debounceTime) {
// we reach here if the switch is stable for the debounce time
_debounceCounter = 0;
if (_switchState != _previousLightState) {
if (_lightMode >= 1) {
// If there's a manual override of whatever kind
// count switch flips while in override mode
_switchCount++;
}
// If we are in auto switching mode
if (_lightMode == 0) {
// switch the light if previous light state differs and switch is debounced
_switchState ? digitalWrite(_lightPin, LIGHTSWITCH_RELAY_ON) : digitalWrite(_lightPin, LIGHTSWITCH_RELAY_OFF);
_previousLightState = _readLightState();
// Notify remote server
_pushNotify();
_stateChanged = true;
_switchCount = 0;
}
}
// end switch after debounce
}
// end check if debounced
}
}