-
Notifications
You must be signed in to change notification settings - Fork 3
/
HostTime.cpp
124 lines (108 loc) · 3.92 KB
/
HostTime.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "HostTime.h"
//SemaphoreHandle_t HostTime::timerUpdateEvent;
HostTime* HostTime::getInstance()
{
static HostTime* instance = nullptr;
if(instance == nullptr)
{
instance = new HostTime();
}
return instance;
}
void HostTime::onTimer(){
HostTime* hostTime = HostTime::getInstance();
hostTime->timerCounter++;
hostTime->currentTime++;
// xSemaphoreGiveFromISR(hostTime->timerUpdateEvent, NULL);
// vTaskDelay(100);
// xSemaphoreTakeFromISR(hostTime->timerUpdateEvent, NULL);
}
HostTime::HostTime()
: timeJsonDoc(512)
{
this->timerCounter = 0;
// this->timerUpdateEvent = xSemaphoreCreateMutex();
// xSemaphoreTakeFromISR(this->timerUpdateEvent, NULL);
this->hwTimer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
timerAttachInterrupt(hwTimer, &this->onTimer, true); // edge = true
timerAlarmWrite(hwTimer, 1000000, true); //1000 ms
timerAlarmEnable(hwTimer);
this->lastGetMillis = this->timerCounter;
}
uint32_t HostTime::getLastUpdateMillis()
{
HostTime* hostTime = HostTime::getInstance();
return hostTime->lastGetMillis;
}
uint32_t HostTime::getMillis()
{
HostTime* hostTime = HostTime::getInstance();
return hostTime->timerCounter;
}
uint32_t HostTime::getCurrentTime()
{
HostTime* hostTime = HostTime::getInstance();
return hostTime->currentTime;
}
uint32_t HostTime::syncCurrentTime()
{
HostTime* hostTime = HostTime::getInstance();
if(!hostTime->client.connected())
{
//Serial.println("HostTime sync connecting to worldtimeapi.org");
if (!hostTime->client.connect("worldtimeapi.org", 80))
{
Serial.println("Hostime sync connect failed to worldtimeapi.org");
}
else{
unsigned long startSendMillis = hostTime->timerCounter;
bool dataReceived = false;
String body;
hostTime->httpClient.begin(hostTime->client, "http://worldtimeapi.org/api/ip");
int httpCode = hostTime->httpClient.GET();
if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
body = hostTime->httpClient.getString();
dataReceived = true;
}
}
if(dataReceived)
{
int startIndex = body.indexOf('{');
int endIndex = body.lastIndexOf('}');
String data = body.substring(startIndex, endIndex + 1);
//Serial.print("timesync parsing data: ");
//Serial.println(data);
hostTime->timeJsonDoc.clear();
DeserializationError parseResult = deserializeJson(hostTime->timeJsonDoc, data);
if (parseResult != DeserializationError::Ok)
{
Serial.println("! parse json doc failed");
Serial.println(parseResult.c_str());
}
else
{
JsonObject root = hostTime->timeJsonDoc.as<JsonObject>();
JsonVariant unixTime = root["unixtime"];
JsonVariant unixTimeOffset = root["raw_offset"];
if (!unixTime.isNull())
{
hostTime->currentTime = unixTime.as<uint32_t>() + unixTimeOffset.as<uint32_t>();
//Serial.print("sync time = ");
//Serial.println(hostTime->currentTime);
}
else
{
Serial.println("! parse json root failed");
}
}
}
else{
Serial.println("Hostime sync receive failed from worldtimeapi.org");
}
}
//Serial.println("HostTime sync done");
hostTime->client.stop();
}
return hostTime->currentTime;
}