From 205bb7d9c1904332c36adba1e5e8c79b8c0f69c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 19 Jan 2018 23:26:45 +0100 Subject: [PATCH] Simplify scheduler code --- code/espurna/config/general.h | 1 - code/espurna/scheduler.ino | 113 ++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 47 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 8ea8f37c..0d70b839 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -741,7 +741,6 @@ PROGMEM const char* const custom_reset_string[] = { #define NTP_SUPPORT 1 // Scheduler needs NTP #endif -#define SCHEDULER_UPDATE_SEC 5 // Scheduler perform switch at hh:mm:05 #define SCHEDULER_MAX_SCHEDULES 10 // Max schedules alowed // ----------------------------------------------------------------------------- diff --git a/code/espurna/scheduler.ino b/code/espurna/scheduler.ino index 08aab791..22c1fa73 100644 --- a/code/espurna/scheduler.ino +++ b/code/espurna/scheduler.ino @@ -11,6 +11,8 @@ Adapted by Xose PĂ©rez #include +// ----------------------------------------------------------------------------- + #if WEB_SUPPORT void _schWebSocketOnSend(JsonObject &root){ @@ -30,6 +32,8 @@ void _schWebSocketOnSend(JsonObject &root){ #endif // WEB_SUPPORT +// ----------------------------------------------------------------------------- + void _schConfigure() { bool delete_flag = false; @@ -71,7 +75,7 @@ void _schConfigure() { bool _schIsThisWeekday(String weekdays){ - // Monday = 1, Tuesday = 2 ... Sunday = 7 + // Convert from Sunday to Monday as day 1 int w = weekday(now()) - 1; if (w == 0) w = 7; @@ -92,67 +96,84 @@ int _schMinutesLeft(unsigned char schedule_hour, unsigned char schedule_minute){ return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute; } -// ----------------------------------------------------------------------------- +void _schCheck() { -void schSetup() { + // Check schedules + for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) { - _schConfigure(); + int sch_switch = getSetting("schSwitch", i, 0xFF).toInt(); + if (sch_switch == 0xFF) break; - // Update websocket clients - #if WEB_SUPPORT - wsOnSendRegister(_schWebSocketOnSend); - wsOnAfterParseRegister(_schConfigure); - #endif + String sch_weekdays = getSetting("schWDs", i, ""); + if (_schIsThisWeekday(sch_weekdays)) { - // Register loop - espurnaRegisterLoop(schLoop); + int sch_hour = getSetting("schHour", i, 0).toInt(); + int sch_minute = getSetting("schMinute", i, 0).toInt(); + int minutes_to_trigger = _schMinutesLeft(sch_hour, sch_minute); -} + if (minutes_to_trigger == 0) { + int sch_action = getSetting("schAction", i, 0).toInt(); + if (sch_action == 2) { + relayToggle(sch_switch); + } else { + relayStatus(sch_switch, sch_action); + } + DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch); + + // Show minutes to trigger every 15 minutes + // or every minute if less than 15 minutes to scheduled time. + // This only works for schedules on this same day. + // For instance, if your scheduler is set for 00:01 you will only + // get one notification before the trigger (at 00:00) + } else if (minutes_to_trigger > 0) { + + #if DEBUG_SUPPORT + if ((minutes_to_trigger % 15 == 0) || (minutes_to_trigger < 15)) { + DEBUG_MSG_P( + PSTR("[SCH] %d minutes to trigger schedule #%d\n"), + minutes_to_trigger, sch_switch + ); + } + #endif -void schLoop() { + } - static unsigned long last_update = 0; - static int update_time = 0; + } + + } + +} + +void _schLoop() { // Check time has been sync'ed if (!ntpSynced()) return; - // Check if we should compare scheduled and actual times - if ((millis() - last_update > update_time) || (last_update == 0)) { - last_update = millis(); + // Check schedules every minute at hh:mm:00 + static unsigned long last_minute = 60; + unsigned char current_minute = minute(); + if (current_minute != last_minute) { + last_minute = current_minute; + _schCheck(); + } - // Calculate next update time - unsigned char current_second = second(); - update_time = (SCHEDULER_UPDATE_SEC + 60 - current_second) * 1000; +} - for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) { +// ----------------------------------------------------------------------------- - int sch_switch = getSetting("schSwitch", i, 0xFF).toInt(); - if (sch_switch == 0xFF) break; +void schSetup() { - String sch_weekdays = getSetting("schWDs", i, ""); - if (_schIsThisWeekday(sch_weekdays)) { + _schConfigure(); + + // Update websocket clients + #if WEB_SUPPORT + wsOnSendRegister(_schWebSocketOnSend); + wsOnAfterParseRegister(_schConfigure); + #endif + + // Register loop + espurnaRegisterLoop(_schLoop); - int sch_hour = getSetting("schHour", i, 0).toInt(); - int sch_minute = getSetting("schMinute", i, 0).toInt(); - int minutes_to_trigger = _schMinutesLeft(sch_hour, sch_minute); - if (minutes_to_trigger == 0) { - int sch_action = getSetting("schAction", i, 0).toInt(); - if (sch_action == 2) { - relayToggle(sch_switch); - } else { - relayStatus(sch_switch, sch_action); - } - DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch); - } else if (minutes_to_trigger > 0) { - DEBUG_MSG_P( - PSTR("[SCH] %d minutes to trigger schedule #%d\n"), - minutes_to_trigger, sch_switch - ); - } - } - } - } } #endif // SCHEDULER_SUPPORT