-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedulerBase.cpp
More file actions
469 lines (400 loc) · 14.8 KB
/
SchedulerBase.cpp
File metadata and controls
469 lines (400 loc) · 14.8 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
Copyright (c) 2024-2025 David Carson (dacarson)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "SchedulerBase.h"
#include "esp_netif_sntp.h"
#include <WiFi.h>
#define NTP_SERVER "pool.ntp.org"
SchedulerBase::SchedulerBase()
: sntpSyncDone(false), currentState(Unknown),
startVacationTime(0), endVacationTime(0),
isInitialized(false) {
}
void SchedulerBase::initDefault() {
memset(weekSchedule, 0xFF, sizeof(weekSchedule));
for (int i = 0; i < 7; ++i) {
weekSchedule[i].slots[0].startHour = 7;
weekSchedule[i].slots[0].startMinute = 0;
weekSchedule[i].slots[0].endHour = 9;
weekSchedule[i].slots[0].endMinute = 0;
weekSchedule[i].slots[1].startHour = 18;
weekSchedule[i].slots[1].startMinute = 0;
weekSchedule[i].slots[1].endHour = 21;
weekSchedule[i].slots[1].endMinute = 0;
}
}
bool SchedulerBase::saveScheduleToStorage() {
// Don't write unchanged data
DaySchedule weekScheduleEEPROM[7];
size_t len = sizeof(weekScheduleEEPROM);
esp_err_t status = nvs_get_blob(nvsStorageHandle, "weekSchedule", &weekScheduleEEPROM, &len);
if (status) {
Serial.print(F("Not critical, failed to load existing schedule for save comparison: "));
Serial.println(esp_err_to_name(status));
}
if (!status && memcmp(weekSchedule, weekScheduleEEPROM, sizeof(weekSchedule)) == 0) {
Serial.println(F("Schedule unchanged, not saving to NVS"));
return true;
}
status = nvs_set_blob(nvsStorageHandle, "weekSchedule", &weekSchedule, sizeof(weekSchedule));
if (status) {
Serial.print(F("Failed to save schedule to NVS "));
Serial.println(esp_err_to_name(status));
return false;
}
Serial.println(F("Schedule updated to NVS."));
nvs_commit(nvsStorageHandle);
return true;
}
bool SchedulerBase::loadScheduleFromStorage() {
size_t len = sizeof(weekSchedule);
esp_err_t status = nvs_get_blob(nvsStorageHandle, "weekSchedule", &weekSchedule, &len);
if (status) {
Serial.print(F("Opening NVS "));
Serial.println(esp_err_to_name(status));
return false;
}
// Validate what we loaded.
for (int i = 0; i < 7; i++) {
for (int x = 0; x < 4; x++) {
if (weekSchedule[i].slots[x].startHour != 0xFF && (weekSchedule[i].slots[x].startHour < 0 || weekSchedule[i].slots[x].startHour > 23)) {
Serial.println(F("Schedule corrupt in NVS."));
return false;
}
if (weekSchedule[i].slots[x].startMinute != 0xFF && (weekSchedule[i].slots[x].startMinute < 0 || weekSchedule[i].slots[x].startMinute > 59)) {
Serial.println(F("Schedule corrupt in NVS."));
return false;
}
if (weekSchedule[i].slots[x].endHour != 0xFF && (weekSchedule[i].slots[x].endHour < 0 || weekSchedule[i].slots[x].endHour > 23)) {
Serial.println(F("Schedule corrupt in NVS."));
return false;
}
if (weekSchedule[i].slots[x].endMinute != 0xFF && (weekSchedule[i].slots[x].endMinute < 0 || weekSchedule[i].slots[x].endMinute > 59)) {
Serial.println(F("Schedule corrupt in NVS."));
return false;
}
}
}
return true;
}
void SchedulerBase::setVacationState(bool active) {
endVacationTime = 0;
if (active) {
startVacationTime = time(nullptr);
} else {
startVacationTime = 0;
}
// Recalculate next state change since vacation state changed
if (isInitialized) {
getNextState(&nextStateChangeTime);
}
}
void SchedulerBase::activateOverride(int durationMinutes) {
time_t now = time(nullptr);
overrideStartTime = now;
overrideEndTime = now + (durationMinutes * 60); // Convert minutes to seconds
overrideActive = true;
// Calculate next state change (which will be when override expires)
nextStateChangeTime = overrideEndTime;
Serial.printf("Override activated! Scheduler forced ON for %d minutes.\n", durationMinutes);
}
SchedulerBase::State SchedulerBase::getNextState(time_t *nextStateTime) const {
time_t nextTime;
// Handle vacation state
if (currentState == State::Vacation) {
// If no end time set, stay in vacation indefinitely
if (endVacationTime == 0) {
if (nextStateTime) {
*nextStateTime = 0; // No next state change
}
return State::Vacation;
}
if (nextStateTime) {
*nextStateTime = endVacationTime;
}
// Check if we'll be in an active slot when vacation ends
struct tm *end_tm = localtime(&endVacationTime);
int endHour = end_tm->tm_hour;
int endMinute = end_tm->tm_min;
int endDay = end_tm->tm_wday;
for (int slot = 0; slot < 4 && weekSchedule[endDay].slots[slot].startHour != 0xFF; slot++) {
if (isTimeWithinSlot(endHour, endMinute, weekSchedule[endDay].slots[slot])) {
return State::Active; // We'll transition from Vacation -> Active
}
}
return State::InActive; // We'll transition from Vacation -> InActive
}
// Find the next event
time_t now = time(nullptr);
struct tm *tm_struct = localtime(&now);
int currentHour = tm_struct->tm_hour;
int currentMinute = tm_struct->tm_min;
int currentDay = tm_struct->tm_wday;
int currentTimeInMinutes = currentHour * 60 + currentMinute;
for (int dayoffset = 0; dayoffset < 7; dayoffset++) {
int day = (currentDay + dayoffset) % 7;
for (int slot = 0; slot < 4 && weekSchedule[day].slots[slot].startHour != 0xFF; slot++) {
int startMin = dayoffset * 24 * 60 + weekSchedule[day].slots[slot].startHour * 60 + weekSchedule[day].slots[slot].startMinute;
if (startMin > currentTimeInMinutes) {
// Calculate start time of next item
tm nextState_tm;
nextState_tm.tm_year = tm_struct->tm_year;
nextState_tm.tm_mon = tm_struct->tm_mon;
nextState_tm.tm_mday = tm_struct->tm_mday + dayoffset;
nextState_tm.tm_hour = weekSchedule[day].slots[slot].startHour;
nextState_tm.tm_min = weekSchedule[day].slots[slot].startMinute;
nextState_tm.tm_sec = 0;
nextState_tm.tm_isdst = -1; // Use TZ to determine DST offsets.
nextTime = mktime(&nextState_tm);
if (startVacationTime && startVacationTime < nextTime) {
if (nextStateTime) {
*nextStateTime = startVacationTime;
}
return State::Vacation;
}
if (nextStateTime) {
*nextStateTime = nextTime;
}
return State::Active;
}
int endMin = dayoffset * 24 * 60 + weekSchedule[day].slots[slot].endHour * 60 + weekSchedule[day].slots[slot].endMinute;
if (endMin > currentTimeInMinutes) {
// Calculate start time of next item
tm nextState_tm;
nextState_tm.tm_year = tm_struct->tm_year;
nextState_tm.tm_mon = tm_struct->tm_mon;
nextState_tm.tm_mday = tm_struct->tm_mday + dayoffset;
nextState_tm.tm_hour = weekSchedule[day].slots[slot].endHour;
nextState_tm.tm_min = weekSchedule[day].slots[slot].endMinute;
nextState_tm.tm_sec = 0;
nextState_tm.tm_isdst = -1; // Use TZ to determine DST offsets.
nextTime = mktime(&nextState_tm);
if (startVacationTime && startVacationTime < nextTime) {
if (nextStateTime) {
*nextStateTime = startVacationTime;
}
return State::Vacation;
}
if (nextStateTime) {
*nextStateTime = nextTime;
}
return State::InActive;
}
}
}
// Found no timeslots or vacation slot, so we are in active indefinitely
if (nextStateTime) {
*nextStateTime = 0;
}
return State::InActive;
}
bool SchedulerBase::isTimeWithinSlot(int currentHour, int currentMinute, TimeSlot slot) const {
// Convert times to minutes since midnight for comparison
int currentTimeInMinutes = currentHour * 60 + currentMinute;
int slotStartInMinutes = slot.startHour * 60 + slot.startMinute;
int slotEndInMinutes = slot.endHour * 60 + slot.endMinute;
return currentTimeInMinutes >= slotStartInMinutes && currentTimeInMinutes <= slotEndInMinutes;
}
bool SchedulerBase::setTz(String timezone) {
// Reload the current timezone as it may have been updated elsewhere.
size_t len = 64;
char tzStr[64];
esp_err_t status = nvs_get_str(nvsStorageHandle, "TZ", tzStr, &len);
if (status) {
tz = String();
} else {
tz = tzStr;
}
if (tz == timezone) {
Serial.print(timezone);
Serial.println(" Timezones are the same, not updating.");
return true;
} else {
Serial.print("Updating timezone to ");
Serial.println(timezone);
tz = timezone;
setenv("TZ", timezone.c_str(), 1);
tzset();
status = nvs_set_str(nvsStorageHandle, "TZ", tz.c_str());
if (status) {
Serial.printf("Failed to write TZ from NVS: %s.\n", esp_err_to_name(status));
return false;
}
nvs_commit(nvsStorageHandle);
return true;
}
}
void SchedulerBase::eraseTz() {
esp_err_t status = nvs_erase_key(nvsStorageHandle, "TZ");
if (status == ESP_OK) {
Serial.println("Time zone erased from NVS.");
} else if (status == ESP_ERR_NVS_NOT_FOUND) {
Serial.println("No stored time zone found.");
} else {
Serial.printf("Failed to erase key: %s\n", esp_err_to_name(status));
}
nvs_commit(nvsStorageHandle);
unsetenv("TZ");
tzset();
tz = String();
}
bool SchedulerBase::isActive() {
return currentState == State::Active;
}
bool SchedulerBase::vacationActive() {
return currentState == State::Vacation;
}
int SchedulerBase::begin() {
esp_err_t status;
status = nvs_open("SCHEDULER", NVS_READWRITE, &nvsStorageHandle);
if (status) {
Serial.printf("Failed to open NVS Storage: %s\n", esp_err_to_name(status));
return false;
}
// If we failed to load the schedule from EEPROM, then
// init to a default schedule.
if (!loadScheduleFromStorage()) {
Serial.println("No saved Schedule, loading default.");
initDefault();
}
uint32_t storedTime = 0;
status = nvs_get_u32(nvsStorageHandle, "startVacation", &storedTime);
startVacationTime = (time_t)storedTime;
if (status) {
Serial.println("Failed to load vacation start time, defaulting to unset.");
startVacationTime = 0;
}
status = nvs_get_u32(nvsStorageHandle, "endVacation", &storedTime);
startVacationTime = (time_t)storedTime;
if (status) {
Serial.println("Failed to load vacation end time, defaulting to unset.");
endVacationTime = 0;
}
size_t len = 64;
char tzStr[64];
status = nvs_get_str(nvsStorageHandle, "TZ", tzStr, &len);
if (status) {
Serial.printf("Failed to load TZ from NVS: %s.\n", esp_err_to_name(status));
Serial.println("Schedules won't run until TZ set.");
} else {
tz = tzStr;
Serial.print("Restoring saved TZ: ");
Serial.println(tz);
setenv("TZ", tz.c_str(), 1);
tzset();
}
// Enable SNTP
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG(NTP_SERVER);
esp_netif_sntp_init(&config); // DEFAULT_CONFIG is to start the server on init()
sntpSyncDone = false;
// Don't calculate next state change time here - it will be done in loop()
// once SNTP sync is complete and TZ is set
return true;
}
void SchedulerBase::initializeCurrentState() {
time_t now = time(nullptr);
State newState = State::InActive; // Default to inactive
// First check if we're in override mode
if (overrideActive && now < overrideEndTime) {
newState = State::Override;
}
// Then check if we're in vacation mode
else if (startVacationTime && now >= startVacationTime && (!endVacationTime || now < endVacationTime)) {
newState = State::Vacation;
}
// Finally check if we're in an active time slot
else {
struct tm *tm_struct = localtime(&now);
int currentHour = tm_struct->tm_hour;
int currentMinute = tm_struct->tm_min;
int currentDay = tm_struct->tm_wday;
for (int i = 0; i < 4 && weekSchedule[currentDay].slots[i].startHour != 0xFF; i++) {
if (isTimeWithinSlot(currentHour, currentMinute, weekSchedule[currentDay].slots[i])) {
newState = State::Active;
break;
}
}
}
// Set the new state and notify
if (newState != currentState) {
stateChange(newState);
currentState = newState;
}
// Calculate the next state change time
getNextState(&nextStateChangeTime);
}
void SchedulerBase::loop() {
if (!sntpSyncDone && WiFi.status() != WL_CONNECTED) {
return;
}
esp_err_t value;
if (!sntpSyncDone && (value = esp_netif_sntp_sync_wait(2000) == ESP_ERR_TIMEOUT)) {
if (tz.length() > 0) {
Serial.print("+");
}
return;
}
if (!sntpSyncDone) {
Serial.printf("Sync %s\n", esp_err_to_name(value));
Serial.println("SNTP Sync completed");
sntpSyncDone = true;
return;
}
// No timezone set, so we can't schedule.
if (getenv("TZ") == 0) {
isInitialized = false;
return;
}
// If we just became initialized, determine our current state
if (!isInitialized) {
isInitialized = true;
initializeCurrentState();
return;
}
time_t now = time(nullptr);
State newState = currentState; // Default to current state
// Handle override expiration
if (overrideActive && now >= overrideEndTime) {
overrideActive = false;
Serial.println("Override expired. Reverting to normal scheduling.");
// Recalculate next state change since override expired
getNextState(&nextStateChangeTime);
}
// If override is active, force the state and return
if (overrideActive) {
newState = State::Override;
if (newState != currentState) {
stateChange(newState);
currentState = newState;
}
return;
}
// Check if it's time for a state change
if (nextStateChangeTime > 0 && now >= nextStateChangeTime) {
// Get the next state and its time
newState = getNextState(&nextStateChangeTime);
if (newState != currentState) {
stateChange(newState);
currentState = newState;
}
}
// If we don't have a next state change time, calculate it
if (nextStateChangeTime == 0) {
getNextState(&nextStateChangeTime);
}
}