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.

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