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.

158 lines
4.6 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. #if WEB_SUPPORT
  9. void _schWebSocketOnSend(JsonObject &root){
  10. root["maxScheduled"] = SCHEDULER_MAX_SCHEDULES;
  11. JsonArray &sch = root.createNestedArray("schedule");
  12. for (byte i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  13. if (!hasSetting("schSwitch", i)) break;
  14. JsonObject &scheduler = sch.createNestedObject();
  15. scheduler["schSwitch"] = getSetting("schSwitch", i, "");
  16. scheduler["schAction"] = getSetting("schAction", i, "");
  17. scheduler["schHour"] = getSetting("schHour", i, "");
  18. scheduler["schMinute"] = getSetting("schMinute", i, "");
  19. scheduler["schWDs"] = getSetting("schWDs", i, "");
  20. }
  21. }
  22. #endif // WEB_SUPPORT
  23. void _schConfigure() {
  24. bool delete_flag = false;
  25. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  26. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  27. if (sch_switch == 0xFF) delete_flag = true;
  28. if (delete_flag) {
  29. delSetting("schSwitch", i);
  30. delSetting("schAction", i);
  31. delSetting("schHour", i);
  32. delSetting("schMinute", i);
  33. delSetting("schWDs", i);
  34. } else {
  35. #if DEBUG_SUPPORT
  36. int sch_action = getSetting("schAction", i, 0).toInt();
  37. int sch_hour = getSetting("schHour", i, 0).toInt();
  38. int sch_minute = getSetting("schMinute", i, 0).toInt();
  39. String sch_weekdays = getSetting("schWDs", i, "");
  40. DEBUG_MSG_P(
  41. PSTR("[SCH] Schedule #%d: %s switch #%d at %02d:%02d on %s\n"),
  42. i, sch_action == 0 ? "turn OFF" : sch_action == 1 ? "turn ON" : "toggle", sch_switch,
  43. sch_hour, sch_minute, (char *) sch_weekdays.c_str()
  44. );
  45. #endif // DEBUG_SUPPORT
  46. }
  47. }
  48. }
  49. bool _schIsThisWeekday(String weekdays){
  50. // Monday = 1, Tuesday = 2 ... Sunday = 7
  51. int w = weekday(now()) - 1;
  52. if (w == 0) w = 7;
  53. char pch;
  54. char * p = (char *) weekdays.c_str();
  55. unsigned char position = 0;
  56. while (pch = p[position++]) {
  57. if ((pch - '0') == w) return true;
  58. }
  59. return false;
  60. }
  61. int _schMinutesLeft(unsigned char schedule_hour, unsigned char schedule_minute){
  62. time_t t = now();
  63. unsigned char now_hour = hour(t);
  64. unsigned char now_minute = minute(t);
  65. return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
  66. }
  67. // -----------------------------------------------------------------------------
  68. void schSetup() {
  69. _schConfigure();
  70. // Update websocket clients
  71. #if WEB_SUPPORT
  72. wsOnSendRegister(_schWebSocketOnSend);
  73. wsOnAfterParseRegister(_schConfigure);
  74. #endif
  75. // Register loop
  76. espurnaRegisterLoop(schLoop);
  77. }
  78. void schLoop() {
  79. static unsigned long last_update = 0;
  80. static int update_time = 0;
  81. // Check time has been sync'ed
  82. if (!ntpSynced()) return;
  83. // Check if we should compare scheduled and actual times
  84. if ((millis() - last_update > update_time) || (last_update == 0)) {
  85. last_update = millis();
  86. // Calculate next update time
  87. unsigned char current_second = second();
  88. update_time = (SCHEDULER_UPDATE_SEC + 60 - current_second) * 1000;
  89. for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
  90. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  91. if (sch_switch == 0xFF) break;
  92. String sch_weekdays = getSetting("schWDs", i, "");
  93. if (_schIsThisWeekday(sch_weekdays)) {
  94. int sch_hour = getSetting("schHour", i, 0).toInt();
  95. int sch_minute = getSetting("schMinute", i, 0).toInt();
  96. int minutes_to_trigger = _schMinutesLeft(sch_hour, sch_minute);
  97. if (minutes_to_trigger == 0) {
  98. int sch_action = getSetting("schAction", i, 0).toInt();
  99. if (sch_action == 2) {
  100. relayToggle(sch_switch);
  101. } else {
  102. relayStatus(sch_switch, sch_action);
  103. }
  104. DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch);
  105. } else if (minutes_to_trigger > 0) {
  106. DEBUG_MSG_P(
  107. PSTR("[SCH] %d minutes to trigger schedule #%d\n"),
  108. minutes_to_trigger, sch_switch
  109. );
  110. }
  111. }
  112. }
  113. }
  114. }
  115. #endif // SCHEDULER_SUPPORT