-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmyTime.cpp
More file actions
82 lines (64 loc) · 2.33 KB
/
myTime.cpp
File metadata and controls
82 lines (64 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "myTime.h"
#include <AceWire.h> // TwoWireInterface
#include <Wire.h> // TwoWire, Wire
#include <AceTimeClock.h> // https://github.com/bxparks/AceTimeClock/
#include "myConfig.h"
extern Config config;
using namespace ace_time;
using ace_time::acetime_t;
using ace_time::OffsetDateTime;
using ace_time::clock::SystemClockLoop;
using ace_time::clock::NtpClock;
using ace_time::clock::DS3231Clock;
using WireInterface = ace_wire::TwoWireInterface<TwoWire>;
WireInterface wireInterface(Wire);
DS3231Clock<WireInterface> dsClock(wireInterface);
//NtpClock ntpClock("time.kfki.hu");
//SystemClockLoop systemClock(&ntpClock /*reference*/, &dsClock /*backup*/);
NtpClock ntpClock;
SystemClockLoop* systemClock;
// Create an ExtendedZoneManager with the entire TZ Database of Zone and Link
// entries. Cache size of 3 means that it can support 3 concurrent timezones
// without performance penalties.
static const int CACHE_SIZE = 3;
static ExtendedZoneProcessorCache<CACHE_SIZE> zoneProcessorCache;
static ExtendedZoneManager manager(zonedbx::kZoneAndLinkRegistrySize, zonedbx::kZoneAndLinkRegistry, zoneProcessorCache);
void printCurrentTime() {
acetime_t nowSeconds = systemClock->getNow();
TimeZone currentTz = manager.createForZoneName(config.timezone);
ZonedDateTime localTime = ZonedDateTime::forEpochSeconds(nowSeconds, currentTz);
//localTime.printTo(Serial);
//Serial.println();
}
Time getLocalTime() {
acetime_t nowSeconds = systemClock->getNow();
TimeZone currentTz = manager.createForZoneName(config.timezone);
ZonedDateTime localTime = ZonedDateTime::forEpochSeconds(nowSeconds, currentTz);
Time t;
t.year = localTime.year();
t.month = localTime.month();
t.day = localTime.day();
t.hour = localTime.hour();
t.minute = localTime.minute();
t.second = localTime.second();
printCurrentTime(); // only for debug purposes
return t;
}
void syncTimeSetup() {
new (&ntpClock) NtpClock(config.ntp_server);
systemClock = new SystemClockLoop(&ntpClock /*reference*/, &dsClock /*backup*/);
Wire.begin();
wireInterface.begin();
dsClock.setup();
//Serial.printf("NTP server: %s\n", ntpClock.getServer());
ntpClock.setup();
if (ntpClock.isSetup()) {
infoLog("NTP setup OK.");
} else {
errorLog("Connection failed... try again.");
}
systemClock->setup();
}
void syncTimeLoop() {
systemClock->loop();
}