forked from WLANThermo-nano/WLANThermo_ESP82XX_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_webhandler.h
847 lines (695 loc) · 30.3 KB
/
c_webhandler.h
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/***************************************************
Copyright (C) 2016 Steffen Ochs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Inspired and partly taken over by Vitotai
https://github.com/vitotai/BrewPiLess
HISTORY: Please refer Github History
****************************************************/
// Eventuell andere Lösung zum Auslesen des Body-Inhalts
// https://github.com/me-no-dev/ESPAsyncWebServer/issues/123
// https://github.com/me-no-dev/ESPAsyncWebServer#request-variables
#define FLIST_PATH "/list"
#define DELETE_PATH "/rm"
#define FPUTS_PATH "/fputs"
#define DATA_PATH "/data"
#define SETTING_PATH "/settings"
#define UPDATE_PATH "/update"
#define BAD_PATH "BAD PATH"
#define DEFAULT_INDEX_FILE "index.html"
#define LOGLIST_PATH "/loglist.php"
#define CHART_DATA_PATH "/chart.php"
#define NETWORK_SCAN "/networkscan"
#define NETWORK_LIST "/networklist"
#define NETWORK_STOP "/stopwifi"
#define NETWORK_CLEAR "/clearwifi"
#define CONFIG_RESET "/configreset"
#define SET_NETWORK "/setnetwork"
#define SET_SYSTEM "/setsystem"
#define SET_CHANNELS "/setchannels"
#define SET_PITMASTER "/setpitmaster"
#define SET_PID "/setpid"
#define SET_DC "/setDC"
#define SET_IOT "/setIoT"
#define UPDATE_CHECK "/checkupdate"
#define UPDATE_STATUS "/updatestatus"
#define DC_STATUS "/dcstatus"
#define APPLICATIONJSON "application/json"
#define TEXTPLAIN "text/plain"
#define TEXTON "aktiviert"
#define TEXTOFF "deaktiviert"
#define TEXTTRUE "true"
#define TEXTFALSE "false"
#define TEXTADD "Add"
const char *public_list[]={
"/nano.ttf"
};
// ---------------------------------------------------------------
// WEBHANDLER
class NanoWebHandler: public AsyncWebHandler {
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void handleSettings(AsyncWebServerRequest *request) {
String jsonStr;
jsonStr = cloudSettings();
request->send(200, APPLICATIONJSON, jsonStr);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void handleData(AsyncWebServerRequest *request) {
String jsonStr;
jsonStr = cloudData(false);
request->send(200, APPLICATIONJSON, jsonStr);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void handleWifiResult(AsyncWebServerRequest *request, bool www) {
// https://github.com/me-no-dev/ESPAsyncWebServer/issues/85
AsyncJsonResponse * response = new AsyncJsonResponse();
response->addHeader("Server","ESP Async Web Server");
JsonObject& json = response->getRoot();
int n = WiFi.scanComplete();
if (n > 0) {
if (WiFi.status() == WL_CONNECTED) {
json["Connect"] = true;
json["Scantime"] = millis()-wifi.scantime;
json["SSID"] = WiFi.SSID();
json["IP"] = WiFi.localIP().toString();
json["Mask"] = WiFi.subnetMask().toString();
json["Gate"] = WiFi.gatewayIP().toString();
}
else {
json["Connect"] = false;
json["SSID"] = APNAME;
json["IP"] = WiFi.softAPIP().toString();
}
JsonArray& _scan = json.createNestedArray("Scan");
for (int i = 0; i < n; i++) {
JsonObject& _wifi = _scan.createNestedObject();
_wifi["SSID"] = WiFi.SSID(i);
_wifi["RSSI"] = WiFi.RSSI(i);
_wifi["Enc"] = WiFi.encryptionType(i);
//_wifi["Hid"] = WiFi.isHidden(i);
if (WiFi.status() == WL_CONNECTED & WiFi.SSID(i) == WiFi.SSID()) {
json["Enc"] = WiFi.encryptionType(i);
json["RSSI"] = WiFi.RSSI(i);
}
}
}
if (www) {
response->setLength();
request->send(response);
} else json.printTo(Serial);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void handleWifiScan(AsyncWebServerRequest *request, bool www) {
//dumpClients();
// kein Scan zu Systemstart sonst keine Reconnection nach Systemstart
WiFi.scanDelete();
if (WiFi.scanComplete() == -2){
WiFi.scanNetworks(true); // true = scan async
wifi.scantime = millis();
if (www) request->send(200, TEXTPLAIN, "OK");
//else Serial.println("OK");
}
}
/*
// ---------------------
void handleFileList(AsyncWebServerRequest *request) {
Dir dir = SPIFFS.openDir("/");
String path = String();
String output = "[";
while(dir.next()){
File entry = dir.openFile("r");
if (output != "[") output += ',';
bool isDir = false;
output += "{\"type\":\"";
output += (isDir)?"dir":"file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
output += "\"}";
entry.close();
}
output += "]";
request->send(200, "text/json", output);
output = String();
}
// ---------------------
void handleFileDelete(AsyncWebServerRequest *request){
if (request->hasParam("path", true)){
ESP.wdtDisable(); SPIFFS.remove(request->getParam("path", true)->value()); ESP.wdtEnable(10);
request->send(200, "", "DELETE: "+request->getParam("path", true)->value());
} else request->send(404);
}
// ---------------------
void handleFilePuts(AsyncWebServerRequest *request){
if(request->hasParam("path", true) && request->hasParam("content", true)){
ESP.wdtDisable();
String file=request->getParam("path", true)->value();
File fh= SPIFFS.open(file, "w");
if(!fh){
request->send(500);
return;
}
String c=request->getParam("content", true)->value();
fh.print(c.c_str());
fh.close();
ESP.wdtEnable(10);
request->send(200);
DPRINTF("fputs path=%s\n",file.c_str());
//if(file == PROFILE_FILENAME){
//DBG_PRINTF("reload file\n");
//brewKeeper.reloadProfile();
//}
} else request->send(404);
}
*/
/*
// ---------------------
bool fileExists(String path) {
if(SPIFFS.exists(path)) return true;
bool dum;
unsigned int dum2;
if(getEmbeddedFile(path.c_str(),dum,dum2)) return true;
if(path.endsWith(CHART_LIB_PATH) && SPIFFS.exists(CHART_LIB_PATH)) return true;
return false;
}
// ---------------------
void sendProgmem(AsyncWebServerRequest *request,const char* html) {
AsyncWebServerResponse *response = request->beginResponse(String("text/html"),strlen_P(html),[=](uint8_t *buffer, size_t maxLen, size_t alreadySent) -> size_t {
if (strlen_P(html+alreadySent)>maxLen) {
memcpy_P((char*)buffer, html+alreadySent, maxLen);
return maxLen;
}
// Ok, last chunk
memcpy_P((char*)buffer, html+alreadySent, strlen_P(html+alreadySent));
return strlen_P(html+alreadySent); // Return from here to end of indexhtml
});
response->addHeader("Cache-Control","max-age=2592000");
request->send(response);
}
// ---------------------
void sendFile(AsyncWebServerRequest *request,String path) {
if(SPIFFS.exists(path)){
//request->send(SPIFFS, path);
bool nocache=false;
for(byte i=0;i< sizeof(nocache_list)/sizeof(const char*);i++){
if(path.equals(nocache_list[i])){
nocache=true;
break;
}
}
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, path);
if(nocache)
response->addHeader("Cache-Control","no-cache");
else
response->addHeader("Cache-Control","max-age=2592000");
request->send(response);
return;
}
//else
bool gzip;
uint32_t size;
const uint8_t* file=getEmbeddedFile(path.c_str(),gzip,size);
if(file){
DBG_PRINTF("using embedded file:%s\n",path.c_str());
if(gzip){
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", file, size);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
} else sendProgmem(request,(const char*)file);
}
}
*/
public:
NanoWebHandler(void){}
void handleWifiResult(byte www) {
AsyncWebServerRequest *request;
return handleWifiResult(request, www);
}
void handleWifiScan(byte www) {
AsyncWebServerRequest *request;
return handleWifiScan(request, www);
}
void configreset() {
set_channels(1);
setconfig(eCHANNEL,{});
loadconfig(eCHANNEL,0);
set_system();
setconfig(eSYSTEM,{});
loadconfig(eSYSTEM,0);
set_pitmaster(1);
set_pid(0);
setconfig(ePIT,{});
loadconfig(ePIT,0);
set_iot(1);
setconfig(eTHING,{});
loadconfig(eTHING,0);
}
// ---------------------
void handleRequest(AsyncWebServerRequest *request){
if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && request->url() == DATA_PATH){
handleData(request);
} else if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && request->url() == SETTING_PATH){
handleSettings(request);
} else if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && request->url() == NETWORK_SCAN){
handleWifiScan(request, true);
} else if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && request->url() == NETWORK_LIST){
handleWifiResult(request, true);
/*
} else if (request->method() == HTTP_POST && request->url() == FLIST_PATH){
if(!request->authenticate(www_username, www_password))
return request->requestAuthentication();
handleFileList(request);
} else if (request->method() == HTTP_DELETE && request->url() == DELETE_PATH){
if(!request->authenticate(www_username, www_password))
return request->requestAuthentication();
handleFileDelete(request);
} else if (request->method() == HTTP_POST && request->url() == FPUTS_PATH){
if(!request->authenticate(www_username, www_password))
return request->requestAuthentication();
handleFilePuts(request);
*/
// REQUEST: /stop wifi
} else if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && request->url() == NETWORK_STOP) {
wifi.mode = 4; // Turn Wifi off with timer
request->send(200, TEXTPLAIN, TEXTTRUE);
// REQUEST: /clear wifi
} else if (request->url() == NETWORK_CLEAR) {
if (request->method() == HTTP_GET) {
request->send(200, "text/html", "<form method='POST' action='/clearwifi'>Wifi-Speicher wirklich leeren?<br><br><input type='submit' value='Ja'></form>");
} else if (request->method() == HTTP_POST) {
setconfig(eWIFI,{}); // clear Wifi settings
sys.restartnow = true;
wifi.mode = 5;
request->send(200, TEXTPLAIN, TEXTTRUE);
} else request->send(500, TEXTPLAIN, BAD_PATH);
// REQUEST: /configreset
} else if (request->url() == CONFIG_RESET) {
if (request->method() == HTTP_GET) {
request->send(200, "text/html", "<form method='POST' action='/configreset'>System-Speicher wirklich resetten?<br><br><input type='submit' value='Ja'></form>");
} else if (request->method() == HTTP_POST) {
configreset();
request->send(200, TEXTPLAIN, TEXTTRUE);
} else request->send(500, TEXTPLAIN, BAD_PATH);
// REQUEST: /update
} else if (request->url() == UPDATE_PATH) {
if (request->method() == HTTP_GET) {
request->send(200, "text/html", "<form method='POST' action='/update'>Version mit v eingeben: <input type='text' name='version'><br><br><input type='submit' value='Update'></form>");
} else if (request->method() == HTTP_POST) {
if(!request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
if (request->hasParam("version", true)) {
ESP.wdtDisable();
// use getParam(xxx, true) for form-data parameters in POST request header
String version = request->getParam("version", true)->value();
if (version.indexOf("v") == 0) sys.getupdate = version;
else request->send(200, TEXTPLAIN, "Version unknown!");
}
sys.update = 1;
ESP.wdtEnable(10);
request->send(200, TEXTPLAIN, "Do Update...");
} else request->send(500, TEXTPLAIN, BAD_PATH);
// REQUEST: /checkupdate
} else if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && request->url() == UPDATE_CHECK) {
sys.update = -1;
request->send(200, TEXTPLAIN, TEXTTRUE);
// REQUEST: /updatestatus
} else if ((request->method() == HTTP_POST) && request->url() == UPDATE_STATUS) {
DPRINTLN("... in process");
if(sys.update > 0) request->send(200, TEXTPLAIN, TEXTTRUE);
request->send(200, TEXTPLAIN, TEXTFALSE);
// REQUEST: /dcstatus
} else if ((request->method() == HTTP_POST) && request->url() == DC_STATUS) {
if (pitMaster[0].active == DUTYCYCLE || pitMaster[1].active == DUTYCYCLE) request->send(200, TEXTPLAIN, TEXTTRUE);
else request->send(200, TEXTPLAIN, TEXTFALSE);
// REQUEST: File from SPIFFS
} else if (request->method() == HTTP_GET){
String path = request->url();
if (path.endsWith("/")) path += DEFAULT_INDEX_FILE;
//else if (path.endsWith(CHART_LIB_PATH)) path = CHART_LIB_PATH;
if (request->url().equals("/")){
//sendFile(request,path); //
request->send(SPIFFS, path);
return;
}
bool auth = true;
for(byte i=0;i< sizeof(public_list)/sizeof(const char*);i++){
if(path.equals(public_list[i])){
auth = false;
break;
}
}
if(auth && !request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
//sendFile(request,path); //
request->send(SPIFFS, path);
}
}
// ---------------------
bool canHandle(AsyncWebServerRequest *request){
if (request->method() == HTTP_GET){
if(request->url() == DATA_PATH || request->url() == SETTING_PATH
|| request->url() == NETWORK_LIST || request->url() == NETWORK_SCAN
|| request->url() == NETWORK_STOP || request->url() == NETWORK_CLEAR
|| request->url() == CONFIG_RESET || request->url() == UPDATE_PATH
|| request->url() == UPDATE_CHECK
//|| request->url() == LOGGING_PATH
){
return true;
} else {
// get file
String path = request->url();
if (path.endsWith("/")) path +=DEFAULT_INDEX_FILE;
//if(fileExists(path)) return true;
if(SPIFFS.exists(path)) return true;
}
} else if (request->method() == HTTP_DELETE && request->url() == DELETE_PATH){
return true;
} else if (request->method() == HTTP_POST){
if(request->url() == FPUTS_PATH || request->url() == FLIST_PATH
|| request->url() == DATA_PATH || request->url() == SETTING_PATH
|| request->url() == NETWORK_LIST || request->url() == NETWORK_SCAN
|| request->url() == NETWORK_STOP || request->url() == NETWORK_CLEAR
|| request->url() == CONFIG_RESET || request->url() == UPDATE_PATH
|| request->url() == UPDATE_CHECK || request->url() == UPDATE_STATUS
|| request->url() == DC_STATUS //|| request->url() == LOGGING_PATH
)
return true;
}
return false;
}
};
NanoWebHandler nanoWebHandler;
// ---------------------------------------------------------------
// WEBHANDLER
class BodyWebHandler: public AsyncWebHandler {
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int checkStringLength(String tex) {
int index = tex.length();
while (tex.lastIndexOf(195) != -1) {
tex.remove(tex.lastIndexOf(195),1);
index--;
}
return index;
}
String checkString(String tex) {
tex.replace("&","&"); // &
tex.replace("<","<"); // <
tex.replace(">",">"); // >
return tex;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool setSystem(AsyncWebServerRequest *request, uint8_t *datas) {
printRequest(datas);
DynamicJsonBuffer jsonBuffer;
JsonObject& _system = jsonBuffer.parseObject((const char*)datas); //https://github.com/esp8266/Arduino/issues/1321
if (!_system.success()) return 0;
String unit, _name;
if (_system.containsKey("language")) sys.language = _system["language"].asString();
if (_system.containsKey("unit")) unit = _system["unit"].asString();
if (_system.containsKey("autoupd")) sys.autoupdate = _system["autoupd"];
if (_system.containsKey("fastmode")) sys.fastmode = _system["fastmode"];
if (_system.containsKey("host")) {
_name = _system["host"].asString();
if (checkStringLength(_name) < 14) sys.host = _name;
}
if (_system.containsKey("ap")) {
_name = _system["ap"].asString();
if (checkStringLength(_name) < 14) sys.apname = _name;
}
if (_system.containsKey("hwversion")) {
_name = _system["hwversion"].asString();
_name.replace("V","");
sys.hwversion = _name.toInt();
}
setconfig(eSYSTEM,{}); // SPEICHERN
if (sys.unit != unit) {
sys.unit = unit;
transform_limits(); // Transform Limits
setconfig(eCHANNEL,{}); // Save Config
get_Temperature(); // Update Temperature
}
return 1;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool setChannels(AsyncWebServerRequest *request, uint8_t *datas) {
// https://github.com/me-no-dev/ESPAsyncWebServer/issues/123
printRequest(datas);
DynamicJsonBuffer jsonBuffer;
JsonObject& _cha = jsonBuffer.parseObject((const char*)datas); //https://github.com/esp8266/Arduino/issues/1321
if (!_cha.success()) return 0;
int num = _cha["number"];
if (num > 0) { // Feld vorhanden
num--; // Intern beginnt die Zählung bei 0
String _name;
if (_cha.containsKey("name")) {
_name = _cha["name"].asString(); // KANALNAME
if (checkStringLength(_name) < 11) ch[num].name = _name;
}
byte _typ;
if (_cha.containsKey("typ")) {
_typ = _cha["typ"]; // FÜHLERTYP
if (_typ > -1 && _typ < SENSORTYPEN) ch[num].typ = _typ;
}
float _limit;
if (_cha.containsKey("min")) {
_limit = _cha["min"]; // LIMIT
if (_limit > LIMITUNTERGRENZE && _limit < LIMITOBERGRENZE) ch[num].min = _limit;
}
if (_cha.containsKey("max")) {
_limit = _cha["max"]; // LIMIT
if (_limit > LIMITUNTERGRENZE && _limit < LIMITOBERGRENZE) ch[num].max = _limit;
}
if (_cha.containsKey("alarm")) ch[num].alarm = _cha["alarm"]; // ALARM
if (_cha.containsKey("color")) ch[num].color = _cha["color"].asString(); // COLOR
} else return 0;
setconfig(eCHANNEL,{}); // SPEICHERN
return 1;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool setNetwork(AsyncWebServerRequest *request, uint8_t *datas) {
printRequest(datas);
DynamicJsonBuffer jsonBuffer;
JsonObject& _network = jsonBuffer.parseObject((const char*)datas); //https://github.com/esp8266/Arduino/issues/1321
if (!_network.success()) return 0;
if (!_network.containsKey("ssid")) return 0;
holdssid.ssid = checkString(_network["ssid"].asString());
if (!_network.containsKey("password")) return 0;
holdssid.pass = _network["password"].asString();
holdssid.connect = millis();
holdssid.hold = 1;
return 1;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool setIoT(AsyncWebServerRequest *request, uint8_t *datas) {
printRequest(datas);
DynamicJsonBuffer jsonBuffer;
JsonObject& _chart = jsonBuffer.parseObject((const char*)datas); //https://github.com/esp8266/Arduino/issues/1321
if (!_chart.success()) return 0;
bool refresh = iot.CL_on;
if (_chart.containsKey("TSwrite")) iot.TS_writeKey = _chart["TSwrite"].asString();
if (_chart.containsKey("TShttp")) iot.TS_httpKey = _chart["TShttp"].asString();
if (_chart.containsKey("TSuser")) iot.TS_userKey = _chart["TSuser"].asString();
if (_chart.containsKey("TSchID")) iot.TS_chID = _chart["TSchID"].asString();
if (_chart.containsKey("TSshow8")) iot.TS_show8 = _chart["TSshow8"];
if (_chart.containsKey("TSint")) iot.TS_int = _chart["TSint"];
if (_chart.containsKey("TSon")) iot.TS_on = _chart["TSon"];
if (_chart.containsKey("PMQhost")) iot.P_MQTT_HOST = _chart["PMQhost"].asString();
if (_chart.containsKey("PMQport")) iot.P_MQTT_PORT = _chart["PMQport"];
if (_chart.containsKey("PMQuser")) iot.P_MQTT_USER = _chart["PMQuser"].asString();
if (_chart.containsKey("PMQpass")) iot.P_MQTT_PASS = _chart["PMQpass"].asString();
if (_chart.containsKey("PMQqos")) iot.P_MQTT_QoS = _chart["PMQqos"];
if (_chart.containsKey("PMQon")) iot.P_MQTT_on = _chart["PMQon"];
if (_chart.containsKey("PMQint")) iot.P_MQTT_int = _chart["PMQint"];
if (_chart.containsKey("TGon")) iot.TG_on = _chart["TGon"];
if (_chart.containsKey("TGtoken")) iot.TG_token = _chart["TGtoken"].asString();
if (_chart.containsKey("TGid")) iot.TG_id = _chart["TGid"].asString();
if (_chart.containsKey("CLon")) iot.CL_on = _chart["CLon"];
if (_chart.containsKey("CLtoken")) iot.CL_token = _chart["CLtoken"].asString();
if (_chart.containsKey("CLint")) iot.CL_int = _chart["CLint"];
if (!refresh && iot.CL_on) lastUpdateCloud = 0; // Daten senden forcieren
if (!setconfig(eTHING,{})) return 0;
return 1;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool setPitmaster(AsyncWebServerRequest *request, uint8_t *datas) {
printRequest(datas);
DynamicJsonBuffer jsonBuffer;
JsonArray& json = jsonBuffer.parseArray((const char*)datas); //https://github.com/esp8266/Arduino/issues/1321
if (!json.success()) return 0;
byte id, ii = 0;
for (JsonArray::iterator it=json.begin(); it!=json.end(); ++it) {
JsonObject& _pitmaster = json[ii];
if (_pitmaster.containsKey("id")) id = _pitmaster["id"];
else break;
if (id >= PITMASTERSIZE) break;
String typ;
if (_pitmaster.containsKey("typ"))
typ = _pitmaster["typ"].asString();
else return 0;
if (_pitmaster.containsKey("channel")) {
byte cha = _pitmaster["channel"];
pitMaster[id].channel = cha - 1;
}
else return 0;
if (_pitmaster.containsKey("pid")) pitMaster[ii].pid = _pitmaster["pid"];
else return 0;
if (_pitmaster.containsKey("set")) pitMaster[ii].set = _pitmaster["set"];
else return 0;
bool _manual = false;
bool _autotune = false;
if (typ == "autotune") _autotune = true;
else if (typ == "manual") _manual = true;
else if (typ == "auto") pitMaster[id].active = AUTO;
else pitMaster[id].active = PITOFF;
if (_pitmaster.containsKey("value") && _manual) {
int _val = _pitmaster["value"];
pitMaster[id].value = constrain(_val,0,100);
pitMaster[id].active = MANUAL;
//return 1; // nicht speichern
}
if (_autotune && id == 0) {
startautotunePID(5, true, 40, 120L*60L*1000L, id); // 1h Timelimit
return 1; // nicht speichern
} else if (autotune.initialized) { // Autotune was still in action
autotune.stop = 2;
}
ii++;
}
// Spezial-Funktionen
if (pid[pitMaster[0].pid].aktor == DAMPER && sys.hwversion > 1) {
pitMaster[0].io = PITMASTER1; // Zurücksetzen falls vorher Servo gewählt
// aktiviere zweiten Pitmaster nur wenn ein Servo-Profil vorhanden
if (pid[2].aktor == SERVO) {
pitMaster[1].pid = 2;
pitMaster[1].channel = pitMaster[0].channel;
pitMaster[1].set = pitMaster[0].set;
pitMaster[1].active = pitMaster[0].active;
pitMaster[1].value = pitMaster[0].value; // Manual
} // FAN trotzdem laufen lassen? Oder Speichern abbrechen?
} else if (pid[pitMaster[0].pid].aktor == SERVO && sys.hwversion > 1) {
pitMaster[0].io = PITMASTER2; // SERVO DUPLICATE
// muss auch bei DutyCycle
} else {
pitMaster[1].active = PITOFF;
pitMaster[0].io = PITMASTER1; // Zurücksetzen
}
if (!setconfig(ePIT,{})) return 0;
return 1;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool setPID(AsyncWebServerRequest *request, uint8_t *datas) {
printRequest(datas);
DynamicJsonBuffer jsonBuffer;
JsonArray& json = jsonBuffer.parseArray((const char*)datas); //https://github.com/esp8266/Arduino/issues/1321
if (!json.success()) return 0;
byte id = 0, ii = 0;
float val;
for (JsonArray::iterator it=json.begin(); it!=json.end(); ++it) {
JsonObject& _pid = json[ii];
if (_pid.containsKey("id")) id = _pid["id"];
else break;
if (id >= pidsize) break;
if (_pid.containsKey("name")) pid[id].name = _pid["name"].asString();
if (_pid.containsKey("aktor")) pid[id].aktor = _pid["aktor"];
if (_pid.containsKey("Kp")) pid[id].Kp = _pid["Kp"];
if (_pid.containsKey("Ki")) pid[id].Ki = _pid["Ki"];
if (_pid.containsKey("Kd")) pid[id].Kd = _pid["Kd"];
if (_pid.containsKey("Kp_a")) pid[id].Kp_a = _pid["Kp_a"];
if (_pid.containsKey("Ki_a")) pid[id].Ki_a = _pid["Ki_a"];
if (_pid.containsKey("Kd_a")) pid[id].Kd_a = _pid["Kd_a"];
if (_pid.containsKey("DCmmin")) {
val = _pid["DCmmin"];
if (val >= SERVOPULSMIN && val <= SERVOPULSMAX && pid[id].aktor == SERVO) {
pid[id].DCmin = getDC(val*10)/10.0;
} else pid[id].DCmin = constrain(val*10,0,1000)/10.0; // 1. Nachkommastelle
}
if (_pid.containsKey("DCmmax")) {
val = _pid["DCmmax"];
if (val >= SERVOPULSMIN && val <= SERVOPULSMAX && pid[id].aktor == SERVO) {
pid[id].DCmax = getDC(val*10)/10.0;
} else pid[id].DCmax = constrain(val*10,0,1000)/10.0; // 1. Nachkommastelle
}
ii++;
}
if (!setconfig(ePIT,{})) return 0;
return 1;
}
public:
BodyWebHandler(void){}
// {"ssid":"xxx","password":"xxx"}
bool setNetwork(uint8_t *datas) {
AsyncWebServerRequest *request;
return setNetwork(request, datas);
}
// {"number":1,"name":"Kanal 1","typ":0,"temp":24.50,"min":10.00,"max":35.00,"alarm":false,"color":"#0C4C88"}
bool setChannels(uint8_t *datas) {
AsyncWebServerRequest *request;
return setChannels(request, datas);
}
// {"ap":"NANO-AP","host":"NANO-82e0b3","language":"de","unit":"C","hwalarm":false,"fastmode":false,"autoupd":true,"hwversion":"V1"}
bool setSystem(uint8_t *datas) {
AsyncWebServerRequest *request;
return setSystem(request, datas);
}
// {"channel":1,"pid":0,"value":0,"set":50.00,"typ":"off"}
bool setPitmaster(uint8_t *datas) {
AsyncWebServerRequest *request;
return setPitmaster(request, datas);
}
bool setPID(uint8_t *datas) {
AsyncWebServerRequest *request;
return setPID(request, datas);
}
// {"TSwrite":"","TShttp":"","TSuser":"","TSchID":"","TSshow8":false,"TSint":30,"TSon":false,"PMQhost":"192.168.2.1","PMQport":1883,"PMQuser":"","PMQpass":"","PMQqos":0,"PMQon":false,"PMQint":30,"TGon":0,"TGtoken":"","TGid":"","CLon":true,"CLtoken":"82e0b30c6486bede","CLint":30}
bool setIoT(uint8_t *datas) {
AsyncWebServerRequest *request;
return setIoT(request, datas);
}
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
if (request->url() == SET_NETWORK) {
if (!setNetwork(request, data)) request->send(200, TEXTPLAIN, TEXTFALSE);
request->send(200, TEXTPLAIN, TEXTTRUE);
} else if (request->url() == SET_CHANNELS) {
if(!request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
if(!setChannels(request,data)) request->send(200, TEXTPLAIN, TEXTFALSE);
request->send(200, TEXTPLAIN, TEXTTRUE);
} else if (request->url() == SET_SYSTEM) {
if(!request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
if(!setSystem(request, data)) request->send(200, TEXTPLAIN, TEXTFALSE);
request->send(200, TEXTPLAIN, TEXTTRUE);
} else if (request->url() == SET_PITMASTER) {
if(!request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
if(!setPitmaster(request, data)) request->send(200, TEXTPLAIN, TEXTFALSE);
request->send(200, TEXTPLAIN, TEXTTRUE);
} else if (request->url() == SET_PID) {
if(!request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
if(!setPID(request, data)) request->send(200, TEXTPLAIN, TEXTFALSE);
request->send(200, TEXTPLAIN, TEXTTRUE);
} else if (request->url() == SET_IOT) {
if(!request->authenticate(sys.www_username, sys.www_password.c_str()))
return request->requestAuthentication();
if(!setIoT(request, data)) request->send(200, TEXTPLAIN, TEXTFALSE);
request->send(200, TEXTPLAIN, TEXTTRUE);
}
}
bool canHandle(AsyncWebServerRequest *request){
if (request->method() == HTTP_GET) return false;
if (request->url() == SET_NETWORK || request->url() == SET_CHANNELS
|| request->url() == SET_SYSTEM || request->url() == SET_PITMASTER
|| request->url() == SET_PID || request->url() == SET_IOT
) return true;
return false;
}
};
BodyWebHandler bodyWebHandler;