-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDynamicData.cpp
465 lines (406 loc) · 14.7 KB
/
DynamicData.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//
//
//
#define DBG_OUTPUT_PORT Serial
//#define DEBUG_DYNAMICDATA
#include "DynamicData.h"
#include "Config.h"
#include "FSWebServer.h"
#include <NtpClientLib.h>
#include <StreamString.h>
extern ntpClient* ntp;
#ifdef DEBUG_DYNAMICDATA
int wsNumber = 0;
#endif // DEBUG_DYNAMICDATA
const char Page_WaitAndReload[] PROGMEM = R"=====(
<meta http-equiv="refresh" content="10; URL=/config.html">
Please Wait....Configuring and Restarting.
)=====";
const char Page_Restart[] PROGMEM = R"=====(
<meta http-equiv="refresh" content="10; URL=/general.html">
Please Wait....Configuring and Restarting.
)=====";
void send_general_configuration_values_html()
{
String values = "";
values += "devicename|" + (String)config.DeviceName + "|input\n";
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
//
// FILL THE PAGE WITH NETWORKSTATE & NETWORKS
//
void send_connection_state_values_html()
{
String state = "N/A";
String Networks = "";
if (WiFi.status() == 0) state = "Idle";
else if (WiFi.status() == 1) state = "NO SSID AVAILBLE";
else if (WiFi.status() == 2) state = "SCAN COMPLETED";
else if (WiFi.status() == 3) state = "CONNECTED";
else if (WiFi.status() == 4) state = "CONNECT FAILED";
else if (WiFi.status() == 5) state = "CONNECTION LOST";
else if (WiFi.status() == 6) state = "DISCONNECTED";
int n = WiFi.scanNetworks();
if (n == 0)
{
Networks = "<font color='#FF0000'>No networks found!</font>";
}
else
{
Networks = "Found " + String(n) + " Networks<br>";
Networks += "<table border='0' cellspacing='0' cellpadding='3'>";
Networks += "<tr bgcolor='#DDDDDD' ><td><strong>Name</strong></td><td><strong>Quality</strong></td><td><strong>Enc</strong></td><tr>";
for (int i = 0; i < n; ++i)
{
int quality = 0;
if (WiFi.RSSI(i) <= -100)
{
quality = 0;
}
else if (WiFi.RSSI(i) >= -50)
{
quality = 100;
}
else
{
quality = 2 * (WiFi.RSSI(i) + 100);
}
Networks += "<tr><td><a href='javascript:selssid(\"" + String(WiFi.SSID(i)) + "\")'>" + String(WiFi.SSID(i)) + "</a></td><td>" + String(quality) + "%</td><td>" + String((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*") + "</td></tr>";
}
Networks += "</table>";
}
String values = "";
values += "connectionstate|" + state + "|div\n";
values += "networks|" + Networks + "|div\n";
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
//
// FILL THE PAGE WITH VALUES
//
void send_network_configuration_values_html()
{
String values = "";
values += "ssid|" + (String)config.ssid + "|input\n";
values += "password|" + (String)config.password + "|input\n";
values += "ip_0|" + (String)config.IP[0] + "|input\n";
values += "ip_1|" + (String)config.IP[1] + "|input\n";
values += "ip_2|" + (String)config.IP[2] + "|input\n";
values += "ip_3|" + (String)config.IP[3] + "|input\n";
values += "nm_0|" + (String)config.Netmask[0] + "|input\n";
values += "nm_1|" + (String)config.Netmask[1] + "|input\n";
values += "nm_2|" + (String)config.Netmask[2] + "|input\n";
values += "nm_3|" + (String)config.Netmask[3] + "|input\n";
values += "gw_0|" + (String)config.Gateway[0] + "|input\n";
values += "gw_1|" + (String)config.Gateway[1] + "|input\n";
values += "gw_2|" + (String)config.Gateway[2] + "|input\n";
values += "gw_3|" + (String)config.Gateway[3] + "|input\n";
values += "dns_0|" + (String)config.DNS[0] + "|input\n";
values += "dns_1|" + (String)config.DNS[1] + "|input\n";
values += "dns_2|" + (String)config.DNS[2] + "|input\n";
values += "dns_3|" + (String)config.DNS[3] + "|input\n";
values += "dhcp|" + (String)(config.dhcp ? "checked" : "") + "|chk\n";
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
//
// FILL WITH INFOMATION
//
void send_information_values_html()
{
String values = "";
values += "x_ssid|" + (String)WiFi.SSID() + "|div\n";
values += "x_ip|" + (String)WiFi.localIP()[0] + "." + (String)WiFi.localIP()[1] + "." + (String)WiFi.localIP()[2] + "." + (String)WiFi.localIP()[3] + "|div\n";
values += "x_gateway|" + (String)WiFi.gatewayIP()[0] + "." + (String)WiFi.gatewayIP()[1] + "." + (String)WiFi.gatewayIP()[2] + "." + (String)WiFi.gatewayIP()[3] + "|div\n";
values += "x_netmask|" + (String)WiFi.subnetMask()[0] + "." + (String)WiFi.subnetMask()[1] + "." + (String)WiFi.subnetMask()[2] + "." + (String)WiFi.subnetMask()[3] + "|div\n";
values += "x_mac|" + GetMacAddress() + "|div\n";
values += "x_dns|" + (String)WiFi.dnsIP()[0] + "." + (String)WiFi.dnsIP()[1] + "." + (String)WiFi.dnsIP()[2] + "." + (String)WiFi.dnsIP()[3] + "|div\n";
values += "x_ntp_sync|" + ntp->getTimeString(ntp->getLastNTPSync()) + "|div\n";
values += "x_ntp_time|" + ntp->getTimeStr() + "|div\n";
values += "x_ntp_date|" + ntp->getDateStr() + "|div\n";
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
String GetMacAddress()
{
uint8_t mac[6];
char macStr[18] = { 0 };
WiFi.macAddress(mac);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
void send_NTP_configuration_values_html()
{
String values = "";
values += "ntpserver|" + (String)config.ntpServerName + "|input\n";
values += "update|" + (String)config.Update_Time_Via_NTP_Every + "|input\n";
values += "tz|" + (String)config.timezone + "|input\n";
values += "dst|" + (String)(config.daylight ? "checked" : "") + "|chk\n";
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void send_network_configuration_html()
{
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
if (server.args() > 0) // Save Settings
{
//String temp = "";
bool oldDHCP = config.dhcp; // Save status to avoid general.html cleares it
config.dhcp = false;
for (uint8_t i = 0; i < server.args(); i++) {
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("Arg %d: %s\n", i, server.arg(i).c_str());
#endif // DEBUG_DYNAMICDATA
if (server.argName(i) == "devicename") {
config.DeviceName = urldecode(server.arg(i));
config.dhcp = oldDHCP;
continue; }
if (server.argName(i) == "ssid") { config.ssid = urldecode(server.arg(i)); continue; }
if (server.argName(i) == "password") { config.password = urldecode(server.arg(i)); continue; }
if (server.argName(i) == "ip_0") { if (checkRange(server.arg(i))) config.IP[0] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "ip_1") { if (checkRange(server.arg(i))) config.IP[1] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "ip_2") { if (checkRange(server.arg(i))) config.IP[2] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "ip_3") { if (checkRange(server.arg(i))) config.IP[3] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "nm_0") { if (checkRange(server.arg(i))) config.Netmask[0] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "nm_1") { if (checkRange(server.arg(i))) config.Netmask[1] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "nm_2") { if (checkRange(server.arg(i))) config.Netmask[2] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "nm_3") { if (checkRange(server.arg(i))) config.Netmask[3] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "gw_0") { if (checkRange(server.arg(i))) config.Gateway[0] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "gw_1") { if (checkRange(server.arg(i))) config.Gateway[1] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "gw_2") { if (checkRange(server.arg(i))) config.Gateway[2] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "gw_3") { if (checkRange(server.arg(i))) config.Gateway[3] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "dns_0") { if (checkRange(server.arg(i))) config.DNS[0] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "dns_1") { if (checkRange(server.arg(i))) config.DNS[1] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "dns_2") { if (checkRange(server.arg(i))) config.DNS[2] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "dns_3") { if (checkRange(server.arg(i))) config.DNS[3] = server.arg(i).toInt(); continue; }
if (server.argName(i) == "dhcp") { config.dhcp = true; continue; }
}
server.send(200, "text/html", Page_WaitAndReload);
save_config();
yield();
delay(1000);
ESP.restart();
//ConfigureWifi();
//AdminTimeOutCounter = 0;
}
else
{
DBG_OUTPUT_PORT.println(server.uri());
handleFileRead(server.uri());
}
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void send_general_configuration_html()
{
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
if (server.args() > 0) // Save Settings
{
for (uint8_t i = 0; i < server.args(); i++) {
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("Arg %d: %s\n", i, server.arg(i).c_str());
#endif // DEBUG_DYNAMICDATA
if (server.argName(i) == "devicename") {
config.DeviceName = urldecode(server.arg(i));
continue;
}
}
server.send(200, "text/html", Page_Restart);
save_config();
ESP.restart();
//ConfigureWifi();
//AdminTimeOutCounter = 0;
}
else
{
handleFileRead(server.uri());
}
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void send_NTP_configuration_html()
{
if (server.args() > 0) // Save Settings
{
config.daylight = false;
//String temp = "";
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "ntpserver") {
config.ntpServerName = urldecode(server.arg(i));
ntp->setNtpServerName(config.ntpServerName);
continue;
}
if (server.argName(i) == "update") {
config.Update_Time_Via_NTP_Every = server.arg(i).toInt();
ntp->setInterval(config.Update_Time_Via_NTP_Every * 60);
continue;
}
if (server.argName(i) == "tz") {
config.timezone = server.arg(i).toInt();
ntp->setTimeZone(config.timezone / 10);
continue;
}
if (server.argName(i) == "dst") {
config.daylight = true;
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("Daylight Saving: %d\n", config.daylight);
#endif // DEBUG_DYNAMICDATA
continue;
}
}
ntp->setDayLight(config.daylight);
save_config();
//firstStart = true;
setTime(ntp->getTime()); //set time
}
handleFileRead("/ntp.html");
//server.send(200, "text/html", PAGE_NTPConfiguration);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void restart_esp() {
//server.send(200, "text/html", Page_Restart);
delay(1000);
ESP.reset();
}
void send_wwwauth_configuration_values_html() {
String values = "";
values += "wwwauth|" + (String)(httpAuth.auth ? "checked" : "") + "|chk\n";
values += "wwwuser|" + (String)httpAuth.wwwUsername + "|input\n";
values += "wwwpass|" + (String)httpAuth.wwwPassword + "|input\n";
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void send_wwwauth_configuration_html()
{
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("%s %d\n",__FUNCTION__, server.args());
#endif // DEBUG_DYNAMICDATA
if (server.args() > 0) // Save Settings
{
httpAuth.auth = false;
//String temp = "";
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "wwwuser") {
httpAuth.wwwUsername = urldecode(server.arg(i));
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("User: %s\n", httpAuth.wwwUsername.c_str());
#endif // DEBUG_DYNAMICDATA
continue;
}
if (server.argName(i) == "wwwpass") {
httpAuth.wwwPassword = urldecode(server.arg(i));
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("Pass: %s\n", httpAuth.wwwPassword.c_str());
#endif // DEBUG_DYNAMICDATA
continue;
}
if (server.argName(i) == "wwwauth") {
httpAuth.auth = true;
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("HTTP Auth enabled\n");
#endif // DEBUG_DYNAMICDATA
continue;
}
}
saveHTTPAuth();
}
handleFileRead("/system.html");
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void send_update_firmware_values_html() {
String values = "";
uint32_t maxSketchSpace = (ESP.getSketchSize() - 0x1000) & 0xFFFFF000;
//bool updateOK = Update.begin(maxSketchSpace);
bool updateOK = maxSketchSpace < ESP.getFreeSketchSpace();
StreamString result;
Update.printError(result);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("--MaxSketchSpace: %d\n", maxSketchSpace);
DBG_OUTPUT_PORT.printf("--Update error = %s\n", result.c_str());
#endif // DEBUG_DYNAMICDATA
values += "remupd|" + (String)((updateOK) ? "OK" : "ERROR") + "|div\n";
if (Update.hasError()) {
result.trim();
values += "remupdResult|" + result + "|div\n";
}
else {
values += "remupdResult||div\n";
}
server.send(200, "text/plain", values);
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.println(__FUNCTION__);
#endif // DEBUG_DYNAMICDATA
}
void sendTimeData() {
for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
#ifdef DEBUG_DYNAMICDATA
//DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_DYNAMICDATA
String time = "T" + ntp->getTimeStr();
wsServer.sendTXT(i, time);
String date = "D" + ntp->getDateStr();
wsServer.sendTXT(i, date);
String sync = "S" + ntp->getTimeString(ntp->getLastNTPSync());
wsServer.sendTXT(i, sync);
}
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch (type) {
case WStype_DISCONNECTED:
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("[%u] Disconnected!\n", num);
#endif // DEBUG_DYNAMICDATA
break;
case WStype_CONNECTED:
{
#ifdef DEBUG_DYNAMICDATA
wsNumber = num;
IPAddress ip = wsServer.remoteIP(num);
DBG_OUTPUT_PORT.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
#endif // DEBUG_DYNAMICDATA
// send message to client
//wsServer.sendTXT(num, "Connected");
}
break;
case WStype_TEXT:
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("[%u] get Text: %s\n", num, payload);
#endif // DEBUG_DYNAMICDATA
// send message to client
// webSocket.sendTXT(num, "message here");
// send data to all connected clients
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
#ifdef DEBUG_DYNAMICDATA
DBG_OUTPUT_PORT.printf("[%u] get binary lenght: %u\n", num, lenght);
#endif // DEBUG_DYNAMICDATA
hexdump(payload, lenght);
// send message to client
// webSocket.sendBIN(num, payload, lenght);
break;
}
}