-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Lightweight Scheduling System Proposal (Feedback Requested) #4775
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d1d4fe
add scheduling
PagedPenguin 958a34a
Fix the wled.h include statement
PagedPenguin e95c2b7
Add configurable JSON buffer size
PagedPenguin 01ac8b0
fix bracket formating
PagedPenguin c892931
Using single instance of "/schedule.json" string
PagedPenguin e130bb9
Refactor schedule event storage to use std::vector
PagedPenguin de9630e
Fix undefined variable and improve validation.
PagedPenguin 8e321de
Merge branch 'wled:main' into clean-scheduling
PagedPenguin 255ba9b
dynamic schedule ui adding
PagedPenguin 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,141 @@ | ||
// schedule.cpp | ||
// Handles reading, parsing, and checking the preset schedule from schedule.json | ||
|
||
#include "schedule.h" | ||
#include "wled.h" | ||
#include <time.h> | ||
#include <vector> | ||
|
||
#define SCHEDULE_FILE "/schedule.json" | ||
#define SCHEDULE_JSON_BUFFER_SIZE 4096 | ||
|
||
// Array to hold scheduled events, max size defined in schedule.h | ||
std::vector<ScheduleEvent> scheduleEvents; | ||
|
||
// Helper function to check if current date (cm, cd) is within the event's start and end date range | ||
bool isTodayInRange(uint8_t sm, uint8_t sd, uint8_t em, uint8_t ed, uint8_t cm, uint8_t cd) | ||
{ | ||
// Handles ranges that wrap over the year end, e.g., Dec to Jan | ||
if (sm < em || (sm == em && sd <= ed)) { | ||
// Normal range within a year | ||
return (cm > sm || (cm == sm && cd >= sd)) && | ||
(cm < em || (cm == em && cd <= ed)); | ||
} | ||
else { | ||
// Range wraps year-end (e.g., Nov 20 to Feb 10) | ||
return (cm > sm || (cm == sm && cd >= sd)) || | ||
(cm < em || (cm == em && cd <= ed)); | ||
} | ||
} | ||
|
||
// Checks current time against schedule entries and applies matching presets | ||
void checkSchedule() { | ||
static int lastMinute = -1; // To avoid multiple triggers within the same minute | ||
|
||
time_t now = localTime; | ||
if (now < 100000) return; // Invalid or uninitialized time guard | ||
|
||
struct tm* timeinfo = localtime(&now); | ||
|
||
int thisMinute = timeinfo->tm_min + timeinfo->tm_hour * 60; | ||
if (thisMinute == lastMinute) return; // Already checked this minute | ||
lastMinute = thisMinute; | ||
|
||
// Extract date/time components for easier use | ||
uint8_t cm = timeinfo->tm_mon + 1; // Month [1-12] | ||
uint8_t cd = timeinfo->tm_mday; // Day of month [1-31] | ||
uint8_t wday = timeinfo->tm_wday; // Weekday [0-6], Sunday=0 | ||
uint8_t hr = timeinfo->tm_hour; // Hour [0-23] | ||
uint8_t min = timeinfo->tm_min; // Minute [0-59] | ||
|
||
DEBUG_PRINTF_P(PSTR("[Schedule] Checking schedule at %02u:%02u\n"), hr, min); | ||
|
||
// Iterate through all scheduled events | ||
for (size_t i = 0; i < scheduleEvents.size(); i++) { | ||
const ScheduleEvent &e = scheduleEvents[i]; | ||
|
||
// Skip if hour or minute doesn't match current time | ||
if (e.hour != hr || e.minute != min) | ||
continue; | ||
|
||
bool match = false; | ||
|
||
// Check if repeat mask matches current weekday (bitmask with Sunday=LSB) | ||
if (e.repeatMask && ((e.repeatMask >> wday) & 0x01)) | ||
match = true; | ||
|
||
// Otherwise check if current date is within start and end date range | ||
if (e.startMonth) { | ||
if (isTodayInRange(e.startMonth, e.startDay, e.endMonth, e.endDay, cm, cd)) | ||
match = true; | ||
} | ||
|
||
// If match, apply preset and print debug | ||
if (match) { | ||
applyPreset(e.presetId); | ||
DEBUG_PRINTF_P(PSTR("[Schedule] Applying preset %u at %02u:%02u\n"), e.presetId, hr, min); | ||
} | ||
} | ||
} | ||
|
||
// Loads schedule events from the schedule JSON file | ||
// Returns true if successful, false on error or missing file | ||
bool loadSchedule() { | ||
if (!WLED_FS.exists(SCHEDULE_FILE)) return false; | ||
|
||
// Acquire JSON buffer lock to prevent concurrent access | ||
if (!requestJSONBufferLock(7)) return false; | ||
|
||
File file = WLED_FS.open(SCHEDULE_FILE, "r"); | ||
if (!file) { | ||
releaseJSONBufferLock(); | ||
return false; | ||
} | ||
|
||
DynamicJsonDocument doc(SCHEDULE_JSON_BUFFER_SIZE); | ||
DeserializationError error = deserializeJson(doc, file); | ||
file.close(); // Always close file before releasing lock | ||
|
||
if (error) { | ||
DEBUG_PRINTF_P(PSTR("[Schedule] JSON parse failed: %s\n"), error.c_str()); | ||
releaseJSONBufferLock(); | ||
return false; | ||
} | ||
|
||
scheduleEvents.clear(); | ||
for (JsonObject e : doc.as<JsonArray>()) { | ||
|
||
// Read and validate fields with type safety | ||
int sm = e["sm"].as<int>(); | ||
int sd = e["sd"].as<int>(); | ||
int em = e["em"].as<int>(); | ||
int ed = e["ed"].as<int>(); | ||
int r = e["r"].as<int>(); | ||
int h = e["h"].as<int>(); | ||
int m = e["m"].as<int>(); | ||
int p = e["p"].as<int>(); | ||
|
||
// Validate ranges to prevent bad data | ||
if (sm < 1 || sm > 12 || em < 1 || em > 12 || | ||
sd < 1 || sd > 31 || ed < 1 || ed > 31 || | ||
h < 0 || h > 23 || m < 0 || m > 59 || | ||
r < 0 || r > 127|| p < 1 || p > 250) { | ||
DEBUG_PRINTF_P(PSTR("[Schedule] Invalid values in event %u, skipping\n"), (uint16_t)scheduleEvents.size()); | ||
continue; | ||
} | ||
|
||
scheduleEvents.push_back({ | ||
(uint8_t)sm, (uint8_t)sd, | ||
(uint8_t)em, (uint8_t)ed, | ||
(uint8_t)r, (uint8_t)h, | ||
(uint8_t)m, (uint8_t)p | ||
}); | ||
} | ||
|
||
DEBUG_PRINTF_P(PSTR("[Schedule] Loaded %u schedule entries from schedule.json\n"), (uint16_t)scheduleEvents.size()); | ||
|
||
// Release JSON buffer lock after finishing | ||
releaseJSONBufferLock(); | ||
|
||
return true; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good adaptation to dynamic presets with minor issue.
The FC() and Wd() functions are correctly updated to handle dynamic presets with proper null checks. However, line 194 calls
updateAddButtonState()
which doesn't exist - should this beupdateButtonStates()
?📝 Committable suggestion
🤖 Prompt for AI Agents