supla-device
Loading...
Searching...
No Matches
DS1307RTC.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_CLOCK_DS1307RTC_H_
5#define SRC_SUPLA_CLOCK_DS1307RTC_H_
6
7/*
8Dependency: https://github.com/adafruit/RTClib,
9use library manager to install it
10*/
11
12#include <supla/log_wrapper.h>
13#include <time.h>
14#include "clock.h"
15
16#include "RTClib.h"
17
18namespace Supla {
19
20class DS1307RTC : public Clock {
21 public:
22 DS1307RTC() {}
23
24 void onInit() {
25 if (!rtc.begin()) {
26 SUPLA_LOG_DEBUG("Unable to find RTC");
27 } else {
28 struct tm timeinfo {};
29 isRTCReady = true;
30 RTCLostPower = (rtc.isrunning()) ? false : true;
31 DateTime now = rtc.now();
32
33 timeinfo.tm_year = now.year() - 1900;
34 timeinfo.tm_mon = now.month() - 1;
35 timeinfo.tm_mday = now.day();
36 timeinfo.tm_hour = now.hour();
37 timeinfo.tm_min = now.minute();
38 timeinfo.tm_sec = now.second();
39
40 localtime = mktime(&timeinfo);
41
42#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
43 timeval tv = {localtime, 0};
44 settimeofday(&tv, nullptr);
45#elif defined(ARDUINO_ARCH_AVR)
46 set_system_time(mktime(&timeinfo));
47#endif
48 printCurrentTime("from RTC:");
49
50 if (getYear() >= 2023) {
51 isClockReady = true;
52 } else {
53 SUPLA_LOG_DEBUG("Clock is not ready");
54 }
55 }
56 }
57
58 bool rtcIsReady() {
59 return isRTCReady;
60 }
61
62 bool getRTCLostPowerFlag() {
63 return RTCLostPower;
64 }
65
66 void resetRTCLostPowerFlag() {
67 RTCLostPower = false;
68 }
69
70 void parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result) {
71 struct tm timeinfo {};
72
73 isClockReady = true;
74
75 printCurrentTime("current");
76
77 timeinfo.tm_year = result->year - 1900;
78 timeinfo.tm_mon = result->month - 1;
79 timeinfo.tm_mday = result->day;
80 timeinfo.tm_hour = result->hour;
81 timeinfo.tm_min = result->min;
82 timeinfo.tm_sec = result->sec;
83
84 localtime = mktime(&timeinfo);
85
86#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
87 timeval tv = {localtime, 0};
88 settimeofday(&tv, nullptr);
89#elif defined(ARDUINO_ARCH_AVR)
90 set_system_time(mktime(&timeinfo));
91#endif
92 printCurrentTime("new");
93
94 // Update RTC if minutes or seconds are different
95 // from the time obtained from the server
96 if (isRTCReady) {
97 DateTime now = rtc.now();
98 if ((now.year() != getYear()) || (now.month() != getMonth()) ||
99 (now.day() != getDay()) || (now.hour() != getHour()) ||
100 (now.minute() != getMin()) || (now.second() - getSec() > 5) ||
101 (now.second() - getSec() < -5)) {
102 rtc.adjust(DateTime(getTimeStamp()));
103 SUPLA_LOG_DEBUG("Update RTC time from server");
104 }
105 }
106}
107
108 protected:
109 RTC_DS1307 rtc;
110 bool RTCLostPower = false;
111 bool isRTCReady = false;
112};
113
114}; // namespace Supla
115
116#endif // SRC_SUPLA_CLOCK_DS1307RTC_H_
Definition clock.h:16
Definition DS1307RTC.h:20
void onInit()
Third method called on element in SuplaDevice.begin()
Definition DS1307RTC.h:24
Definition proto.h:2642