Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.1 KiB

6 years ago
  1. /*
  2. SCHEDULER MODULE
  3. Copyright (C) 2017 by faina09
  4. Adapted by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #if SCHEDULER_SUPPORT
  7. #include <TimeLib.h>
  8. // -----------------------------------------------------------------------------
  9. #if WEB_SUPPORT
  10. bool _schWebSocketOnReceive(const char * key, JsonVariant& value) {
  11. return (strncmp(key, "sch", 3) == 0);
  12. }
  13. void _schWebSocketOnSend(JsonObject &root){
  14. if (relayCount() > 0) {
  15. root["schVisible"] = 1;
  16. root["maxSchedules"] = SCHEDULER_MAX_SCHEDULES;
  17. JsonArray &sch = root.createNestedArray("schedule");
  18. for (byte i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  19. if (!hasSetting("schSwitch", i)) break;
  20. JsonObject &scheduler = sch.createNestedObject();
  21. scheduler["schEnabled"] = getSetting("schEnabled", i, 1).toInt() == 1;
  22. scheduler["schSwitch"] = getSetting("schSwitch", i, 0).toInt();
  23. scheduler["schAction"] = getSetting("schAction", i, 0).toInt();
  24. scheduler["schType"] = getSetting("schType", i, 0).toInt();
  25. scheduler["schHour"] = getSetting("schHour", i, 0).toInt();
  26. scheduler["schMinute"] = getSetting("schMinute", i, 0).toInt();
  27. scheduler["schUTC"] = getSetting("schUTC", i, 0).toInt() == 1;
  28. scheduler["schWDs"] = getSetting("schWDs", i, "");
  29. }
  30. }
  31. }
  32. #endif // WEB_SUPPORT
  33. // -----------------------------------------------------------------------------
  34. void _schConfigure() {
  35. bool delete_flag = false;
  36. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  37. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  38. if (sch_switch == 0xFF) delete_flag = true;
  39. if (delete_flag) {
  40. delSetting("schEnabled", i);
  41. delSetting("schSwitch", i);
  42. delSetting("schAction", i);
  43. delSetting("schHour", i);
  44. delSetting("schMinute", i);
  45. delSetting("schWDs", i);
  46. delSetting("schType", i);
  47. delSetting("schUTC", i);
  48. } else {
  49. #if DEBUG_SUPPORT
  50. bool sch_enabled = getSetting("schEnabled", i, 1).toInt() == 1;
  51. int sch_action = getSetting("schAction", i, 0).toInt();
  52. int sch_hour = getSetting("schHour", i, 0).toInt();
  53. int sch_minute = getSetting("schMinute", i, 0).toInt();
  54. bool sch_utc = getSetting("schUTC", i, 0).toInt() == 1;
  55. String sch_weekdays = getSetting("schWDs", i, "");
  56. unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt();
  57. DEBUG_MSG_P(
  58. PSTR("[SCH] Schedule #%d: %s #%d to %d at %02d:%02d %s on %s%s\n"),
  59. i, SCHEDULER_TYPE_SWITCH == sch_type ? "switch" : "channel", sch_switch,
  60. sch_action, sch_hour, sch_minute, sch_utc ? "UTC" : "local time",
  61. (char *) sch_weekdays.c_str(),
  62. sch_enabled ? "" : " (disabled)"
  63. );
  64. #endif // DEBUG_SUPPORT
  65. }
  66. }
  67. }
  68. bool _schIsThisWeekday(time_t t, String weekdays){
  69. // Convert from Sunday to Monday as day 1
  70. int w = weekday(t) - 1;
  71. if (0 == w) w = 7;
  72. char pch;
  73. char * p = (char *) weekdays.c_str();
  74. unsigned char position = 0;
  75. while ((pch = p[position++])) {
  76. if ((pch - '0') == w) return true;
  77. }
  78. return false;
  79. }
  80. int _schMinutesLeft(time_t t, unsigned char schedule_hour, unsigned char schedule_minute){
  81. unsigned char now_hour = hour(t);
  82. unsigned char now_minute = minute(t);
  83. return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
  84. }
  85. void _schCheck() {
  86. time_t local_time = now();
  87. time_t utc_time = ntpLocal2UTC(local_time);
  88. // Check schedules
  89. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  90. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  91. if (sch_switch == 0xFF) break;
  92. // Skip disabled schedules
  93. if (getSetting("schEnabled", i, 1).toInt() == 0) continue;
  94. // Get the datetime used for the calculation
  95. bool sch_utc = getSetting("schUTC", i, 0).toInt() == 1;
  96. time_t t = sch_utc ? utc_time : local_time;
  97. String sch_weekdays = getSetting("schWDs", i, "");
  98. if (_schIsThisWeekday(t, sch_weekdays)) {
  99. int sch_hour = getSetting("schHour", i, 0).toInt();
  100. int sch_minute = getSetting("schMinute", i, 0).toInt();
  101. int minutes_to_trigger = _schMinutesLeft(t, sch_hour, sch_minute);
  102. if (minutes_to_trigger == 0) {
  103. unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt();
  104. if (SCHEDULER_TYPE_SWITCH == sch_type) {
  105. int sch_action = getSetting("schAction", i, 0).toInt();
  106. DEBUG_MSG_P(PSTR("[SCH] Switching switch %d to %d\n"), sch_switch, sch_action);
  107. if (sch_action == 2) {
  108. relayToggle(sch_switch);
  109. } else {
  110. relayStatus(sch_switch, sch_action);
  111. }
  112. }
  113. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  114. if (SCHEDULER_TYPE_DIM == sch_type) {
  115. int sch_brightness = getSetting("schAction", i, -1).toInt();
  116. DEBUG_MSG_P(PSTR("[SCH] Set channel %d value to %d\n"), sch_switch, sch_brightness);
  117. lightChannel(sch_switch, sch_brightness);
  118. lightUpdate(true, true);
  119. }
  120. #endif
  121. DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), i);
  122. // Show minutes to trigger every 15 minutes
  123. // or every minute if less than 15 minutes to scheduled time.
  124. // This only works for schedules on this same day.
  125. // For instance, if your scheduler is set for 00:01 you will only
  126. // get one notification before the trigger (at 00:00)
  127. } else if (minutes_to_trigger > 0) {
  128. #if DEBUG_SUPPORT
  129. if ((minutes_to_trigger % 15 == 0) || (minutes_to_trigger < 15)) {
  130. DEBUG_MSG_P(
  131. PSTR("[SCH] %d minutes to trigger schedule #%d\n"),
  132. minutes_to_trigger, i
  133. );
  134. }
  135. #endif
  136. }
  137. }
  138. }
  139. }
  140. void _schLoop() {
  141. // Check time has been sync'ed
  142. if (!ntpSynced()) return;
  143. // Check schedules every minute at hh:mm:00
  144. static unsigned long last_minute = 60;
  145. unsigned char current_minute = minute();
  146. if (current_minute != last_minute) {
  147. last_minute = current_minute;
  148. _schCheck();
  149. }
  150. }
  151. // -----------------------------------------------------------------------------
  152. void schSetup() {
  153. _schConfigure();
  154. // Update websocket clients
  155. #if WEB_SUPPORT
  156. wsOnSendRegister(_schWebSocketOnSend);
  157. wsOnReceiveRegister(_schWebSocketOnReceive);
  158. wsOnAfterParseRegister(_schConfigure);
  159. #endif
  160. // Register loop
  161. espurnaRegisterLoop(_schLoop);
  162. }
  163. #endif // SCHEDULER_SUPPORT