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.

199 lines
6.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. void _schWebSocketOnSend(JsonObject &root){
  11. root["schVisible"] = 1;
  12. root["maxScheduled"] = SCHEDULER_MAX_SCHEDULES;
  13. JsonArray &sch = root.createNestedArray("schedule");
  14. for (byte i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  15. if (!hasSetting("schSwitch", i)) break;
  16. JsonObject &scheduler = sch.createNestedObject();
  17. scheduler["schEnabled"] = getSetting("schEnabled", i, 1).toInt() == 1;
  18. scheduler["schSwitch"] = getSetting("schSwitch", i, 0).toInt();
  19. scheduler["schAction"] = getSetting("schAction", i, 0).toInt();
  20. scheduler["schType"] = getSetting("schType", i, 0);
  21. scheduler["schHour"] = getSetting("schHour", i, 0).toInt();
  22. scheduler["schMinute"] = getSetting("schMinute", i, 0).toInt();
  23. scheduler["schWDs"] = getSetting("schWDs", i, "");
  24. }
  25. }
  26. #endif // WEB_SUPPORT
  27. // -----------------------------------------------------------------------------
  28. void _schConfigure() {
  29. bool delete_flag = false;
  30. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  31. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  32. if (sch_switch == 0xFF) delete_flag = true;
  33. if (delete_flag) {
  34. delSetting("schEnabled", i);
  35. delSetting("schSwitch", i);
  36. delSetting("schAction", i);
  37. delSetting("schHour", i);
  38. delSetting("schMinute", i);
  39. delSetting("schWDs", i);
  40. delSetting("schType", i);
  41. } else {
  42. #if DEBUG_SUPPORT
  43. int sch_enabled = getSetting("schEnabled", i, 1).toInt() == 1;
  44. int sch_action = getSetting("schAction", i, 0).toInt();
  45. int sch_hour = getSetting("schHour", i, 0).toInt();
  46. int sch_minute = getSetting("schMinute", i, 0).toInt();
  47. String sch_weekdays = getSetting("schWDs", i, "");
  48. DEBUG_MSG_P(
  49. PSTR("[SCH] Schedule #%d: %s switch #%d at %02d:%02d on %s%s\n"),
  50. i, sch_action == 0 ? "turn OFF" : sch_action == 1 ? "turn ON" : "toggle", sch_switch,
  51. sch_hour, sch_minute, (char *) sch_weekdays.c_str(),
  52. sch_enabled ? "" : " (disabled)"
  53. );
  54. #endif // DEBUG_SUPPORT
  55. }
  56. }
  57. }
  58. bool _schIsThisWeekday(String weekdays){
  59. // Convert from Sunday to Monday as day 1
  60. int w = weekday(now()) - 1;
  61. if (w == 0) w = 7;
  62. char pch;
  63. char * p = (char *) weekdays.c_str();
  64. unsigned char position = 0;
  65. while (pch = p[position++]) {
  66. if ((pch - '0') == w) return true;
  67. }
  68. return false;
  69. }
  70. int _schMinutesLeft(unsigned char schedule_hour, unsigned char schedule_minute){
  71. time_t t = now();
  72. unsigned char now_hour = hour(t);
  73. unsigned char now_minute = minute(t);
  74. return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
  75. }
  76. void _schCheck() {
  77. // Check schedules
  78. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  79. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  80. if (sch_switch == 0xFF) break;
  81. // Skip disabled schedules
  82. if (getSetting("schEnabled", i, 1).toInt() == 0) continue;
  83. String sch_weekdays = getSetting("schWDs", i, "");
  84. if (_schIsThisWeekday(sch_weekdays)) {
  85. int sch_hour = getSetting("schHour", i, 0).toInt();
  86. int sch_minute = getSetting("schMinute", i, 0).toInt();
  87. int minutes_to_trigger = _schMinutesLeft(sch_hour, sch_minute);
  88. if (minutes_to_trigger == 0) {
  89. if (getSetting("schType", i, "") == "light") {
  90. int sch_brightness = getSetting("schAction", i, -1).toInt();
  91. DEBUG_MSG_P(PSTR("[SCH] Switching light %d to %d\n"), sch_switch, sch_brightness);
  92. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  93. lightChannel(sch_switch, sch_brightness);
  94. lightUpdate(true, true);
  95. #endif
  96. } else {
  97. int sch_action = getSetting("schAction", i, 0).toInt();
  98. DEBUG_MSG_P(PSTR("[SCH] Switching switch %d to %d\n"), sch_switch, sch_action);
  99. if (sch_action == 2) {
  100. relayToggle(sch_switch);
  101. } else {
  102. relayStatus(sch_switch, sch_action);
  103. }
  104. }
  105. DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch);
  106. // Show minutes to trigger every 15 minutes
  107. // or every minute if less than 15 minutes to scheduled time.
  108. // This only works for schedules on this same day.
  109. // For instance, if your scheduler is set for 00:01 you will only
  110. // get one notification before the trigger (at 00:00)
  111. } else if (minutes_to_trigger > 0) {
  112. #if DEBUG_SUPPORT
  113. if ((minutes_to_trigger % 15 == 0) || (minutes_to_trigger < 15)) {
  114. DEBUG_MSG_P(
  115. PSTR("[SCH] %d minutes to trigger schedule #%d\n"),
  116. minutes_to_trigger, sch_switch
  117. );
  118. }
  119. #endif
  120. }
  121. }
  122. }
  123. }
  124. void _schLoop() {
  125. // Check time has been sync'ed
  126. if (!ntpSynced()) return;
  127. // Check schedules every minute at hh:mm:00
  128. static unsigned long last_minute = 60;
  129. unsigned char current_minute = minute();
  130. if (current_minute != last_minute) {
  131. last_minute = current_minute;
  132. _schCheck();
  133. }
  134. }
  135. // -----------------------------------------------------------------------------
  136. void schSetup() {
  137. _schConfigure();
  138. // Update websocket clients
  139. #if WEB_SUPPORT
  140. wsOnSendRegister(_schWebSocketOnSend);
  141. wsOnAfterParseRegister(_schConfigure);
  142. #endif
  143. // Register loop
  144. espurnaRegisterLoop(_schLoop);
  145. }
  146. #endif // SCHEDULER_SUPPORT