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.

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