1+ #include " ZmartChargeCloudConnector.h"
2+ #include " Uptime.h"
3+ #include " ArduinoJson.h"
4+
5+ void ZmartChargeCloudConnector::setup (const char * baseUrl, const char * token) {
6+ memset (this ->baseUrl , 0 , 64 );
7+ memset (this ->token , 0 , 21 );
8+ strcpy (this ->baseUrl , baseUrl);
9+ strcpy (this ->token , token);
10+ }
11+
12+ bool ZmartChargeCloudConnector::isConfigChanged () {
13+ return configChanged;
14+ }
15+
16+ void ZmartChargeCloudConnector::ackConfigChanged () {
17+ configChanged = false ;
18+ }
19+
20+ const char * ZmartChargeCloudConnector::getBaseUrl () {
21+ return baseUrl;
22+ }
23+
24+
25+ void ZmartChargeCloudConnector::update (AmsData& data) {
26+ if (strlen (token) == 0 ) return ;
27+
28+ uint64_t now = millis64 ();
29+
30+ float maximum = max (max (data.getL1Current (), data.getL1Current ()), data.getL3Current ());
31+ bool fast = maximum > heartbeatFastThreshold;
32+
33+ if (now - lastUpdate < (fast ? heartbeatFast : heartbeat) * 1000 ) return ;
34+
35+ if (strlen (token) != 20 ) {
36+ lastUpdate = now;
37+ if (debugger->isActive (RemoteDebug::WARNING)) debugger->printf_P (PSTR (" (ZmartCharge) Token defined, but is incorrect length (%s, %d)\n " ), token, strlen (token));
38+ return ;
39+ }
40+
41+ if (((now - lastUpdate) / 1000 ) > (fast || lastFailed ? heartbeatFast : heartbeat)) {
42+ if (debugger->isActive (RemoteDebug::DEBUG)) debugger->printf_P (PSTR (" (ZmartCharge) Preparing to update cloud\n " ));
43+ memset (json, 0 , BufferSize);
44+ snprintf_P (json, BufferSize, ZC_LB_JSON,
45+ token,
46+ data.getL1Current (),
47+ data.getL2Current (),
48+ data.getL3Current (),
49+ fast ? 1 : 0 ,
50+ data.getActiveImportPower ()
51+ );
52+ lastFailed = true ;
53+ char url[128 ];
54+ memset (url, 0 , 128 );
55+ snprintf_P (url, 128 , PSTR (" %s/loadbalancer" ), baseUrl);
56+ if (debugger->isActive (RemoteDebug::VERBOSE)) debugger->printf_P (PSTR (" (ZmartCharge) Connecting to: %s\n " ), baseUrl);
57+ if (http->begin (url)) {
58+ if (debugger->isActive (RemoteDebug::VERBOSE)) debugger->printf_P (PSTR (" (ZmartCharge) Sending data: %s\n " ), json);
59+ int status = http->POST (json);
60+ if (status == 200 ) {
61+ lastFailed = false ;
62+ JsonDocument doc;
63+ String body = http->getString ();
64+ if (debugger->isActive (RemoteDebug::VERBOSE)) debugger->printf_P (PSTR (" (ZmartCharge) Received data: %s\n " ), body.c_str ());
65+ deserializeJson (doc, body);
66+ if (doc.containsKey (" Settings" )) {
67+ if (doc[" Settings" ].containsKey (" HeartBeatTime" )) {
68+ heartbeat = doc[" Settings" ][" HeartBeatTime" ].as <long >();
69+ }
70+ if (doc[" Settings" ].containsKey (" HearBeatTimeFast" )) {
71+ heartbeatFast = doc[" Settings" ][" HearBeatTimeFast" ].as <long >();
72+ }
73+ if (doc[" Settings" ].containsKey (" HeartBeatTimeFastThreshold" )) {
74+ heartbeatFastThreshold = doc[" Settings" ][" HeartBeatTimeFastThreshold" ].as <long >();
75+ }
76+ if (doc[" Settings" ].containsKey (" ZmartChargeUrl" )) {
77+ String newBaseUrl = doc[" Settings" ][" ZmartChargeUrl" ].as <String>();
78+ if (newBaseUrl.startsWith (" https:" ) && strncmp (newBaseUrl.c_str (), baseUrl, strlen (baseUrl)) != 0 ) {
79+ newBaseUrl.replace (" \\ /" , " /" );
80+ if (debugger->isActive (RemoteDebug::INFO)) debugger->printf_P (PSTR (" (ZmartCharge) Received new URL: %s\n " ), newBaseUrl.c_str ());
81+ memset (baseUrl, 0 , 64 );
82+ memcpy (baseUrl, newBaseUrl.c_str (), strlen (newBaseUrl.c_str ()));
83+ configChanged = true ;
84+ }
85+ }
86+ }
87+ http->end ();
88+ } else {
89+ if (debugger->isActive (RemoteDebug::ERROR)) debugger->printf_P (PSTR (" (ZmartCharge) Communication error, returned status: %d\n " ), status);
90+ if (debugger->isActive (RemoteDebug::ERROR)) debugger->printf (http->errorToString (status).c_str ());
91+ if (debugger->isActive (RemoteDebug::DEBUG)) debugger->printf (http->getString ().c_str ());
92+
93+ http->end ();
94+ }
95+ } else {
96+ if (debugger->isActive (RemoteDebug::ERROR)) debugger->printf_P (PSTR (" (ZmartCharge) Unable to establish connection with cloud\n " ));
97+ }
98+ lastUpdate = now;
99+ }
100+ }
0 commit comments