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.

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