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.

197 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. lightChannel(sch_switch, sch_brightness);
  93. lightUpdate(true, true);
  94. } else {
  95. int sch_action = getSetting("schAction", i, 0).toInt();
  96. DEBUG_MSG_P(PSTR("[SCH] Switching switch %d to %d\n"), sch_switch, sch_action);
  97. if (sch_action == 2) {
  98. relayToggle(sch_switch);
  99. } else {
  100. relayStatus(sch_switch, sch_action);
  101. }
  102. }
  103. DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch);
  104. // Show minutes to trigger every 15 minutes
  105. // or every minute if less than 15 minutes to scheduled time.
  106. // This only works for schedules on this same day.
  107. // For instance, if your scheduler is set for 00:01 you will only
  108. // get one notification before the trigger (at 00:00)
  109. } else if (minutes_to_trigger > 0) {
  110. #if DEBUG_SUPPORT
  111. if ((minutes_to_trigger % 15 == 0) || (minutes_to_trigger < 15)) {
  112. DEBUG_MSG_P(
  113. PSTR("[SCH] %d minutes to trigger schedule #%d\n"),
  114. minutes_to_trigger, sch_switch
  115. );
  116. }
  117. #endif
  118. }
  119. }
  120. }
  121. }
  122. void _schLoop() {
  123. // Check time has been sync'ed
  124. if (!ntpSynced()) return;
  125. // Check schedules every minute at hh:mm:00
  126. static unsigned long last_minute = 60;
  127. unsigned char current_minute = minute();
  128. if (current_minute != last_minute) {
  129. last_minute = current_minute;
  130. _schCheck();
  131. }
  132. }
  133. // -----------------------------------------------------------------------------
  134. void schSetup() {
  135. _schConfigure();
  136. // Update websocket clients
  137. #if WEB_SUPPORT
  138. wsOnSendRegister(_schWebSocketOnSend);
  139. wsOnAfterParseRegister(_schConfigure);
  140. #endif
  141. // Register loop
  142. espurnaRegisterLoop(_schLoop);
  143. }
  144. #endif // SCHEDULER_SUPPORT