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.

242 lines
7.5 KiB

6 years ago
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. bool _schWebSocketOnReceive(const char * key, JsonVariant& value) {
  11. return (strncmp(key, "sch", 3) == 0);
  12. }
  13. void _schWebSocketOnSend(JsonObject &root){
  14. if (!relayCount()) return;
  15. root["schVisible"] = 1;
  16. root["maxSchedules"] = SCHEDULER_MAX_SCHEDULES;
  17. JsonObject &schedules = root.createNestedObject("schedules");
  18. uint8_t size = 0;
  19. JsonArray& enabled = schedules.createNestedArray("schEnabled");
  20. JsonArray& switch_ = schedules.createNestedArray("schSwitch");
  21. JsonArray& action = schedules.createNestedArray("schAction");
  22. JsonArray& type = schedules.createNestedArray("schType");
  23. JsonArray& hour = schedules.createNestedArray("schHour");
  24. JsonArray& minute = schedules.createNestedArray("schMinute");
  25. JsonArray& utc = schedules.createNestedArray("schUTC");
  26. JsonArray& weekdays = schedules.createNestedArray("schWDs");
  27. for (byte i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  28. if (!hasSetting("schSwitch", i)) break;
  29. ++size;
  30. enabled.add<uint8_t>(getSetting("schEnabled", i, 1).toInt() == 1);
  31. utc.add<uint8_t>(getSetting("schUTC", i, 0).toInt() == 1);
  32. switch_.add(getSetting("schSwitch", i, 0).toInt());
  33. action.add(getSetting("schAction", i, 0).toInt());
  34. type.add(getSetting("schType", i, 0).toInt());
  35. hour.add(getSetting("schHour", i, 0).toInt());
  36. minute.add(getSetting("schMinute", i, 0).toInt());
  37. weekdays.add(getSetting("schWDs", i, ""));
  38. }
  39. schedules["size"] = size;
  40. schedules["start"] = 0;
  41. }
  42. #endif // WEB_SUPPORT
  43. // -----------------------------------------------------------------------------
  44. void _schConfigure() {
  45. bool delete_flag = false;
  46. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  47. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  48. if (sch_switch == 0xFF) delete_flag = true;
  49. if (delete_flag) {
  50. delSetting("schEnabled", i);
  51. delSetting("schSwitch", i);
  52. delSetting("schAction", i);
  53. delSetting("schHour", i);
  54. delSetting("schMinute", i);
  55. delSetting("schWDs", i);
  56. delSetting("schType", i);
  57. delSetting("schUTC", i);
  58. } else {
  59. #if DEBUG_SUPPORT
  60. bool sch_enabled = getSetting("schEnabled", i, 1).toInt() == 1;
  61. int sch_action = getSetting("schAction", i, 0).toInt();
  62. int sch_hour = getSetting("schHour", i, 0).toInt();
  63. int sch_minute = getSetting("schMinute", i, 0).toInt();
  64. bool sch_utc = getSetting("schUTC", i, 0).toInt() == 1;
  65. String sch_weekdays = getSetting("schWDs", i, "");
  66. unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt();
  67. DEBUG_MSG_P(
  68. PSTR("[SCH] Schedule #%d: %s #%d to %d at %02d:%02d %s on %s%s\n"),
  69. i, SCHEDULER_TYPE_SWITCH == sch_type ? "switch" : "channel", sch_switch,
  70. sch_action, sch_hour, sch_minute, sch_utc ? "UTC" : "local time",
  71. (char *) sch_weekdays.c_str(),
  72. sch_enabled ? "" : " (disabled)"
  73. );
  74. #endif // DEBUG_SUPPORT
  75. }
  76. }
  77. }
  78. bool _schIsThisWeekday(time_t t, String weekdays){
  79. // Convert from Sunday to Monday as day 1
  80. int w = weekday(t) - 1;
  81. if (0 == w) w = 7;
  82. char pch;
  83. char * p = (char *) weekdays.c_str();
  84. unsigned char position = 0;
  85. while ((pch = p[position++])) {
  86. if ((pch - '0') == w) return true;
  87. }
  88. return false;
  89. }
  90. int _schMinutesLeft(time_t t, unsigned char schedule_hour, unsigned char schedule_minute){
  91. unsigned char now_hour = hour(t);
  92. unsigned char now_minute = minute(t);
  93. return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
  94. }
  95. void _schCheck() {
  96. time_t local_time = now();
  97. time_t utc_time = ntpLocal2UTC(local_time);
  98. // Check schedules
  99. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  100. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  101. if (sch_switch == 0xFF) break;
  102. // Skip disabled schedules
  103. if (getSetting("schEnabled", i, 1).toInt() == 0) continue;
  104. // Get the datetime used for the calculation
  105. bool sch_utc = getSetting("schUTC", i, 0).toInt() == 1;
  106. time_t t = sch_utc ? utc_time : local_time;
  107. String sch_weekdays = getSetting("schWDs", i, "");
  108. if (_schIsThisWeekday(t, sch_weekdays)) {
  109. int sch_hour = getSetting("schHour", i, 0).toInt();
  110. int sch_minute = getSetting("schMinute", i, 0).toInt();
  111. int minutes_to_trigger = _schMinutesLeft(t, sch_hour, sch_minute);
  112. if (minutes_to_trigger == 0) {
  113. unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt();
  114. if (SCHEDULER_TYPE_SWITCH == sch_type) {
  115. int sch_action = getSetting("schAction", i, 0).toInt();
  116. DEBUG_MSG_P(PSTR("[SCH] Switching switch %d to %d\n"), sch_switch, sch_action);
  117. if (sch_action == 2) {
  118. relayToggle(sch_switch);
  119. } else {
  120. relayStatus(sch_switch, sch_action);
  121. }
  122. }
  123. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  124. if (SCHEDULER_TYPE_DIM == sch_type) {
  125. int sch_brightness = getSetting("schAction", i, -1).toInt();
  126. DEBUG_MSG_P(PSTR("[SCH] Set channel %d value to %d\n"), sch_switch, sch_brightness);
  127. lightChannel(sch_switch, sch_brightness);
  128. lightUpdate(true, true);
  129. }
  130. #endif
  131. DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), i);
  132. // Show minutes to trigger every 15 minutes
  133. // or every minute if less than 15 minutes to scheduled time.
  134. // This only works for schedules on this same day.
  135. // For instance, if your scheduler is set for 00:01 you will only
  136. // get one notification before the trigger (at 00:00)
  137. } else if (minutes_to_trigger > 0) {
  138. #if DEBUG_SUPPORT
  139. if ((minutes_to_trigger % 15 == 0) || (minutes_to_trigger < 15)) {
  140. DEBUG_MSG_P(
  141. PSTR("[SCH] %d minutes to trigger schedule #%d\n"),
  142. minutes_to_trigger, i
  143. );
  144. }
  145. #endif
  146. }
  147. }
  148. }
  149. }
  150. void _schLoop() {
  151. // Check time has been sync'ed
  152. if (!ntpSynced()) return;
  153. // Check schedules every minute at hh:mm:00
  154. static unsigned long last_minute = 60;
  155. unsigned char current_minute = minute();
  156. if (current_minute != last_minute) {
  157. last_minute = current_minute;
  158. _schCheck();
  159. }
  160. }
  161. // -----------------------------------------------------------------------------
  162. void schSetup() {
  163. _schConfigure();
  164. // Update websocket clients
  165. #if WEB_SUPPORT
  166. wsOnSendRegister(_schWebSocketOnSend);
  167. wsOnReceiveRegister(_schWebSocketOnReceive);
  168. #endif
  169. // Main callbacks
  170. espurnaRegisterLoop(_schLoop);
  171. espurnaRegisterReload(_schConfigure);
  172. }
  173. #endif // SCHEDULER_SUPPORT