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.

115 lines
4.6 KiB

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