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.

101 lines
4.0 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 %d AT %02d:%02d ON %s\n"), sch_switch, sch_operation, 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. // TODO (?) start every minute and 0 seconds
  41. static unsigned long last_update = 0;
  42. static bool r_on = true;
  43. if ((millis() - last_update > SCH_UPDATE_INTERVAL) || (last_update == 0)) {
  44. last_update = millis();
  45. int i;
  46. for (i = 0; i < MAX_SCHEDULED; i++) {
  47. if (getSetting("sch_switch" + String(i)).length() == 0)
  48. break;
  49. String sch_weekdays = getSetting("sch_weekdays" + String(i));
  50. if (isThisWday(sch_weekdays)) {
  51. int sch_switch = getSetting("sch_switch" + String(i)).toInt();
  52. int sch_operation = getSetting("sch_operation" + String(i)).toInt();
  53. int sch_hour = getSetting("sch_hour" + String(i)).toInt();
  54. int sch_minute = getSetting("sch_minute" + String(i)).toInt();
  55. DEBUG_MSG_P(PSTR("[SCH] Today it will turn switch #%d %d @ %02d:%02d\n"), sch_switch, sch_operation, sch_hour, sch_minute);
  56. int minToTrigger = diffTime(sch_hour, sch_minute);
  57. if (minToTrigger == 0) {
  58. relayStatus(sch_switch, sch_operation);
  59. DEBUG_MSG_P(PSTR("[SCH] TRIGGERED!!\n"));
  60. }
  61. if (minToTrigger < 0) {
  62. //DEBUG_MSG_P(PSTR("[SCH] Time now: %s\n"), (char *)ntpDateTime().c_str()); // aaaa/mm/dd hh:mm:ss
  63. DEBUG_MSG_P(PSTR("[SCH] Time now: %s, %d minutes to trigger %02d:%02d\n"), (char *)ntpDateTime().c_str(), minToTrigger, sch_hour, sch_minute);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. bool isThisWday(String weekdays){
  70. //Sunday = 1, Monday = 2, ...
  71. int w = weekday(now());
  72. //DEBUG_MSG_P(PSTR("[SCH] ntp weekday: %d\n"), w);
  73. char * pch;
  74. char * p = (char *)weekdays.c_str();
  75. while ((pch = strtok_r(p, ",", &p)) != NULL) {
  76. //DEBUG_MSG_P(PSTR("[SCH] w found: %d\n"), atoi(pch));
  77. if (atoi(pch) == w) return true;
  78. }
  79. return false;
  80. }
  81. int diffTime(int schhour, int schminute){
  82. if (!ntpConnected())
  83. return 9999; //NTP time not set
  84. String value = NTP.getTimeDateString();
  85. int hour = value.substring(0, 2).toInt();
  86. int minute = value.substring(3, 5).toInt();
  87. //DEBUG_MSG_P(PSTR("[SCH] ntp time: %02d:%02d\n"), hour, minute);
  88. //DEBUG_MSG_P(PSTR("[SCH] cmp time: %02d:%02d\n"), schhour, schminute);
  89. return (hour - schhour) * 60 + minute - schminute;
  90. }
  91. #endif