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.

207 lines
6.4 KiB

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