-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add Lightweight Preset Scheduling #4772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bd0b620
Add scheduleing (kinda replaceing macros?)
9776C-bot d9b99af
Update wled00/schedule.cpp
Cloveian c4bca7a
Fix debug statement placement and logic.
9776C-bot d228002
add json locking
9776C-bot 37a536a
Implement atomic updates for schedule.json to avoid read/write races
9776C-bot fa12388
Add error checking for file write operations.
Cloveian b308018
Better error detection and reordered the log messages
9776C-bot be08a47
remove extra log message
9776C-bot 8d696f3
Add validation for JSON field values.
Cloveian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // schedule.cpp | ||
|
|
||
| //TODO: make schedule.json trigger loadshedule(); on upload istead of just once a min (line 50) | ||
|
|
||
| #include "schedule.h" | ||
| #include <WLED.h> | ||
| #include <time.h> | ||
|
|
||
| #define SCHEDULE_FILE "/schedule.json" | ||
|
|
||
| ScheduleEvent scheduleEvents[MAX_SCHEDULE_EVENTS]; | ||
| uint8_t numScheduleEvents = 0; | ||
|
|
||
| bool isTodayInRange(uint8_t sm, uint8_t sd, uint8_t em, uint8_t ed, uint8_t cm, uint8_t cd) | ||
| { | ||
| if (sm < em || (sm == em && sd <= ed)) | ||
| { | ||
| return (cm > sm || (cm == sm && cd >= sd)) && | ||
| (cm < em || (cm == em && cd <= ed)); | ||
| } | ||
| else | ||
| { | ||
| return (cm > sm || (cm == sm && cd >= sd)) || | ||
| (cm < em || (cm == em && cd <= ed)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Checks the schedule and applies any events that match the current time and date. | ||
|
|
||
| void checkSchedule() { | ||
| static int lastMinute = -1; | ||
|
|
||
| time_t now = localTime; | ||
| if (now < 100000) return; | ||
|
|
||
| struct tm* timeinfo = localtime(&now); | ||
| int thisMinute = timeinfo->tm_min + timeinfo->tm_hour * 60; | ||
|
|
||
| if (thisMinute == lastMinute) return; | ||
| lastMinute = thisMinute; | ||
|
|
||
|
|
||
| uint8_t cm = timeinfo->tm_mon + 1; // months since Jan (0-11) | ||
| uint8_t cd = timeinfo->tm_mday; | ||
| uint8_t wday = timeinfo->tm_wday; // days since Sunday (0-6) | ||
| uint8_t hr = timeinfo->tm_hour; | ||
| uint8_t min = timeinfo->tm_min; | ||
|
|
||
| loadSchedule(); | ||
Cloveian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| DEBUG_PRINTF_P(PSTR("[Schedule] Checking schedule at %02u:%02u\n"), hr, min); | ||
|
|
||
| for (uint8_t i = 0; i < numScheduleEvents; i++) | ||
| { | ||
| const ScheduleEvent &e = scheduleEvents[i]; | ||
| if (e.hour != hr || e.minute != min) | ||
| continue; | ||
|
|
||
| bool match = false; | ||
| if (e.repeatMask && ((e.repeatMask >> wday) & 0x01)) | ||
| match = true; | ||
| if (e.startMonth) | ||
| { | ||
| if (isTodayInRange(e.startMonth, e.startDay, e.endMonth, e.endDay, cm, cd)) | ||
| match = true; | ||
| } | ||
|
|
||
| if (match) | ||
| applyPreset(e.presetId); | ||
| DEBUG_PRINTF_P(PSTR("[Schedule] Applying preset %u at %02u:%02u\n"), e.presetId, hr, min); | ||
| DEBUG_PRINTF_P(PSTR("[Schedule] Checked event %u: match=%d\n"), i, match); | ||
| } | ||
Cloveian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Cloveian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void loadSchedule() | ||
| { | ||
| if (!WLED_FS.exists(SCHEDULE_FILE)) | ||
| return; | ||
| File file = WLED_FS.open(SCHEDULE_FILE, "r"); | ||
| if (!file) | ||
| return; | ||
|
|
||
| DynamicJsonDocument doc(4096); | ||
| if (deserializeJson(doc, file)) | ||
| { | ||
| file.close(); | ||
| return; | ||
| } | ||
| file.close(); | ||
|
|
||
| numScheduleEvents = 0; | ||
| for (JsonObject e : doc.as<JsonArray>()) | ||
| { | ||
| if (numScheduleEvents >= MAX_SCHEDULE_EVENTS) | ||
| break; | ||
| scheduleEvents[numScheduleEvents++] = { | ||
| (uint8_t)e["sm"], (uint8_t)e["sd"], // start month, day | ||
| (uint8_t)e["em"], (uint8_t)e["ed"], // end month, day | ||
| (uint8_t)e["r"], (uint8_t)e["h"], // repeat mask, hour | ||
| (uint8_t)e["m"], (uint8_t)e["p"]}; // minute, preset | ||
Cloveian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| DEBUG_PRINTF_P(PSTR("[Schedule] Loaded %u schedule entries from schedule.json\n"), numScheduleEvents); | ||
|
|
||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // schedule.h | ||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #define MAX_SCHEDULE_EVENTS 32 | ||
|
|
||
| struct ScheduleEvent { | ||
| uint8_t startMonth; | ||
| uint8_t startDay; | ||
| uint8_t endMonth; | ||
| uint8_t endDay; | ||
| uint8_t repeatMask; | ||
| uint8_t hour; | ||
| uint8_t minute; | ||
| uint8_t presetId; | ||
| }; | ||
|
|
||
| void loadSchedule(); | ||
| void checkSchedule(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.