Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TimerManager.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * TimerManager.cpp
  3. *
  4. * Created on: 12.07.2026
  5. * Author: FSmilari
  6. */
  7. #include "TimerManager.h"
  8. WiFiUDP ntpUDP;
  9. // Set up your base timezone offset in seconds (e.g., UTC+1 for Baar, Switzerland is 3600)
  10. const long gmtOffset_sec = 3600;
  11. // Set up DST offset in seconds (1 hour = 3600 seconds)
  12. const long daylightOffset_sec = 3600;
  13. TimerManager::TimerManager(ConfigManager &configManager)
  14. : configManager(configManager), ntpClient(ntpUDP, "pool.ntp.org", gmtOffset_sec) {
  15. }
  16. TimerManager::~TimerManager() {
  17. }
  18. void TimerManager::begin() {
  19. ntpClient.begin();
  20. }
  21. void TimerManager::onTimerStatusChange(const char *timerStatus) {
  22. Config cfg = configManager.getConfig();
  23. cfg.timer.on = String(timerStatus).equalsIgnoreCase("on");
  24. configManager.saveConfig(cfg);
  25. }
  26. void TimerManager::onTimerIntervalChange(String timerIntervalName, const char *timerInterval) {
  27. TimeInterval interval;
  28. parseTimeInterval(timerInterval, interval);
  29. saveTimerInterval(timerIntervalName, interval.start, interval.end);
  30. }
  31. void TimerManager::saveTimerInterval(String timerIntervalName, uint32_t start, uint32_t end) {
  32. Config cfg = configManager.getConfig();
  33. if (timerIntervalName.equals("timeInterval1")) {
  34. cfg.timer.timeInterval1.start = start;
  35. cfg.timer.timeInterval1.end = end;
  36. } else if (timerIntervalName.equals("timeInterval2")) {
  37. cfg.timer.timeInterval2.start = start;
  38. cfg.timer.timeInterval2.end = end;
  39. }
  40. configManager.saveConfig(cfg);
  41. }
  42. uint32_t TimerManager::timeToSeconds(uint8_t hour, uint8_t minute, uint8_t second) {
  43. return (uint32_t) hour * 3600UL +
  44. (uint32_t) minute * 60UL +
  45. second;
  46. }
  47. bool TimerManager::parseTimeInterval(const char *str, TimeInterval &interval) {
  48. unsigned int sh, sm, ss;
  49. unsigned int eh, em, es;
  50. if (sscanf(str,
  51. "%u:%u:%u - %u:%u:%u",
  52. &sh, &sm, &ss,
  53. &eh, &em, &es) != 6) {
  54. return false;
  55. }
  56. // Bereich prüfen
  57. if (sh > 23 || eh > 23 ||
  58. sm > 59 || em > 59 ||
  59. ss > 59 || es > 59) {
  60. return false;
  61. }
  62. interval.start = timeToSeconds(sh, sm, ss);
  63. interval.end = timeToSeconds(eh, em, es);
  64. return true;
  65. }
  66. bool TimerManager::isNowInTimeInterval() {
  67. Config cfg = configManager.getConfig();
  68. ntpClient.update();
  69. time_t localEpochTime = ntpClient.getEpochTime() + gmtOffset_sec;
  70. time_t localTime = ntpClient.getHours() * 3600 + ntpClient.getMinutes() * 60 + ntpClient.getSeconds();
  71. bool isDst = isDST(month(localEpochTime), day(localEpochTime), weekday(localEpochTime));
  72. if (isDst) {
  73. localTime += daylightOffset_sec;
  74. }
  75. Serial.print("isDST: ");
  76. Serial.println(isDst ? "true" : "false");
  77. Serial.println(ntpClient.getFormattedTime());
  78. Serial.print("localTime: ");
  79. Serial.println(secondsToFormatedTime(localTime));
  80. return (isInInterval(localTime, cfg.timer.timeInterval1) || isInInterval(localTime, cfg.timer.timeInterval2));
  81. }
  82. bool TimerManager::isDST(int month, int day, int weekday) {
  83. // DST is between last Sunday of March (month 3) and last Sunday of October (month 10)
  84. if (month > 3 && month < 10)
  85. return true;
  86. if (month == 3) {
  87. int lastSunday = 31 - (weekday - 1); // math to find the last Sunday
  88. if (day >= lastSunday)
  89. return true;
  90. }
  91. if (month == 10) {
  92. int lastSunday = 31 - (weekday - 1);
  93. if (day < lastSunday)
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool TimerManager::isInInterval(uint32_t now, const TimeInterval &interval) {
  99. if (interval.start <= interval.end) {
  100. // normales Intervall
  101. return now >= interval.start && now < interval.end;
  102. }
  103. // Intervall geht über Mitternacht
  104. return now >= interval.start || now < interval.end;
  105. }
  106. String TimerManager::secondsToFormatedTime(uint32_t seconds) {
  107. uint8_t hour = seconds / 3600;
  108. seconds %= 3600;
  109. uint8_t minute = seconds / 60;
  110. uint8_t second = seconds % 60;
  111. char buffer[9]; // "HH:mm:ss" + '\0'
  112. snprintf(buffer, sizeof(buffer), "%02u:%02u:%02u",
  113. hour, minute, second);
  114. return String(buffer);
  115. }