|
| 1 | +// schedule.cpp |
| 2 | + |
| 3 | +//TODO: make schedule.json trigger loadshedule(); on upload istead of just once a min (line 50) |
| 4 | + |
| 5 | +#include "schedule.h" |
| 6 | +#include <WLED.h> |
| 7 | +#include <time.h> |
| 8 | + |
| 9 | +#define SCHEDULE_FILE "/schedule.json" |
| 10 | + |
| 11 | +ScheduleEvent scheduleEvents[MAX_SCHEDULE_EVENTS]; |
| 12 | +uint8_t numScheduleEvents = 0; |
| 13 | + |
| 14 | +bool isTodayInRange(uint8_t sm, uint8_t sd, uint8_t em, uint8_t ed, uint8_t cm, uint8_t cd) |
| 15 | +{ |
| 16 | + if (sm < em || (sm == em && sd <= ed)) |
| 17 | + { |
| 18 | + return (cm > sm || (cm == sm && cd >= sd)) && |
| 19 | + (cm < em || (cm == em && cd <= ed)); |
| 20 | + } |
| 21 | + else |
| 22 | + { |
| 23 | + return (cm > sm || (cm == sm && cd >= sd)) || |
| 24 | + (cm < em || (cm == em && cd <= ed)); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +// Checks the schedule and applies any events that match the current time and date. |
| 30 | + |
| 31 | +void checkSchedule() { |
| 32 | + static int lastMinute = -1; |
| 33 | + |
| 34 | + time_t now = localTime; |
| 35 | + if (now < 100000) return; |
| 36 | + |
| 37 | + struct tm* timeinfo = localtime(&now); |
| 38 | + int thisMinute = timeinfo->tm_min + timeinfo->tm_hour * 60; |
| 39 | + |
| 40 | + if (thisMinute == lastMinute) return; |
| 41 | + lastMinute = thisMinute; |
| 42 | + |
| 43 | + |
| 44 | + uint8_t cm = timeinfo->tm_mon + 1; // months since Jan (0-11) |
| 45 | + uint8_t cd = timeinfo->tm_mday; |
| 46 | + uint8_t wday = timeinfo->tm_wday; // days since Sunday (0-6) |
| 47 | + uint8_t hr = timeinfo->tm_hour; |
| 48 | + uint8_t min = timeinfo->tm_min; |
| 49 | + |
| 50 | + loadSchedule(); |
| 51 | + DEBUG_PRINTF_P(PSTR("[Schedule] Checking schedule at %02u:%02u\n"), hr, min); |
| 52 | + |
| 53 | + for (uint8_t i = 0; i < numScheduleEvents; i++) |
| 54 | + { |
| 55 | + const ScheduleEvent &e = scheduleEvents[i]; |
| 56 | + if (e.hour != hr || e.minute != min) |
| 57 | + continue; |
| 58 | + |
| 59 | + bool match = false; |
| 60 | + if (e.repeatMask && ((e.repeatMask >> wday) & 0x01)) |
| 61 | + match = true; |
| 62 | + if (e.startMonth) |
| 63 | + { |
| 64 | + if (isTodayInRange(e.startMonth, e.startDay, e.endMonth, e.endDay, cm, cd)) |
| 65 | + match = true; |
| 66 | + } |
| 67 | + |
| 68 | + if (match) |
| 69 | + applyPreset(e.presetId); |
| 70 | + DEBUG_PRINTF_P(PSTR("[Schedule] Applying preset %u at %02u:%02u\n"), e.presetId, hr, min); |
| 71 | + DEBUG_PRINTF_P(PSTR("[Schedule] Checked event %u: match=%d\n"), i, match); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void loadSchedule() |
| 76 | +{ |
| 77 | + if (!WLED_FS.exists(SCHEDULE_FILE)) |
| 78 | + return; |
| 79 | + File file = WLED_FS.open(SCHEDULE_FILE, "r"); |
| 80 | + if (!file) |
| 81 | + return; |
| 82 | + |
| 83 | + DynamicJsonDocument doc(4096); |
| 84 | + if (deserializeJson(doc, file)) |
| 85 | + { |
| 86 | + file.close(); |
| 87 | + return; |
| 88 | + } |
| 89 | + file.close(); |
| 90 | + |
| 91 | + numScheduleEvents = 0; |
| 92 | + for (JsonObject e : doc.as<JsonArray>()) |
| 93 | + { |
| 94 | + if (numScheduleEvents >= MAX_SCHEDULE_EVENTS) |
| 95 | + break; |
| 96 | + scheduleEvents[numScheduleEvents++] = { |
| 97 | + (uint8_t)e["sm"], (uint8_t)e["sd"], // start month, day |
| 98 | + (uint8_t)e["em"], (uint8_t)e["ed"], // end month, day |
| 99 | + (uint8_t)e["r"], (uint8_t)e["h"], // repeat mask, hour |
| 100 | + (uint8_t)e["m"], (uint8_t)e["p"]}; // minute, preset |
| 101 | + } |
| 102 | + DEBUG_PRINTF_P(PSTR("[Schedule] Loaded %u schedule entries from schedule.json\n"), numScheduleEvents); |
| 103 | + |
| 104 | +} |
| 105 | + |
0 commit comments