diff --git a/code/espurna/scheduler.cpp b/code/espurna/scheduler.cpp index fa3e94ca..57c8efc8 100644 --- a/code/espurna/scheduler.cpp +++ b/code/espurna/scheduler.cpp @@ -16,11 +16,20 @@ Adapted by Xose PĂ©rez #include "ntp.h" #include "relay.h" #include "ws.h" +#include "curtain_kingart.h" constexpr const int SchedulerDummySwitchId = 0xff; int _sch_restore = 0; +unsigned char schedulableCount() { + return relayCount() +#ifdef CURTAIN_SUPPORT + + curtainCount() +#endif + ; +} + // ----------------------------------------------------------------------------- #if WEB_SUPPORT @@ -30,13 +39,13 @@ bool _schWebSocketOnKeyCheck(const char * key, JsonVariant& value) { } void _schWebSocketOnVisible(JsonObject& root) { - if (!relayCount()) return; + if (!schedulableCount()) return; root["schVisible"] = 1; } void _schWebSocketOnConnected(JsonObject &root){ - if (!relayCount()) return; + if (!schedulableCount()) return; JsonObject &schedules = root.createNestedObject("schedules"); schedules["max"] = SCHEDULER_MAX_SCHEDULES; @@ -62,7 +71,7 @@ void _schWebSocketOnConnected(JsonObject &root){ switch_.add(getSetting({"schSwitch", i}, 0)); action.add(getSetting({"schAction", i}, 0)); type.add(getSetting({"schType", i}, SCHEDULER_TYPE_SWITCH)); - hour.add(getSetting({"schHour", i}, 0)); + hour.add(getSetting({"schHour", i}, 12)); minute.add(getSetting({"schMinute", i}, 0)); weekdays.add(getSetting({"schWDs", i}, SCHEDULER_WEEKDAYS)); } @@ -102,7 +111,7 @@ void _schConfigure() { bool sch_enabled = getSetting({"schEnabled", i}, false); int sch_action = getSetting({"schAction", i}, 0); - int sch_hour = getSetting({"schHour", i}, 0); + int sch_hour = getSetting({"schHour", i}, 12); int sch_minute = getSetting({"schMinute", i}, 0); bool sch_utc = getSetting({"schUTC", i}, false); String sch_weekdays = getSetting({"schWDs", i}, SCHEDULER_WEEKDAYS); @@ -110,7 +119,7 @@ void _schConfigure() { DEBUG_MSG_P( PSTR("[SCH] Schedule #%d: %s #%d to %d at %02d:%02d %s on %s%s\n"), - i, SCHEDULER_TYPE_SWITCH == sch_type ? "switch" : "channel", sch_switch, + i, SCHEDULER_TYPE_SWITCH == sch_type ? "switch" : (SCHEDULER_TYPE_CURTAIN == sch_type ? "curtain" : "channel"), sch_switch, sch_action, sch_hour, sch_minute, sch_utc ? "UTC" : "local time", (char *) sch_weekdays.c_str(), sch_enabled ? "" : " (disabled)" @@ -163,6 +172,13 @@ void _schAction(unsigned char sch_id, int sch_action, int sch_switch) { lightUpdate(true, true); } #endif + + #if CURTAIN_SUPPORT == 1 + if (SCHEDULER_TYPE_CURTAIN == sch_type) { + DEBUG_MSG_P(PSTR("[SCH] Set curtain %d value to %d\n"), sch_switch, sch_action); + curtainSetPosition(sch_switch, sch_action); + } + #endif } #if NTP_LEGACY_SUPPORT @@ -230,7 +246,7 @@ void _schCheck(int relay, int daybefore) { String sch_weekdays = getSetting({"schWDs", i}, SCHEDULER_WEEKDAYS); if (_schIsThisWeekday(sch_utc ? calendar_weekday.utc_wday : calendar_weekday.local_wday, sch_weekdays)) { - int sch_hour = getSetting({"schHour", i}, 0); + int sch_hour = getSetting({"schHour", i}, 12); int sch_minute = getSetting({"schMinute", i}, 0); int sch_action = getSetting({"schAction", i}, 0); int sch_type = getSetting({"schType", i}, SCHEDULER_TYPE_SWITCH); @@ -255,6 +271,16 @@ void _schCheck(int relay, int daybefore) { } #endif + #if CURTAIN_SUPPORT == 1 + if (SCHEDULER_TYPE_CURTAIN == sch_type && sch_switch == relay && minutes_to_trigger < 0 && minutes_to_trigger > minimum_restore_time) { + minimum_restore_time = minutes_to_trigger; + saved_action = sch_action; + saved_sch = i; + } + #endif + + + if (minutes_to_trigger == 0 && relay == -1) { _schAction(i, sch_action, sch_switch); @@ -311,7 +337,7 @@ void schSetup() { static bool restore_once = true; if (restore_once) { - for (unsigned char i = 0; i < relayCount(); i++) { + for (unsigned char i = 0; i < schedulableCount(); i++) { if (getSetting({"relayLastSch", i}, 1 == SCHEDULER_RESTORE_LAST_SCHEDULE)) { _schCheck(i, 0); }