Topuino_Hardware/src/OnlineConnector.cpp

54 lines
1.3 KiB
C++
Raw Normal View History

2022-07-02 13:04:34 +08:00
#include "OnlineConnector.h"
#include "StatusLed.h"
#include "UserData.h"
#include "FuncButton.h"
2022-07-02 13:38:24 +08:00
#include "FactoryInfo.h"
2021-08-29 16:24:40 +08:00
2022-07-02 13:04:34 +08:00
extern StatusLed* statusLed;
2021-08-29 22:14:08 +08:00
extern UserData* userdataManager;
extern FuncButton* funcButton;
2021-08-29 17:00:30 +08:00
2022-07-02 13:04:34 +08:00
OnlineConnector::OnlineConnector()
2021-08-29 22:14:08 +08:00
{
WiFi.begin(userdataManager->GetWifiSsid(), userdataManager->GetWifiPasswd());
2021-08-29 16:24:40 +08:00
2022-07-02 13:04:34 +08:00
statusLed->SetBlinkRate(StatusLed::BlinkRate::Rate2Hz);
2021-08-29 16:24:40 +08:00
while (WiFi.status() != WL_CONNECTED)
{
2021-08-29 22:14:08 +08:00
delay(1000);
2021-08-29 16:24:40 +08:00
}
2022-07-02 13:04:34 +08:00
statusLed->SetBlinkRate(StatusLed::BlinkRate::RateAlwaysOff);
2022-03-27 12:40:12 +08:00
url = "http://iot.vvzero.com/topuino/getdata?SN=";
url += DEVICE_SN;
2021-08-29 16:24:40 +08:00
status = FAIL;
}
2022-07-02 13:04:34 +08:00
STATUS OnlineConnector::FetchNewData()
2021-08-29 16:24:40 +08:00
{
status = FAIL;
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
http = new HTTPClient();
2021-08-29 22:14:08 +08:00
if (http->begin(client, url)) {
2021-08-29 16:24:40 +08:00
if (http->GET() == HTTP_CODE_OK) {
if (deserializeJson(receivedData, http->getString().c_str()) == DeserializationError::Code::Ok) {
status = OK;
}
}
http->end();
}
delete http;
}
return status;
}
2022-07-02 13:04:34 +08:00
uint8_t OnlineConnector::GetPercent(String name)
2021-08-29 16:24:40 +08:00
{
return status == OK ? receivedData[name] : 0;
}
2022-07-02 13:04:34 +08:00
uint32_t OnlineConnector::GetRate(String name)
2021-08-29 16:24:40 +08:00
{
return status == OK ? receivedData[name] : 0;
}