-
Notifications
You must be signed in to change notification settings - Fork 3
/
HttpsDataSource.cpp
72 lines (62 loc) · 2.04 KB
/
HttpsDataSource.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
#include "HttpsDataSource.h"
#include "HostTime.h"
HttpsDataSource::HttpsDataSource(const std::string cur1, const std::string cur2)
: DataSource(cur1,cur2), lastValueUpdate(0)
{
}
bool HttpsDataSource::update()
{
if(this->wifiClient.connected()){
return false;
}
else{
this->wifiClient.setTimeout(5000);
if (!this->wifiClient.connect(this->getHostName().c_str(), 443))
{
Serial.printf("! %s connect failed to %s\n", this->getName().c_str(), this->httpGetUrl().c_str());
}
else{
unsigned long startSendMillis = millis();
bool dataReceived = false;
this->httpClient.begin(this->wifiClient, this->httpGetUrl().c_str());
int httpCode = this->httpClient.GET();
if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
this->lastData = std::string(this->httpClient.getString().c_str());
this->lastValueUpdate = HostTime::getCurrentTime();
dataReceived = true;
}
}
if (dataReceived)
{
DataParser* parser = this->getParser();
if(parser != nullptr)
{
float newValue = this->lastValue;
if( parser->parseData(this->lastData, newValue) )
{
this->lastValue = newValue;
Serial.printf("%s updated = %f\n", this->getName().c_str(), this->lastValue);
}
}
}
else{
Serial.printf("! %s HTTP GET failed to %s\n", this->getName().c_str(), this->httpGetUrl().c_str());
}
this->wifiClient.stop();
return dataReceived;
}
}
}
std::string HttpsDataSource::getLastData() const
{
return this->lastData;
}
float HttpsDataSource::getLastValue() const
{
return this->lastValue;
}
uint32_t HttpsDataSource::getLastDataUpdateTime() const
{
return this->lastValueUpdate;
}