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.

155 lines
4.4 KiB

  1. /*
  2. A SCHEDULER MODULE
  3. Copyright (C) 2017 by faina09
  4. */
  5. #if SCHEDULER_SUPPORT
  6. #include <NtpClientLib.h>
  7. #if WEB_SUPPORT
  8. void _schWebSocketOnSend(JsonObject &root){
  9. root["maxScheduled"] = MAX_SCHEDULED;
  10. JsonArray &sch = root.createNestedArray("schedule");
  11. for (byte i = 0; i < MAX_SCHEDULED; i++) {
  12. if (!hasSetting("schSwitch", i)) break;
  13. JsonObject &scheduler = sch.createNestedObject();
  14. scheduler["schSwitch"] = getSetting("schSwitch", i, "");
  15. scheduler["schAction"] = getSetting("schAction", i, "");
  16. scheduler["schHour"] = getSetting("schHour", i, "");
  17. scheduler["schMinute"] = getSetting("schMinute", i, "");
  18. scheduler["schWDs"] = getSetting("schWDs", i, "");
  19. }
  20. }
  21. void _schConfigure() {
  22. bool delete_flag = false;
  23. for (unsigned char i = 0; i < MAX_SCHEDULED; i++) {
  24. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  25. if (sch_switch == 0xFF) delete_flag = true;
  26. if (delete_flag) {
  27. delSetting("schSwitch", i);
  28. delSetting("schAction", i);
  29. delSetting("schHour", i);
  30. delSetting("schMinute", i);
  31. delSetting("schWDs", i);
  32. } else {
  33. #if DEBUG_SUPPORT
  34. int sch_operation = getSetting("schAction", i, 0).toInt();
  35. int sch_hour = getSetting("schHour", i, 0).toInt();
  36. int sch_minute = getSetting("schMinute", i, 0).toInt();
  37. String sch_weekdays = getSetting("schWDs", i, "");
  38. DEBUG_MSG_P(PSTR("[SCH] Turn switch #%d %s at %02d:%02d on %s\n"), sch_switch, sch_operation ? "ON" : "OFF", sch_hour, sch_minute, (char *)sch_weekdays.c_str());
  39. #endif // DEBUG_SUPPORT
  40. }
  41. }
  42. }
  43. #endif // WEB_SUPPORT
  44. bool _isThisWeekday(String weekdays){
  45. //Sunday = 1, Monday = 2, ...
  46. int w = weekday(now());
  47. //DEBUG_MSG_P(PSTR("[SCH] NTP weekday: %d\n"), w);
  48. char * pch;
  49. char * p = (char *) weekdays.c_str();
  50. while ((pch = strtok_r(p, ",", &p)) != NULL) {
  51. //DEBUG_MSG_P(PSTR("[SCH] w found: %d\n"), atoi(pch));
  52. if (atoi(pch) == w) return true;
  53. }
  54. return false;
  55. }
  56. int _diffTime(unsigned char schedule_hour, unsigned char schedule_minute){
  57. unsigned char now_hour;
  58. unsigned char now_minute;
  59. if (ntpConnected()) {
  60. String value = NTP.getTimeDateString();
  61. now_hour = value.substring(0, 2).toInt();
  62. now_minute = value.substring(3, 5).toInt();
  63. } else {
  64. time_t t = now();
  65. now_hour = hour(t);
  66. now_minute = minute(t);
  67. }
  68. return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
  69. }
  70. // -----------------------------------------------------------------------------
  71. void schSetup() {
  72. _schConfigure();
  73. // Update websocket clients
  74. #if WEB_SUPPORT
  75. wsOnSendRegister(_schWebSocketOnSend);
  76. wsOnAfterParseRegister(_schConfigure);
  77. #endif
  78. }
  79. void schLoop(){
  80. static unsigned long last_update = 0;
  81. static int update_time = 0;
  82. // Check if we should compare scheduled and actual times
  83. if ((millis() - last_update > update_time) || (last_update == 0)) {
  84. last_update = millis();
  85. // Calculate next update time
  86. unsigned char current_second = ntpConnected() ?
  87. NTP.getTimeDateString().substring(6, 8).toInt() :
  88. second(now())
  89. ;
  90. update_time = (SCH_UPDATE_SEC + 60 - current_second) * 1000;
  91. for (unsigned char i = 0; i < MAX_SCHEDULED; i++) {
  92. int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
  93. if (sch_switch == 0xFF) break;
  94. String sch_weekdays = getSetting("schWDs", i, "");
  95. if (_isThisWeekday(sch_weekdays)) {
  96. int sch_hour = getSetting("schHour", i, 0).toInt();
  97. int sch_minute = getSetting("schMinute", i, 0).toInt();
  98. int minutes_to_trigger = _diffTime(sch_hour, sch_minute);
  99. if (minutes_to_trigger == 0) {
  100. int sch_operation = getSetting("schAction", i, 0).toInt();
  101. relayStatus(sch_switch, sch_operation);
  102. DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch);
  103. } else if (minutes_to_trigger > 0) {
  104. DEBUG_MSG_P(PSTR("[SCH] %d minutes to trigger schedule #%d\n"), minutes_to_trigger, sch_switch);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. #endif // SCHEDULER_SUPPORT