forked from lumapu/ahoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.cpp
595 lines (511 loc) · 24.5 KB
/
web.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
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
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#if defined(ESP32) && defined(F)
#undef F
#define F(sl) (sl)
#endif
#include "web.h"
#include "html/h/index_html.h"
#include "html/h/style_css.h"
#include "favicon.h"
#include "html/h/setup_html.h"
#include "html/h/visualization_html.h"
const uint16_t pwrLimitOptionValues[] {
NoPowerLimit,
AbsolutNonPersistent,
AbsolutPersistent,
RelativNonPersistent,
RelativPersistent
};
const char* const pwrLimitOptions[] {
"no power limit",
"absolute in Watt non persistent",
"absolute in Watt persistent",
"relativ in percent non persistent",
"relativ in percent persistent"
};
//-----------------------------------------------------------------------------
web::web(app *main, sysConfig_t *sysCfg, config_t *config, char version[]) {
mMain = main;
mSysCfg = sysCfg;
mConfig = config;
mVersion = version;
#ifdef ESP8266
mWeb = new ESP8266WebServer(80);
mUpdater = new ESP8266HTTPUpdateServer();
#elif defined(ESP32)
mWeb = new WebServer(80);
mUpdater = new HTTPUpdateServer();
#endif
mUpdater->setup(mWeb);
}
//-----------------------------------------------------------------------------
void web::setup(void) {
DPRINTLN(DBG_VERBOSE, F("app::setup-begin"));
mWeb->begin();
DPRINTLN(DBG_VERBOSE, F("app::setup-on"));
mWeb->on("/", std::bind(&web::showIndex, this));
mWeb->on("/style.css", std::bind(&web::showCss, this));
mWeb->on("/favicon.ico", std::bind(&web::showFavicon, this));
mWeb->onNotFound ( std::bind(&web::showNotFound, this));
mWeb->on("/uptime", std::bind(&web::showUptime, this));
mWeb->on("/reboot", std::bind(&web::showReboot, this));
mWeb->on("/erase", std::bind(&web::showErase, this));
mWeb->on("/factory", std::bind(&web::showFactoryRst, this));
mWeb->on("/setup", std::bind(&web::showSetup, this));
mWeb->on("/save", std::bind(&web::showSave, this));
mWeb->on("/cmdstat", std::bind(&web::showStatistics, this));
mWeb->on("/visualization", std::bind(&web::showVisualization, this));
mWeb->on("/livedata", std::bind(&web::showLiveData, this));
mWeb->on("/json", std::bind(&web::showJson, this));
mWeb->on("/api", HTTP_POST, std::bind(&web::showWebApi, this));
}
//-----------------------------------------------------------------------------
void web::loop(void) {
mWeb->handleClient();
}
//-----------------------------------------------------------------------------
void web::showIndex(void) {
DPRINTLN(DBG_VERBOSE, F("showIndex"));
String html = FPSTR(index_html);
html.replace(F("{DEVICE}"), mSysCfg->deviceName);
html.replace(F("{VERSION}"), mVersion);
html.replace(F("{TS}"), String(mConfig->sendInterval) + " ");
html.replace(F("{JS_TS}"), String(mConfig->sendInterval * 1000));
html.replace(F("{BUILD}"), String(AUTO_GIT_HASH));
mWeb->send(200, "text/html", html);
}
//-----------------------------------------------------------------------------
void web::showCss(void) {
mWeb->send(200, "text/css", FPSTR(style_css));
}
//-----------------------------------------------------------------------------
void web::showFavicon(void) {
static const char favicon_type[] PROGMEM = "image/x-icon";
static const char favicon_content[] PROGMEM = FAVICON_PANEL_16;
mWeb->send_P(200, favicon_type, favicon_content, sizeof(favicon_content));
}
//-----------------------------------------------------------------------------
void web::showNotFound(void) {
DPRINTLN(DBG_VERBOSE, F("showNotFound - ") + mWeb->uri());
String msg = F("File Not Found\n\nURI: ");
msg += mWeb->uri();
mWeb->send(404, F("text/plain"), msg);
}
//-----------------------------------------------------------------------------
void web::showUptime(void) {
char time[21] = {0};
uint32_t uptime = mMain->getUptime();
uint32_t upTimeSc = uint32_t((uptime) % 60);
uint32_t upTimeMn = uint32_t((uptime / (60)) % 60);
uint32_t upTimeHr = uint32_t((uptime / (60 * 60)) % 24);
uint32_t upTimeDy = uint32_t((uptime / (60 * 60 * 24)) % 365);
snprintf(time, 20, "%d Days, %02d:%02d:%02d", upTimeDy, upTimeHr, upTimeMn, upTimeSc);
mWeb->send(200, "text/plain", String(time) + "; now: " + mMain->getDateTimeStr(mMain->getTimestamp()));
}
//-----------------------------------------------------------------------------
void web::showReboot(void) {
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Rebooting ...</title><meta http-equiv=\"refresh\" content=\"10; URL=/\"></head><body>rebooting ... auto reload after 10s</body></html>"));
delay(1000);
ESP.restart();
}
//-----------------------------------------------------------------------------
void web::showErase() {
DPRINTLN(DBG_VERBOSE, F("showErase"));
mMain->eraseSettings();
showReboot();
}
//-----------------------------------------------------------------------------
void web::showFactoryRst(void) {
DPRINTLN(DBG_VERBOSE, F("showFactoryRst"));
String content = "";
int refresh = 3;
if(mWeb->args() > 0) {
if(mWeb->arg("reset").toInt() == 1) {
mMain->eraseSettings(true);
content = F("factory reset: success\n\nrebooting ... ");
refresh = 10;
}
else {
content = F("factory reset: aborted");
refresh = 3;
}
}
else {
content = F("<h1>Factory Reset</h1>"
"<p><a href=\"/factory?reset=1\">RESET</a><br/><br/><a href=\"/factory?reset=0\">CANCEL</a><br/></p>");
refresh = 120;
}
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Factory Reset</title><meta http-equiv=\"refresh\" content=\"") + String(refresh) + F("; URL=/\"></head><body>") + content + F("</body></html>"));
if(refresh == 10) {
delay(1000);
ESP.restart();
}
}
//-----------------------------------------------------------------------------
void web::showSetup(void) {
DPRINTLN(DBG_VERBOSE, F("showSetup"));
String html = FPSTR(setup_html);
html.replace(F("{SSID}"), mSysCfg->stationSsid);
// PWD will be left at the default value (for protection)
// -> the PWD will only be changed if it does not match the default "{PWD}"
html.replace(F("{DEVICE}"), String(mSysCfg->deviceName));
html.replace(F("{VERSION}"), String(mVersion));
if(mMain->getWifiApActive())
html.replace("{IP}", String(F("http://192.168.1.1")));
else
html.replace("{IP}", (F("http://") + String(WiFi.localIP().toString())));
String inv = "";
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mMain->mSys->getInverterByPos(i);
inv += F("<p class=\"subdes\">Inverter ") + String(i) + "</p>";
inv += F("<label for=\"inv") + String(i) + F("Addr\">Address*</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("Addr\" value=\"");
if(NULL != iv)
inv += String(iv->serial.u64, HEX);
inv += F("\"/ maxlength=\"12\">");
inv += F("<label for=\"inv") + String(i) + F("Name\">Name*</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("Name\" value=\"");
if(NULL != iv)
inv += String(iv->name);
inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">";
inv += F("<label for=\"inv") + String(i) + F("ActivePowerLimit\">Active Power Limit</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("ActivePowerLimit\" value=\"");
if(NULL != iv)
inv += String(iv->powerLimit[0]);
inv += F("\"/ maxlength=\"") + String(6) + "\">";
inv += F("<label for=\"inv") + String(i) + F("ActivePowerLimitConType\">Active Power Limit Control Type</label>");
inv += F("<select name=\"inv") + String(i) + F("PowerLimitControl\">");
for(uint8_t j = 0; j < 5; j++) {
inv += F("<option value=\"") + String(pwrLimitOptionValues[j]) + F("\"");
if(NULL != iv) {
if(iv->powerLimit[1] == pwrLimitOptionValues[j])
inv += F(" selected");
}
inv += F(">") + String(pwrLimitOptions[j]) + F("</option>");
}
inv += F("</select>");
inv += F("<label for=\"inv") + String(i) + F("ModPwr0\" name=\"lbl") + String(i);
inv += F("ModPwr\">Max Module Power (Wp)</label><div class=\"modpwr\">");
for(uint8_t j = 0; j < 4; j++) {
inv += F("<input type=\"text\" class=\"text sh\" name=\"inv") + String(i) + F("ModPwr") + String(j) + F("\" value=\"");
if(NULL != iv)
inv += String(iv->chMaxPwr[j]);
inv += F("\"/ maxlength=\"4\">");
}
inv += F("</div><br/><label for=\"inv") + String(i) + F("ModName0\" name=\"lbl") + String(i);
inv += F("ModName\">Module Name</label><div class=\"modname\">");
for(uint8_t j = 0; j < 4; j++) {
inv += F("<input type=\"text\" class=\"text sh\" name=\"inv") + String(i) + F("ModName") + String(j) + F("\" value=\"");
if(NULL != iv)
inv += String(iv->chName[j]);
inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">";
}
inv += F("</div>");
}
html.replace(F("{INVERTERS}"), String(inv));
// pinout
String pinout;
for(uint8_t i = 0; i < 3; i++) {
pinout += F("<label for=\"") + String(pinArgNames[i]) + "\">" + String(pinNames[i]) + F("</label>");
pinout += F("<select name=\"") + String(pinArgNames[i]) + "\">";
for(uint8_t j = 0; j <= 16; j++) {
pinout += F("<option value=\"") + String(j) + "\"";
switch(i) {
default: if(j == mConfig->pinCs) pinout += F(" selected"); break;
case 1: if(j == mConfig->pinCe) pinout += F(" selected"); break;
case 2: if(j == mConfig->pinIrq) pinout += F(" selected"); break;
}
pinout += ">" + String(wemosPins[j]) + F("</option>");
}
pinout += F("</select>");
}
html.replace(F("{PINOUT}"), String(pinout));
// nrf24l01+
String rf24;
for(uint8_t i = 0; i <= 3; i++) {
rf24 += F("<option value=\"") + String(i) + "\"";
if(i == mConfig->amplifierPower)
rf24 += F(" selected");
rf24 += ">" + String(rf24AmpPowerNames[i]) + F("</option>");
}
html.replace(F("{RF24}"), String(rf24));
html.replace(F("{INV_INTVL}"), String(mConfig->sendInterval));
html.replace(F("{INV_RETRIES}"), String(mConfig->maxRetransPerPyld));
html.replace(F("{SER_INTVL}"), String(mConfig->serialInterval));
html.replace(F("{SER_VAL_CB}"), (mConfig->serialShowIv) ? "checked" : "");
html.replace(F("{SER_DBG_CB}"), (mConfig->serialDebug) ? "checked" : "");
html.replace(F("{NTP_ADDR}"), String(mConfig->ntpAddr));
html.replace(F("{NTP_PORT}"), String(mConfig->ntpPort));
html.replace(F("{MQTT_ADDR}"), String(mConfig->mqtt.broker));
html.replace(F("{MQTT_PORT}"), String(mConfig->mqtt.port));
html.replace(F("{MQTT_USER}"), String(mConfig->mqtt.user));
html.replace(F("{MQTT_PWD}"), String(mConfig->mqtt.pwd));
html.replace(F("{MQTT_TOPIC}"), String(mConfig->mqtt.topic));
mWeb->send(200, F("text/html"), html);
}
//-----------------------------------------------------------------------------
void web::showSave(void) {
DPRINTLN(DBG_VERBOSE, F("showSave"));
if(mWeb->args() > 0) {
char buf[20] = {0};
// general
if(mWeb->arg("ssid") != "")
mWeb->arg("ssid").toCharArray(mSysCfg->stationSsid, SSID_LEN);
if(mWeb->arg("pwd") != "{PWD}")
mWeb->arg("pwd").toCharArray(mSysCfg->stationPwd, PWD_LEN);
if(mWeb->arg("device") != "")
mWeb->arg("device").toCharArray(mSysCfg->deviceName, DEVNAME_LEN);
// inverter
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mMain->mSys->getInverterByPos(i, false);
// address
mWeb->arg("inv" + String(i) + "Addr").toCharArray(buf, 20);
if(strlen(buf) == 0)
memset(buf, 0, 20);
iv->serial.u64 = mMain->Serial2u64(buf);
// active power limit
uint16_t actPwrLimit = mWeb->arg("inv" + String(i) + "ActivePowerLimit").toInt();
uint16_t actPwrLimitControl = mWeb->arg("inv" + String(i) + "PowerLimitControl").toInt();
if (actPwrLimit != 0xffff && actPwrLimit > 0){
iv->powerLimit[0] = actPwrLimit;
iv->powerLimit[1] = actPwrLimitControl;
iv->devControlCmd = ActivePowerContr;
iv->devControlRequest = true;
if ((iv->powerLimit[1] & 0x0001) == 0x0001)
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("%") );
else {
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("W") );
DPRINTLN(DBG_INFO, F("Power Limit Control Setting ") + String(iv->powerLimit[1]));
}
}
if (actPwrLimit == 0xffff) { // set to 100%
iv->powerLimit[0] = 100;
iv->powerLimit[1] = RelativPersistent;
iv->devControlCmd = ActivePowerContr;
iv->devControlRequest = true;
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to unlimted"));
}
// name
mWeb->arg("inv" + String(i) + "Name").toCharArray(iv->name, MAX_NAME_LENGTH);
// max channel power / name
for(uint8_t j = 0; j < 4; j++) {
iv->chMaxPwr[j] = mWeb->arg("inv" + String(i) + "ModPwr" + String(j)).toInt() & 0xffff;
mWeb->arg("inv" + String(i) + "ModName" + String(j)).toCharArray(iv->chName[j], MAX_NAME_LENGTH);
}
iv->initialized = true;
}
if(mWeb->arg("invInterval") != "")
mConfig->sendInterval = mWeb->arg("invInterval").toInt();
if(mWeb->arg("invRetry") != "")
mConfig->maxRetransPerPyld = mWeb->arg("invRetry").toInt();
// pinout
uint8_t pin;
for(uint8_t i = 0; i < 3; i ++) {
pin = mWeb->arg(String(pinArgNames[i])).toInt();
switch(i) {
default: mConfig->pinCs = pin; break;
case 1: mConfig->pinCe = pin; break;
case 2: mConfig->pinIrq = pin; break;
}
}
// nrf24 amplifier power
mConfig->amplifierPower = mWeb->arg("rf24Power").toInt() & 0x03;
// ntp
if(mWeb->arg("ntpAddr") != "") {
mWeb->arg("ntpAddr").toCharArray(mConfig->ntpAddr, NTP_ADDR_LEN);
mConfig->ntpPort = mWeb->arg("ntpPort").toInt() & 0xffff;
}
// mqtt
if(mWeb->arg("mqttAddr") != "") {
String addr = mWeb->arg("mqttAddr");
addr.trim();
addr.toCharArray(mConfig->mqtt.broker, MQTT_ADDR_LEN);
mWeb->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN);
mWeb->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN);
mWeb->arg("mqttTopic").toCharArray(mConfig->mqtt.topic, MQTT_TOPIC_LEN);
mConfig->mqtt.port = mWeb->arg("mqttPort").toInt();
}
// serial console
if(mWeb->arg("serIntvl") != "") {
mConfig->serialInterval = mWeb->arg("serIntvl").toInt() & 0xffff;
mConfig->serialDebug = (mWeb->arg("serDbg") == "on");
mConfig->serialShowIv = (mWeb->arg("serEn") == "on");
// Needed to log TX buffers to serial console
mMain->mSys->Radio.mSerialDebug = mConfig->serialDebug;
}
mMain->saveValues();
if(mWeb->arg("reboot") == "on")
showReboot();
else
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Setup saved</title><meta http-equiv=\"refresh\" content=\"0; URL=/setup\"></head><body>"
"<p>saved</p></body></html>"));
}
}
//-----------------------------------------------------------------------------
void web::showStatistics(void) {
DPRINTLN(DBG_VERBOSE, F("web::showStatistics"));
mWeb->send(200, F("text/plain"), mMain->getStatistics());
}
//-----------------------------------------------------------------------------
void web::showVisualization(void) {
DPRINTLN(DBG_VERBOSE, F("web::showVisualization"));
String html = FPSTR(visualization_html);
html.replace(F("{DEVICE}"), mSysCfg->deviceName);
html.replace(F("{VERSION}"), mVersion);
html.replace(F("{TS}"), String(mConfig->sendInterval) + " ");
html.replace(F("{JS_TS}"), String(mConfig->sendInterval * 1000));
mWeb->send(200, F("text/html"), html);
}
//-----------------------------------------------------------------------------
void web::showLiveData(void) {
DPRINTLN(DBG_VERBOSE, F("web::showLiveData"));
String modHtml;
for (uint8_t id = 0; id < mMain->mSys->getNumInverters(); id++) {
Inverter<> *iv = mMain->mSys->getInverterByPos(id);
if (NULL != iv) {
#ifdef LIVEDATA_VISUALIZED
uint8_t modNum, pos;
switch (iv->type) {
default:
case INV_TYPE_1CH: modNum = 1; break;
case INV_TYPE_2CH: modNum = 2; break;
case INV_TYPE_4CH: modNum = 4; break;
}
modHtml += F("<div class=\"iv\">"
"<div class=\"ch-iv\"><span class=\"head\">")
+ String(iv->name) + F(" Limit ")
+ String(iv->actPowerLimit) + F("%");
if(NoPowerLimit == iv->powerLimit[1])
modHtml += F(" (not controlled)");
modHtml += F(" | last Alarm: ") + iv->lastAlarmMsg + F("</span>");
uint8_t list[] = {FLD_UAC, FLD_IAC, FLD_PAC, FLD_F, FLD_PCT, FLD_T, FLD_YT, FLD_YD, FLD_PDC, FLD_EFF, FLD_PRA, FLD_ALARM_MES_ID};
for (uint8_t fld = 0; fld < 11; fld++) {
pos = (iv->getPosByChFld(CH0, list[fld]));
if (0xff != pos) {
modHtml += F("<div class=\"subgrp\">");
modHtml += F("<span class=\"value\">") + String(iv->getValue(pos));
modHtml += F("<span class=\"unit\">") + String(iv->getUnit(pos)) + F("</span></span>");
modHtml += F("<span class=\"info\">") + String(iv->getFieldName(pos)) + F("</span>");
modHtml += F("</div>");
}
}
modHtml += "</div>";
for (uint8_t ch = 1; ch <= modNum; ch++) {
modHtml += F("<div class=\"ch\"><span class=\"head\">");
if (iv->chName[ch - 1][0] == 0)
modHtml += F("CHANNEL ") + String(ch);
else
modHtml += String(iv->chName[ch - 1]);
modHtml += F("</span>");
for (uint8_t j = 0; j < 6; j++) {
switch (j) {
default: pos = (iv->getPosByChFld(ch, FLD_UDC)); break;
case 1: pos = (iv->getPosByChFld(ch, FLD_IDC)); break;
case 2: pos = (iv->getPosByChFld(ch, FLD_PDC)); break;
case 3: pos = (iv->getPosByChFld(ch, FLD_YD)); break;
case 4: pos = (iv->getPosByChFld(ch, FLD_YT)); break;
case 5: pos = (iv->getPosByChFld(ch, FLD_IRR)); break;
}
if (0xff != pos) {
modHtml += F("<span class=\"value\">") + String(iv->getValue(pos));
modHtml += F("<span class=\"unit\">") + String(iv->getUnit(pos)) + F("</span></span>");
modHtml += F("<span class=\"info\">") + String(iv->getFieldName(pos)) + F("</span>");
}
}
modHtml += "</div>";
yield();
}
modHtml += F("<div class=\"ts\">Last received data requested at: ") + mMain->getDateTimeStr(iv->ts) + F("</div>");
modHtml += F("</div>");
#else
// dump all data to web frontend
modHtml = F("<pre>");
char topic[30], val[10];
for (uint8_t i = 0; i < iv->listLen; i++) {
snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i));
snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i));
modHtml += String(topic) + ": " + String(val) + "\n";
}
modHtml += F("</pre>");
#endif
}
}
mWeb->send(200, F("text/html"), modHtml);
}
//-----------------------------------------------------------------------------
void web::showJson(void) {
DPRINTLN(DBG_VERBOSE, F("web::showJson"));
mWeb->send(200, F("application/json"), mMain->getJson());
}
//-----------------------------------------------------------------------------
void web::showWebApi(void)
{
DPRINTLN(DBG_VERBOSE, F("web::showWebApi"));
DPRINTLN(DBG_DEBUG, mWeb->arg("plain"));
const size_t capacity = 200; // Use arduinojson.org/assistant to compute the capacity.
DynamicJsonDocument response(capacity);
// Parse JSON object
deserializeJson(response, mWeb->arg("plain"));
// ToDo: error handling for payload
uint8_t iv_id = response["inverter"];
uint8_t cmd = response["cmd"];
Inverter<> *iv = mMain->mSys->getInverterByPos(iv_id);
if (NULL != iv)
{
if (response["tx_request"] == (uint8_t)TX_REQ_INFO)
{
// if the AlarmData is requested set the Alarm Index to the requested one
if (cmd == AlarmData || cmd == AlarmUpdate){
// set the AlarmMesIndex for the request from user input
iv->alarmMesIndex = response["payload"];
}
DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(cmd) + F(" and payload ") + String((uint16_t) response["payload"]));
// process payload from web request corresponding to the cmd
iv->enqueCommand<InfoCommand>(cmd);
}
if (response["tx_request"] == (uint8_t)TX_REQ_DEVCONTROL)
{
if (response["cmd"] == (uint8_t)ActivePowerContr)
{
uint16_t webapiPayload = response["payload"];
uint16_t webapiPayload2 = response["payload2"];
if (webapiPayload > 0 && webapiPayload < 10000)
{
iv->devControlCmd = ActivePowerContr;
iv->powerLimit[0] = webapiPayload;
if (webapiPayload2 > 0)
{
iv->powerLimit[1] = webapiPayload2; // dev option, no sanity check
}
else
{ // if not set, set it to 0x0000 default
iv->powerLimit[1] = AbsolutNonPersistent; // payload will be seted temporay in Watt absolut
}
if (iv->powerLimit[1] & 0x0001)
{
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("% via REST API"));
}
else
{
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("W via REST API"));
}
iv->devControlRequest = true; // queue it in the request loop
}
}
if (response["cmd"] == (uint8_t)TurnOff){
iv->devControlCmd = TurnOff;
iv->devControlRequest = true; // queue it in the request loop
}
if (response["cmd"] == (uint8_t)TurnOn){
iv->devControlCmd = TurnOn;
iv->devControlRequest = true; // queue it in the request loop
}
}
}
mWeb->send(200, "text/json", "{success:true}");
}