Skip to content

Commit 747c67a

Browse files
authored
Merge pull request #1 from davirxavier/feature/esp8266-compat
Feature/esp8266 compat
2 parents 323836c + 87d1619 commit 747c67a

File tree

11 files changed

+370
-153
lines changed

11 files changed

+370
-153
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.venv

.idea/EmberIot.iml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EmberIot.h

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <EmberIotUtil.h>
1010
#include <WithSecureClient.h>
1111
#include <EmberIotAuth.h>
12+
#include <EmberIotShared.h>
1213
#include <EmberIotStream.h>
1314
#include <time.h>
1415

@@ -39,6 +40,7 @@ class EmberIot : WithSecureClient
3940
{
4041
stream = nullptr;
4142
inited = false;
43+
isPaused = false;
4244
path = nullptr;
4345
auth = nullptr;
4446
lastUpdatedChannels = 0;
@@ -73,7 +75,11 @@ class EmberIot : WithSecureClient
7375
stream->start();
7476
inited = true;
7577
EmberIotChannels::started = true;
78+
isPaused = false;
79+
80+
#ifdef ESP32
7681
configTime(0, 0, "pool.ntp.org");
82+
#endif
7783
}
7884

7985
/**
@@ -86,13 +92,54 @@ class EmberIot : WithSecureClient
8692
return;
8793
}
8894

89-
auth->loop();
95+
if (auth != nullptr && !auth->ready())
96+
{
97+
auth->loop();
98+
return;
99+
}
100+
101+
if (auth != nullptr && auth->isExpired())
102+
{
103+
#ifdef ESP32
104+
auth->loop();
105+
#elif ESP8266
106+
pause();
107+
auth->loop();
108+
resume();
109+
#endif
110+
return;
111+
}
112+
else if (auth != nullptr)
113+
{
114+
auth->loop();
115+
}
116+
117+
if (!stream->isConnected())
118+
{
119+
EmberIotChannels::reconnectedFlag = true;
120+
}
121+
90122
stream->loop();
91123

92124
if (enableHeartbeat && millis() - lastHeartbeat > UPDATE_LAST_SEEN_INTERVAL)
93125
{
94-
writeLastSeen();
95-
lastHeartbeat = millis();
126+
#ifdef ESP32
127+
bool written = writeLastSeen();
128+
#elif ESP8266
129+
pause();
130+
bool written = writeLastSeen();
131+
resume();
132+
#endif
133+
134+
if (written)
135+
{
136+
lastHeartbeat = millis();
137+
}
138+
else
139+
{
140+
HTTP_LOGN("Write heartbeat failed, trying again in one second.");
141+
lastHeartbeat = millis() - UPDATE_LAST_SEEN_INTERVAL + 2000;
142+
}
96143
}
97144

98145
if (millis() - lastUpdatedChannels < 500)
@@ -130,7 +177,13 @@ class EmberIot : WithSecureClient
130177
char body[bodySize];
131178
serializeJson(doc, body, bodySize);
132179

180+
#ifdef ESP32
133181
bool result = write(body);
182+
#elif ESP8266
183+
pause();
184+
bool result = write(body);
185+
resume();
186+
#endif
134187
if (result)
135188
{
136189
for (bool& i : hasUpdateByChannel)
@@ -192,6 +245,20 @@ class EmberIot : WithSecureClient
192245
sprintf(updateDataByChannel[channel], "%lld", value);
193246
}
194247

248+
void pause()
249+
{
250+
isPaused = true;
251+
stream->stop();
252+
delay(100);
253+
}
254+
255+
void resume()
256+
{
257+
delay(100);
258+
isPaused = false;
259+
stream->start();
260+
}
261+
195262
/**
196263
* Enable or disable the heartbeat packet. If disabled the device will always appear as offline in the android app.
197264
* You can disable this to reduce transfers in the databse, if you want (you should probably disable this for board-to-board
@@ -202,9 +269,14 @@ class EmberIot : WithSecureClient
202269
private:
203270
bool write(const char* data)
204271
{
205-
size_t bufSize = strlen(EmberIotStreamValues::PROTOCOL) +
272+
if (auth != nullptr && auth->getUserUid() == nullptr)
273+
{
274+
return false;
275+
}
276+
277+
size_t bufSize = EmberIotStreamValues::PROTOCOL_SIZE +
206278
strlen(dbUrl) +
207-
strlen(EmberIotStreamValues::AUTH_PARAM) +
279+
EmberIotStreamValues::AUTH_PARAM_SIZE +
208280
strlen(stream->getPath()) +
209281
(auth != nullptr ? auth->getTokenSize() : 0) + 8;
210282

@@ -217,7 +289,7 @@ class EmberIot : WithSecureClient
217289
}
218290

219291
char buf[bufSize];
220-
sprintf(buf, "%s%s%s.json%s%s&print=silent",
292+
sprintf_P(buf, PSTR("%S%s%s.json%S%s&print=silent"),
221293
EmberIotStreamValues::PROTOCOL,
222294
dbUrl,
223295
finalPath,
@@ -229,15 +301,20 @@ class EmberIot : WithSecureClient
229301
return HTTP_UTIL::doJsonHttpRequest(client,
230302
buf,
231303
dbUrl,
232-
HTTP_UTIL::METHOD_PATCH,
304+
FPSTR(HTTP_UTIL::METHOD_PATCH),
233305
data);
234306
}
235307

236308
bool writeLastSeen()
237309
{
238-
size_t bufSize = strlen(EmberIotStreamValues::PROTOCOL) +
310+
if (auth != nullptr && auth->getUserUid() == nullptr)
311+
{
312+
return false;
313+
}
314+
315+
size_t bufSize = EmberIotStreamValues::PROTOCOL_SIZE +
239316
strlen(dbUrl) +
240-
strlen(EmberIotStreamValues::AUTH_PARAM) +
317+
EmberIotStreamValues::AUTH_PARAM_SIZE +
241318
strlen(stream->getPath()) +
242319
(auth != nullptr ? auth->getTokenSize() : 0) + 8;
243320

@@ -258,7 +335,7 @@ class EmberIot : WithSecureClient
258335
}
259336

260337
char buf[bufSize];
261-
sprintf(buf, "%s%s%s.json%s%s&print=silent",
338+
sprintf_P(buf, PSTR("%S%s%s.json%S%s&print=silent"),
262339
EmberIotStreamValues::PROTOCOL,
263340
dbUrl,
264341
finalPath,
@@ -270,15 +347,20 @@ class EmberIot : WithSecureClient
270347
getLocalTime(&timeinfo);
271348
time(&now);
272349

350+
#ifdef ESP32
273351
char bodyBuf[snprintf(NULL, 0, "%ld", now) + 16];
274352
sprintf(bodyBuf, "{\"%s\":%ld}", EmberIotStreamValues::LAST_SEEN_PATH, now);
353+
#elif ESP8266
354+
char bodyBuf[snprintf(NULL, 0, "%lld", now) + 16];
355+
sprintf_P(bodyBuf, PSTR("{\"%S\":%lld}"), EmberIotStreamValues::LAST_SEEN_PATH, now);
356+
#endif
275357

276358
HTTP_LOGF("Setting last_seen in path: %s, body:\n%s\n", buf, bodyBuf);
277359

278360
return HTTP_UTIL::doJsonHttpRequest(client,
279361
buf,
280362
dbUrl,
281-
HTTP_UTIL::METHOD_PATCH,
363+
FPSTR(HTTP_UTIL::METHOD_PATCH),
282364
bodyBuf);
283365
}
284366

@@ -287,6 +369,7 @@ class EmberIot : WithSecureClient
287369
char* path;
288370
EmberIotStream* stream;
289371
EmberIotAuth* auth;
372+
bool isPaused;
290373

291374
unsigned long lastUpdatedChannels;
292375
unsigned long lastHeartbeat;

0 commit comments

Comments
 (0)