-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEventManager.ino
439 lines (429 loc) · 13.8 KB
/
EventManager.ino
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
void listenerSystem(int event, int parm) // System event listener
{
switch (parm)
{
case EM_OK: // Normal mode
break;
// 1 - 9 Error events
case EM_WLANER: // WLAN error -> handling
// Error Reihenfolge
// 1. WLAN connected?
// 2. MQTT connected
// Wenn WiFi.status() != WL_CONNECTED (wlan_state false nach maxRetries und Delay) ist, ist ein check mqtt überflüssig
oledDisplay.wlanOK = false;
WiFi.reconnect();
if (WiFi.status() == WL_CONNECTED)
{
wlan_state = true;
oledDisplay.wlanOK = true;
break;
}
DEBUG_MSG("%s", "EM WLAN: WLAN Fehler ... versuche neu zu verbinden\n");
break;
case EM_MQTTER: // MQTT Error -> handling
oledDisplay.mqttOK = false;
if (pubsubClient.connect(mqtt_clientid))
{
DEBUG_MSG("%s", "MQTT auto reconnect successful. Subscribing..\n");
cbpiEventSystem(EM_MQTTSUB); // MQTT subscribe
cbpiEventSystem(EM_MQTTRES); // MQTT restore
break;
}
if (millis() - mqttconnectlasttry >= wait_on_error_mqtt)
{
if (StopOnMQTTError && mqtt_state)
{
if (startBuzzer)
sendAlarm(ALARM_ERROR);
// if (useDisplay)
// showDispErr("MQTT ERROR")
DEBUG_MSG("EM MQTTER: MQTT Broker %s nicht erreichbar! StopOnMQTTError: %d mqtt_state: %d\n", mqtthost, StopOnMQTTError, mqtt_state);
cbpiEventActors(EM_ACTER);
cbpiEventInduction(EM_INDER);
mqtt_state = false; // MQTT in error state
}
}
break;
// 10-19 System triggered events
case EM_MQTTRES: // restore saved values after reconnect MQTT (10)
if (pubsubClient.connected())
{
wlan_state = true;
mqtt_state = true;
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].switchable && !actors[i].actor_state)
{
DEBUG_MSG("EM MQTTRES: %s isOnBeforeError: %d Powerlevel: %d\n", actors[i].name_actor.c_str(), actors[i].isOnBeforeError, actors[i].power_actor);
actors[i].isOn = actors[i].isOnBeforeError;
actors[i].actor_state = true; // Sensor ok
actors[i].Update();
}
yield();
}
if (!inductionCooker.induction_state)
{
DEBUG_MSG("EM MQTTRES: Induction power: %d powerLevelOnError: %d powerLevelBeforeError: %d\n", inductionCooker.power, inductionCooker.powerLevelOnError, inductionCooker.powerLevelBeforeError);
inductionCooker.newPower = inductionCooker.powerLevelBeforeError;
inductionCooker.isInduon = true;
inductionCooker.induction_state = true; // Induction ok
inductionCooker.Update();
DEBUG_MSG("EM MQTTRES: Induction restore old value: %d\n", inductionCooker.newPower);
}
}
break;
case EM_REBOOT: // Reboot ESP (11) - manual task
// Stop actors
cbpiEventActors(EM_ACTOFF);
// Stop induction
if (inductionCooker.isInduon)
cbpiEventInduction(EM_INDOFF);
server.send(200, "text/plain", "rebooting...");
LittleFS.end(); // unmount LittleFS
ESP.restart();
break;
// System run & set events
case EM_WLAN: // check WLAN (20) and reconnect on error
if (WiFi.status() == WL_CONNECTED)
{
oledDisplay.wlanOK = true;
if (TickerWLAN.state() == RUNNING)
TickerWLAN.stop();
}
else
{
if (TickerWLAN.state() != RUNNING)
{
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
TickerWLAN.resume();
wlanconnectlasttry = millis();
}
TickerWLAN.update();
}
break;
case EM_MQTT: // check MQTT (22)
if (!WiFi.status() == WL_CONNECTED)
break;
if (pubsubClient.connected())
{
oledDisplay.mqttOK = true;
mqtt_state = true;
pubsubClient.loop();
if (TickerMQTT.state() == RUNNING)
TickerMQTT.stop();
}
else //if (!pubsubClient.connected())
{
if (TickerMQTT.state() != RUNNING)
{
DEBUG_MSG("%s\n", "MQTT Error: Starte TickerMQTT");
TickerMQTT.resume();
mqttconnectlasttry = millis();
}
TickerMQTT.update();
}
break;
case EM_MQTTCON: // MQTT connect (27)
if (WiFi.status() == WL_CONNECTED) // kein wlan = kein mqtt
{
DEBUG_MSG("%s\n", "Verbinde MQTT ...");
pubsubClient.setServer(mqtthost, 1883);
pubsubClient.setCallback(mqttcallback);
pubsubClient.connect(mqtt_clientid);
}
break;
case EM_MQTTSUB: // MQTT subscribe (28)
if (pubsubClient.connected())
{
DEBUG_MSG("%s\n", "MQTT verbunden. Subscribing...");
for (int i = 0; i < numberOfActors; i++)
{
actors[i].mqtt_subscribe();
yield();
}
if (inductionCooker.isEnabled)
inductionCooker.mqtt_subscribe();
if (cbpi == true) // CBPi4
{
//
}
else if (startDB)
{
for (int i = 0; i < numberOfDBMax; i++)
{
dbInflux[i].mqtt_subscribe();
yield();
}
}
oledDisplay.mqttOK = true; // Display MQTT
mqtt_state = true; // MQTT state ok
//if (TickerMQTT.state() == RUNNING)
TickerMQTT.stop();
}
break;
case EM_MDNS: // check MDSN (24)
mdns.update();
break;
case EM_SETNTP: // NTP Update (25)
timeClient.begin();
timeClient.forceUpdate();
checkSummerTime();
break;
case EM_NTP: // NTP Update (25) -> In Ticker Objekt ausgelagert!
timeClient.update();
break;
case EM_MDNSET: // MDNS setup (26)
if (startMDNS && nameMDNS[0] != '\0' && WiFi.status() == WL_CONNECTED)
{
if (mdns.begin(nameMDNS))
Serial.printf("*** SYSINFO: mDNS gestartet als %s verbunden an %s Time: %s RSSI=%d\n", nameMDNS, WiFi.localIP().toString().c_str(), timeClient.getFormattedTime().c_str(), WiFi.RSSI());
else
Serial.printf("*** SYSINFO: Fehler Start mDNS! IP Adresse: %s Time: %s RSSI: %d\n", WiFi.localIP().toString().c_str(), timeClient.getFormattedTime().c_str(), WiFi.RSSI());
}
break;
case EM_DISPUP: // Display screen output update (30)
if (oledDisplay.dispEnabled)
oledDisplay.dispUpdate();
break;
case EM_DB: // InfluxDB
sendData();
break;
case EM_LOG:
if (LittleFS.exists("/log1.txt")) // WebUpdate Zertifikate
{
fsUploadFile = LittleFS.open("/log1.txt", "r");
String line;
while (fsUploadFile.available())
{
line = char(fsUploadFile.read());
}
fsUploadFile.close();
Serial.printf("*** SYSINFO: Update Index Anzahl Versuche %s\n", line.c_str());
LittleFS.remove("/log1.txt");
}
if (LittleFS.exists("/log2.txt")) // WebUpdate Index
{
fsUploadFile = LittleFS.open("/log2.txt", "r");
String line;
while (fsUploadFile.available())
{
line = char(fsUploadFile.read());
}
fsUploadFile.close();
Serial.printf("*** SYSINFO: Update Zertifikate Anzahl Versuche %s\n", line.c_str());
LittleFS.remove("/log2.txt");
}
if (LittleFS.exists("/log3.txt")) // WebUpdate Firmware
{
fsUploadFile = LittleFS.open("/log3.txt", "r");
String line;
while (fsUploadFile.available())
{
line = char(fsUploadFile.read());
}
fsUploadFile.close();
Serial.printf("*** SYSINFO: Update Firmware Anzahl Versuche %s\n", line.c_str());
LittleFS.remove("/log3.txt");
alertState = true;
}
break;
default:
break;
}
}
void listenerSensors(int event, int parm) // Sensor event listener
{
// 1:= Sensor on Err
switch (parm)
{
case EM_OK:
// all sensors ok
lastSenInd = 0; // Delete induction timestamp after event
lastSenAct = 0; // Delete actor timestamp after event
if (WiFi.status() == WL_CONNECTED && pubsubClient.connected() && wlan_state && mqtt_state)
{
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].switchable && !actors[i].actor_state) // Sensor in normal mode: check actor in error state
{
DEBUG_MSG("EM SenOK: %s isOnBeforeError: %d power level: %d\n", actors[i].name_actor.c_str(), actors[i].isOnBeforeError, actors[i].power_actor);
actors[i].isOn = actors[i].isOnBeforeError;
actors[i].actor_state = true;
actors[i].Update();
lastSenAct = 0; // Delete actor timestamp after event
}
yield();
}
if (!inductionCooker.induction_state)
{
DEBUG_MSG("EM SenOK: Induction power: %d powerLevelOnError: %d powerLevelBeforeError: %d\n", inductionCooker.power, inductionCooker.powerLevelOnError, inductionCooker.powerLevelBeforeError);
if (!inductionCooker.induction_state)
{
inductionCooker.newPower = inductionCooker.powerLevelBeforeError;
inductionCooker.isInduon = true;
inductionCooker.induction_state = true;
inductionCooker.Update();
DEBUG_MSG("EM SenOK: Induction restore old value: %d\n", inductionCooker.newPower);
lastSenInd = 0; // Delete induction timestamp after event
}
}
}
break;
case EM_CRCER:
// Sensor CRC ceck failed
case EM_DEVER:
// -127°C device error
case EM_UNPL:
// sensor unpluged
case EM_SENER:
// all other errors
if (WiFi.status() == WL_CONNECTED && pubsubClient.connected() && wlan_state && mqtt_state)
{
for (int i = 0; i < numberOfSensors; i++)
{
if (!sensors[i].getState())
{
switch (parm)
{
case EM_CRCER:
// Sensor CRC ceck failed
DEBUG_MSG("EM CRCER: Sensor %s crc check failed\n", sensors[i].getName().c_str());
break;
case EM_DEVER:
// -127°C device error
DEBUG_MSG("EM DEVER: Sensor %s device error\n", sensors[i].getName().c_str());
break;
case EM_UNPL:
// sensor unpluged
DEBUG_MSG("EM UNPL: Sensor %s unplugged\n", sensors[i].getName().c_str());
break;
default:
break;
}
}
if (sensors[i].getSw() && !sensors[i].getState())
{
if (lastSenAct == 0)
{
lastSenAct = millis(); // Timestamp on error
DEBUG_MSG("EM SENER: Erstelle Zeitstempel für Aktoren wegen Sensor Fehler: %l Wait on error actors: %d\n", lastSenAct, wait_on_Sensor_error_actor / 1000);
}
if (lastSenInd == 0)
{
lastSenInd = millis(); // Timestamp on error
DEBUG_MSG("EM SENER: Erstelle Zeitstempel für Induktion wegen Sensor Fehler: %l Wait on error induction: %d\n", lastSenInd, wait_on_Sensor_error_induction / 1000);
}
if (millis() - lastSenAct >= wait_on_Sensor_error_actor) // Wait bevor Event handling
cbpiEventActors(EM_ACTER);
if (millis() - lastSenInd >= wait_on_Sensor_error_induction) // Wait bevor Event handling
{
if (inductionCooker.isInduon && inductionCooker.powerLevelOnError < 100 && inductionCooker.induction_state)
cbpiEventInduction(EM_INDER);
}
} // Switchable
yield();
} // Iterate sensors
} // wlan und mqtt state
break;
default:
break;
}
handleSensors();
}
void listenerActors(int event, int parm) // Actor event listener
{
switch (parm)
{
case EM_OK:
break;
case 1:
break;
case 2:
break;
case EM_ACTER:
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].switchable && actors[i].actor_state && actors[i].isOn)
{
actors[i].isOnBeforeError = actors[i].isOn;
actors[i].isOn = false;
actors[i].actor_state = false;
actors[i].Update();
DEBUG_MSG("EM ACTER: Aktor: %s : %d isOnBeforeError: %d\n", actors[i].name_actor.c_str(), actors[i].actor_state, actors[i].isOnBeforeError);
}
yield();
}
break;
case EM_ACTOFF:
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].isOn)
{
actors[i].isOn = false;
actors[i].Update();
DEBUG_MSG("EM ACTER: Aktor: %s ausgeschaltet\n", actors[i].name_actor.c_str());
}
yield();
}
break;
default:
break;
}
handleActors();
}
void listenerInduction(int event, int parm) // Induction event listener
{
switch (parm)
{
case EM_OK: // Induction off
break;
case 1: // Induction on
break;
case 2:
//DBG_PRINTLN("EM IND2: received induction event"); // bislang keine Verwendung
break;
case EM_INDER:
if (inductionCooker.isInduon && inductionCooker.powerLevelOnError < 100 && inductionCooker.induction_state) // powerlevelonerror == 100 -> kein event handling
{
inductionCooker.powerLevelBeforeError = inductionCooker.power;
DEBUG_MSG("EM INDER: Induktion Leistung: %d Setze Leistung Induktion auf: %d\n", inductionCooker.power, inductionCooker.powerLevelOnError);
if (inductionCooker.powerLevelOnError == 0)
inductionCooker.isInduon = false;
else
inductionCooker.newPower = inductionCooker.powerLevelOnError;
inductionCooker.newPower = inductionCooker.powerLevelOnError;
inductionCooker.induction_state = false;
inductionCooker.Update();
}
break;
case EM_INDOFF:
if (inductionCooker.isInduon)
{
DEBUG_MSG("%s\n", "EM INDOFF: Induktion ausgeschaltet");
inductionCooker.newPower = 0;
inductionCooker.isInduon = false;
inductionCooker.Update();
}
break;
default:
break;
}
handleInduction();
}
void cbpiEventSystem(int parm) // System events
{
gEM.queueEvent(EventManager::kEventUser0, parm);
}
void cbpiEventSensors(int parm) // Sensor events
{
gEM.queueEvent(EventManager::kEventUser1, parm);
}
void cbpiEventActors(int parm) // Actor events
{
gEM.queueEvent(EventManager::kEventUser2, parm);
}
void cbpiEventInduction(int parm) // Induction events
{
gEM.queueEvent(EventManager::kEventUser3, parm);
}