-
Notifications
You must be signed in to change notification settings - Fork 219
/
webserver.cpp
734 lines (591 loc) · 28.2 KB
/
webserver.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
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
//+--------------------------------------------------------------------------
//
// File: webserver.cpp
//
// NightDriverStrip - (c) 2018 Plummer's Software LLC. All Rights Reserved.
//
// This file is part of the NightDriver software project.
//
// NightDriver 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.
//
// NightDriver 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 Nightdriver. It is normally found in copying.txt
// If not, see <https://www.gnu.org/licenses/>.
//
// Description:
//
// Implementations for some of the web server methods declared in webserver.h
//
// History: Apr-18-2023 Rbergen Created
// Apr-28-2023 Rbergen Reduce code duplication
//---------------------------------------------------------------------------
#include "globals.h"
#include "webserver.h"
#include <utility>
#include "systemcontainer.h"
#include "soundanalyzer.h"
#include "improvserial.h"
// Static member initializers
// Maps settings for which a validator is available to the invocation thereof
const std::map<String, CWebServer::ValueValidator> CWebServer::settingValidators
{
{ DeviceConfig::OpenWeatherApiKeyTag, [](const String& value) { return g_ptrSystem->DeviceConfig().ValidateOpenWeatherAPIKey(value); } },
{ DeviceConfig::PowerLimitTag, [](const String& value) { return g_ptrSystem->DeviceConfig().ValidatePowerLimit(value); } },
{ DeviceConfig::BrightnessTag, [](const String& value) { return g_ptrSystem->DeviceConfig().ValidateBrightness(value); } }
};
std::vector<SettingSpec, psram_allocator<SettingSpec>> CWebServer::mySettingSpecs = {};
std::vector<std::reference_wrapper<SettingSpec>> CWebServer::deviceSettingSpecs{};
// Member function template specializations
// Push param that represents a bool. Values considered true are text "true" and any whole number not equal to 0
template<>
bool CWebServer::PushPostParamIfPresent<bool>(AsyncWebServerRequest * pRequest, const String ¶mName, ValueSetter<bool> setter)
{
return PushPostParamIfPresent<bool>(pRequest, paramName, std::move(setter), [](AsyncWebParameter * param) constexpr
{
return BoolFromText(param->value());
});
}
// Push param that represents a size_t
template<>
bool CWebServer::PushPostParamIfPresent<size_t>(AsyncWebServerRequest * pRequest, const String ¶mName, ValueSetter<size_t> setter)
{
return PushPostParamIfPresent<size_t>(pRequest, paramName, std::move(setter), [](AsyncWebParameter * param) constexpr
{
return strtoul(param->value().c_str(), nullptr, 10);
});
}
// Push param that represents an int
template<>
bool CWebServer::PushPostParamIfPresent<int>(AsyncWebServerRequest * pRequest, const String ¶mName, ValueSetter<int> setter)
{
return PushPostParamIfPresent<int>(pRequest, paramName, std::move(setter), [](AsyncWebParameter * param) constexpr
{
return std::stoi(param->value().c_str());
});
}
// Push param that represents a color
template<>
bool CWebServer::PushPostParamIfPresent<CRGB>(AsyncWebServerRequest * pRequest, const String ¶mName, ValueSetter<CRGB> setter)
{
return PushPostParamIfPresent<CRGB>(pRequest, paramName, std::move(setter), [](AsyncWebParameter * param) constexpr
{
return CRGB(strtoul(param->value().c_str(), nullptr, 10));
});
}
// Add CORS header to and send JSON response
template<>
void CWebServer::AddCORSHeaderAndSendResponse<AsyncJsonResponse>(AsyncWebServerRequest * pRequest, AsyncJsonResponse * pResponse)
{
pResponse->setLength();
AddCORSHeaderAndSendResponse<AsyncWebServerResponse>(pRequest, pResponse);
}
// Member function implementations
// begin - register page load handlers and start serving pages
void CWebServer::begin()
{
[[maybe_unused]] extern const uint8_t html_start[] asm("_binary_site_dist_index_html_gz_start");
[[maybe_unused]] extern const uint8_t html_end[] asm("_binary_site_dist_index_html_gz_end");
[[maybe_unused]] extern const uint8_t js_start[] asm("_binary_site_dist_index_js_gz_start");
[[maybe_unused]] extern const uint8_t js_end[] asm("_binary_site_dist_index_js_gz_end");
[[maybe_unused]] extern const uint8_t ico_start[] asm("_binary_site_dist_favicon_ico_gz_start");
[[maybe_unused]] extern const uint8_t ico_end[] asm("_binary_site_dist_favicon_ico_gz_end");
[[maybe_unused]] extern const uint8_t timezones_start[] asm("_binary_config_timezones_json_start");
[[maybe_unused]] extern const uint8_t timezones_end[] asm("_binary_config_timezones_json_end");
EmbeddedWebFile html_file(html_start, html_end, "text/html", "gzip");
EmbeddedWebFile js_file(js_start, js_end, "application/javascript", "gzip");
EmbeddedWebFile ico_file(ico_start, ico_end, "image/vnd.microsoft.icon", "gzip");
EmbeddedWebFile timezones_file(timezones_start, timezones_end - 1, "text/json"); // end - 1 because of zero-termination
debugI("Embedded html file size: %d", html_file.length);
debugI("Embedded jsx file size: %d", js_file.length);
debugI("Embedded ico file size: %d", ico_file.length);
debugI("Embedded timezones file size: %d", timezones_file.length);
_staticStats.HeapSize = ESP.getHeapSize();
_staticStats.DmaHeapSize = heap_caps_get_total_size(MALLOC_CAP_DMA);
_staticStats.PsramSize = ESP.getPsramSize();
_staticStats.ChipModel = ESP.getChipModel();
_staticStats.ChipCores = ESP.getChipCores();
_staticStats.CpuFreqMHz = ESP.getCpuFreqMHz();
_staticStats.SketchSize = ESP.getSketchSize();
_staticStats.FreeSketchSpace = ESP.getFreeSketchSpace();
_staticStats.FlashChipSize = ESP.getFlashChipSize();
debugI("Connecting Web Endpoints");
// SPIFFS file requests
_server.on("/effectsConfig", HTTP_GET, [](AsyncWebServerRequest* pRequest) { pRequest->send(SPIFFS, EFFECTS_CONFIG_FILE, "text/json"); });
#if ENABLE_IMPROV_LOGGING
_server.on(IMPROV_LOG_FILE, HTTP_GET, [](AsyncWebServerRequest* pRequest) { pRequest->send(SPIFFS, IMPROV_LOG_FILE, "text/plain"); });
#endif
// Instance handler requests
_server.on("/statistics", HTTP_GET, [this](AsyncWebServerRequest* pRequest) { this->GetStatistics(pRequest); });
_server.on("/getStatistics", HTTP_GET, [this](AsyncWebServerRequest* pRequest) { this->GetStatistics(pRequest); });
// Static handler requests
_server.on("/effects", HTTP_GET, GetEffectListText);
_server.on("/getEffectList", HTTP_GET, GetEffectListText);
_server.on("/nextEffect", HTTP_POST, NextEffect);
_server.on("/previousEffect", HTTP_POST, PreviousEffect);
_server.on("/currentEffect", HTTP_POST, SetCurrentEffectIndex);
_server.on("/setCurrentEffectIndex", HTTP_POST, SetCurrentEffectIndex);
_server.on("/enableEffect", HTTP_POST, EnableEffect);
_server.on("/disableEffect", HTTP_POST, DisableEffect);
_server.on("/moveEffect", HTTP_POST, MoveEffect);
_server.on("/copyEffect", HTTP_POST, CopyEffect);
_server.on("/deleteEffect", HTTP_POST, DeleteEffect);
_server.on("/settings/effect/specs", HTTP_GET, GetEffectSettingSpecs);
_server.on("/settings/effect", HTTP_GET, GetEffectSettings);
_server.on("/settings/effect", HTTP_POST, SetEffectSettings);
_server.on("/settings/validated", HTTP_POST, ValidateAndSetSetting);
_server.on("/settings/specs", HTTP_GET, GetSettingSpecs);
_server.on("/settings", HTTP_GET, GetSettings);
_server.on("/settings", HTTP_POST, SetSettings);
_server.on("/reset", HTTP_POST, Reset);
// Embedded file requests
ServeEmbeddedFile("/timezones.json", timezones_file);
#if ENABLE_WEB_UI
debugI("Web UI URL pathnames enabled");
ServeEmbeddedFile("/", html_file);
ServeEmbeddedFile("/index.html", html_file);
ServeEmbeddedFile("/index.js", js_file);
ServeEmbeddedFile("/favicon.ico", ico_file);
#endif
// Not found handler
_server.onNotFound([](AsyncWebServerRequest *request)
{
if (request->method() == HTTP_OPTIONS) {
request->send(HTTP_CODE_OK); // Apparently needed for CORS: https://github.com/me-no-dev/ESPAsyncWebServer
} else {
debugW("Failed GET for %s\n", request->url().c_str() );
request->send(HTTP_CODE_NOT_FOUND);
}
});
_server.begin();
debugI("HTTP server started");
}
bool CWebServer::IsPostParamTrue(AsyncWebServerRequest * pRequest, const String & paramName)
{
bool returnValue = false;
PushPostParamIfPresent<bool>(pRequest, paramName, [&returnValue](auto value) { returnValue = value; return true; });
return returnValue;
}
long CWebServer::GetEffectIndexFromParam(AsyncWebServerRequest * pRequest, bool post)
{
if (!pRequest->hasParam("effectIndex", post, false))
return -1;
return strtol(pRequest->getParam("effectIndex", post, false)->value().c_str(), nullptr, 10);
}
void CWebServer::GetEffectListText(AsyncWebServerRequest * pRequest)
{
static size_t jsonBufferSize = JSON_BUFFER_BASE_SIZE;
bool bufferOverflow;
debugV("GetEffectListText");
do
{
bufferOverflow = false;
auto response = std::make_unique<AsyncJsonResponse>(false, jsonBufferSize);
auto& j = response->getRoot();
auto& effectManager = g_ptrSystem->EffectManager();
j["currentEffect"] = effectManager.GetCurrentEffectIndex();
j["millisecondsRemaining"] = effectManager.GetTimeRemainingForCurrentEffect();
j["eternalInterval"] = effectManager.IsIntervalEternal();
j["effectInterval"] = effectManager.GetInterval();
for (const auto& effect : effectManager.EffectsList())
{
StaticJsonDocument<256> effectDoc;
effectDoc["name"] = effect->FriendlyName();
effectDoc["enabled"] = effect->IsEnabled();
effectDoc["core"] = effect->IsCoreEffect();
if (!j["Effects"].add(effectDoc))
{
bufferOverflow = true;
jsonBufferSize += JSON_BUFFER_INCREMENT;
debugV("JSON response buffer overflow! Increased buffer to %zu bytes", jsonBufferSize);
break;
}
}
if (!bufferOverflow)
AddCORSHeaderAndSendResponse(pRequest, response.release());
} while (bufferOverflow);
}
void CWebServer::GetStatistics(AsyncWebServerRequest * pRequest) const
{
debugV("GetStatistics");
auto response = new AsyncJsonResponse(false, JSON_BUFFER_BASE_SIZE);
auto& j = response->getRoot();
j["LED_FPS"] = g_Values.FPS;
j["SERIAL_FPS"] = g_Analyzer._serialFPS;
j["AUDIO_FPS"] = g_Analyzer._AudioFPS;
j["HEAP_SIZE"] = _staticStats.HeapSize;
j["HEAP_FREE"] = ESP.getFreeHeap();
j["HEAP_MIN"] = ESP.getMinFreeHeap();
j["DMA_SIZE"] = _staticStats.DmaHeapSize;
j["DMA_FREE"] = heap_caps_get_free_size(MALLOC_CAP_DMA);
j["DMA_MIN"] = heap_caps_get_largest_free_block(MALLOC_CAP_DMA);
j["PSRAM_SIZE"] = _staticStats.PsramSize;
j["PSRAM_FREE"] = ESP.getFreePsram();
j["PSRAM_MIN"] = ESP.getMinFreePsram();
j["CHIP_MODEL"] = _staticStats.ChipModel;
j["CHIP_CORES"] = _staticStats.ChipCores;
j["CHIP_SPEED"] = _staticStats.CpuFreqMHz;
j["PROG_SIZE"] = _staticStats.SketchSize;
j["CODE_SIZE"] = _staticStats.SketchSize;
j["CODE_FREE"] = _staticStats.FreeSketchSpace;
j["FLASH_SIZE"] = _staticStats.FlashChipSize;
auto& taskManager = g_ptrSystem->TaskManager();
j["CPU_USED"] = taskManager.GetCPUUsagePercent();
j["CPU_USED_CORE0"] = taskManager.GetCPUUsagePercent(0);
j["CPU_USED_CORE1"] = taskManager.GetCPUUsagePercent(1);
AddCORSHeaderAndSendResponse(pRequest, response);
}
void CWebServer::SetCurrentEffectIndex(AsyncWebServerRequest * pRequest)
{
debugV("SetCurrentEffectIndex");
PushPostParamIfPresent<size_t>(pRequest, "currentEffectIndex", SET_VALUE(g_ptrSystem->EffectManager().SetCurrentEffectIndex(value)));
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::EnableEffect(AsyncWebServerRequest * pRequest)
{
debugV("EnableEffect");
PushPostParamIfPresent<size_t>(pRequest, "effectIndex", SET_VALUE(g_ptrSystem->EffectManager().EnableEffect(value)));
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::DisableEffect(AsyncWebServerRequest * pRequest)
{
debugV("DisableEffect");
PushPostParamIfPresent<size_t>(pRequest, "effectIndex", SET_VALUE(g_ptrSystem->EffectManager().DisableEffect(value)));
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::MoveEffect(AsyncWebServerRequest * pRequest)
{
debugV("MoveEffect");
auto fromIndex = GetEffectIndexFromParam(pRequest, true);
if (fromIndex == -1)
{
AddCORSHeaderAndSendOKResponse(pRequest);
return;
}
PushPostParamIfPresent<size_t>(pRequest, "newIndex", SET_VALUE(g_ptrSystem->EffectManager().MoveEffect(fromIndex, value)));
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::CopyEffect(AsyncWebServerRequest * pRequest)
{
debugV("CopyEffect");
auto index = GetEffectIndexFromParam(pRequest, true);
if (index == -1)
{
AddCORSHeaderAndSendOKResponse(pRequest);
return;
}
auto effect = g_ptrSystem->EffectManager().CopyEffect(index);
if (!effect)
{
AddCORSHeaderAndSendOKResponse(pRequest);
return;
}
ApplyEffectSettings(pRequest, effect);
if (g_ptrSystem->EffectManager().AppendEffect(effect))
SendEffectSettingsResponse(pRequest, effect);
else
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::DeleteEffect(AsyncWebServerRequest * pRequest)
{
debugV("DeleteEffect");
auto index = GetEffectIndexFromParam(pRequest, true);
if (index == -1)
{
AddCORSHeaderAndSendOKResponse(pRequest);
return;
}
if (index < g_ptrSystem->EffectManager().EffectCount() && g_ptrSystem->EffectManager().EffectsList()[index]->IsCoreEffect())
{
AddCORSHeaderAndSendBadRequest(pRequest, "Can't delete core effect");
return;
}
g_ptrSystem->EffectManager().DeleteEffect(index);
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::NextEffect(AsyncWebServerRequest * pRequest)
{
debugV("NextEffect");
g_ptrSystem->EffectManager().NextEffect();
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::PreviousEffect(AsyncWebServerRequest * pRequest)
{
debugV("PreviousEffect");
g_ptrSystem->EffectManager().PreviousEffect();
AddCORSHeaderAndSendOKResponse(pRequest);
}
void CWebServer::SendSettingSpecsResponse(AsyncWebServerRequest * pRequest, const std::vector<std::reference_wrapper<SettingSpec>> & settingSpecs)
{
static size_t jsonBufferSize = JSON_BUFFER_BASE_SIZE;
bool bufferOverflow;
do
{
bufferOverflow = false;
auto response = std::make_unique<AsyncJsonResponse>(false, jsonBufferSize);
auto jsonArray = response->getRoot().to<JsonArray>();
for (auto& specWrapper : settingSpecs)
{
auto& spec = specWrapper.get();
auto specObject = jsonArray.createNestedObject();
StaticJsonDocument<384> jsonDoc;
jsonDoc["name"] = spec.Name;
jsonDoc["friendlyName"] = spec.FriendlyName;
if (spec.Description)
jsonDoc["description"] = spec.Description;
jsonDoc["type"] = to_value(spec.Type);
jsonDoc["typeName"] = spec.TypeName();
if (spec.HasValidation)
jsonDoc["hasValidation"] = true;
if (spec.MinimumValue.has_value())
jsonDoc["minimumValue"] = spec.MinimumValue.value();
if (spec.MaximumValue.has_value())
jsonDoc["maximumValue"] = spec.MaximumValue.value();
if (spec.EmptyAllowed.has_value())
jsonDoc["emptyAllowed"] = spec.EmptyAllowed.value();
switch (spec.Access)
{
case SettingSpec::SettingAccess::ReadOnly:
jsonDoc["readOnly"] = true;
break;
case SettingSpec::SettingAccess::WriteOnly:
jsonDoc["writeOnly"] = true;
break;
default:
// Default is read/write, so we don't need to specify that
break;
}
if (jsonDoc.overflowed())
debugE("JSON buffer overflow while serializing SettingSpec - object incomplete!");
if (!specObject.set(jsonDoc.as<JsonObjectConst>()))
{
bufferOverflow = true;
jsonBufferSize += JSON_BUFFER_INCREMENT;
debugV("JSON response buffer overflow! Increased buffer to %zu bytes", jsonBufferSize);
break;
}
}
if (!bufferOverflow)
AddCORSHeaderAndSendResponse(pRequest, response.release());
} while (bufferOverflow);
}
const std::vector<std::reference_wrapper<SettingSpec>> & CWebServer::LoadDeviceSettingSpecs()
{
if (deviceSettingSpecs.empty())
{
mySettingSpecs.emplace_back(
"effectInterval",
"Effect interval",
"The duration in milliseconds that an individual effect runs, before the next effect is activated.",
SettingSpec::SettingType::PositiveBigInteger
);
deviceSettingSpecs.insert(deviceSettingSpecs.end(), mySettingSpecs.begin(), mySettingSpecs.end());
auto deviceConfigSpecs = g_ptrSystem->DeviceConfig().GetSettingSpecs();
deviceSettingSpecs.insert(deviceSettingSpecs.end(), deviceConfigSpecs.begin(), deviceConfigSpecs.end());
}
return deviceSettingSpecs;
}
void CWebServer::GetSettingSpecs(AsyncWebServerRequest * pRequest)
{
SendSettingSpecsResponse(pRequest, LoadDeviceSettingSpecs());
}
// Responds with current config, excluding any sensitive values
void CWebServer::GetSettings(AsyncWebServerRequest * pRequest)
{
debugV("GetSettings");
auto response = new AsyncJsonResponse(false, JSON_BUFFER_BASE_SIZE);
response->addHeader("Server", "NightDriverStrip");
auto root = response->getRoot();
JsonObject jsonObject = root.to<JsonObject>();
// We get the serialized JSON for the device config, without any sensitive values
g_ptrSystem->DeviceConfig().SerializeToJSON(jsonObject, false);
jsonObject["effectInterval"] = g_ptrSystem->EffectManager().GetInterval();
AddCORSHeaderAndSendResponse(pRequest, response);
}
// Support function that silently sets whatever settings are included in the request passed.
// Composing a response is left to the invoker!
void CWebServer::SetSettingsIfPresent(AsyncWebServerRequest * pRequest)
{
auto& deviceConfig = g_ptrSystem->DeviceConfig();
auto& effectManager = g_ptrSystem->EffectManager();
PushPostParamIfPresent<size_t>(pRequest,"effectInterval", SET_VALUE(effectManager.SetInterval(value)));
PushPostParamIfPresent<String>(pRequest, DeviceConfig::HostnameTag, SET_VALUE(deviceConfig.SetHostname(value)));
PushPostParamIfPresent<String>(pRequest, DeviceConfig::LocationTag, SET_VALUE(deviceConfig.SetLocation(value)));
PushPostParamIfPresent<bool>(pRequest, DeviceConfig::LocationIsZipTag, SET_VALUE(deviceConfig.SetLocationIsZip(value)));
PushPostParamIfPresent<String>(pRequest, DeviceConfig::CountryCodeTag, SET_VALUE(deviceConfig.SetCountryCode(value)));
PushPostParamIfPresent<String>(pRequest, DeviceConfig::OpenWeatherApiKeyTag, SET_VALUE(deviceConfig.SetOpenWeatherAPIKey(value)));
PushPostParamIfPresent<String>(pRequest, DeviceConfig::TimeZoneTag, SET_VALUE(deviceConfig.SetTimeZone(value)));
PushPostParamIfPresent<bool>(pRequest, DeviceConfig::Use24HourClockTag, SET_VALUE(deviceConfig.Set24HourClock(value)));
PushPostParamIfPresent<bool>(pRequest, DeviceConfig::UseCelsiusTag, SET_VALUE(deviceConfig.SetUseCelsius(value)));
PushPostParamIfPresent<String>(pRequest, DeviceConfig::NTPServerTag, SET_VALUE(deviceConfig.SetNTPServer(value)));
PushPostParamIfPresent<bool>(pRequest, DeviceConfig::RememberCurrentEffectTag, SET_VALUE(deviceConfig.SetRememberCurrentEffect(value)));
PushPostParamIfPresent<int>(pRequest, DeviceConfig::PowerLimitTag, SET_VALUE(deviceConfig.SetPowerLimit(value)));
PushPostParamIfPresent<int>(pRequest, DeviceConfig::BrightnessTag, SET_VALUE(deviceConfig.SetBrightness(value)));
#if SHOW_VU_METER
PushPostParamIfPresent<bool>(pRequest, DeviceConfig::ShowVUMeterTag, SET_VALUE(effectManager.ShowVU(value)));
#endif
std::optional<CRGB> globalColor = {};
std::optional<CRGB> secondColor = {};
PushPostParamIfPresent<CRGB>(pRequest, DeviceConfig::GlobalColorTag, SET_VALUE(globalColor = value));
PushPostParamIfPresent<CRGB>(pRequest, DeviceConfig::SecondColorTag, SET_VALUE(secondColor = value));
deviceConfig.ApplyColorSettings(globalColor, secondColor,
IsPostParamTrue(pRequest, DeviceConfig::ClearGlobalColorTag),
IsPostParamTrue(pRequest, DeviceConfig::ApplyGlobalColorsTag));
}
// Set settings and return resulting config
void CWebServer::SetSettings(AsyncWebServerRequest * pRequest)
{
debugV("SetSettings");
SetSettingsIfPresent(pRequest);
// We return the current config in response
GetSettings(pRequest);
}
bool CWebServer::CheckAndGetSettingsEffect(AsyncWebServerRequest * pRequest, std::shared_ptr<LEDStripEffect> & effect, bool post)
{
auto effectsList = g_ptrSystem->EffectManager().EffectsList();
auto effectIndex = GetEffectIndexFromParam(pRequest, post);
if (effectIndex < 0 || effectIndex >= effectsList.size())
{
AddCORSHeaderAndSendOKResponse(pRequest);
return false;
}
effect = effectsList[effectIndex];
return true;
}
void CWebServer::GetEffectSettingSpecs(AsyncWebServerRequest * pRequest)
{
std::shared_ptr<LEDStripEffect> effect;
if (!CheckAndGetSettingsEffect(pRequest, effect))
return;
auto settingSpecs = effect->GetSettingSpecs();
SendSettingSpecsResponse(pRequest, settingSpecs);
}
void CWebServer::SendEffectSettingsResponse(AsyncWebServerRequest * pRequest, std::shared_ptr<LEDStripEffect> & effect)
{
static size_t jsonBufferSize = JSON_BUFFER_BASE_SIZE;
do
{
auto response = std::make_unique<AsyncJsonResponse>(false, jsonBufferSize);
auto jsonObject = response->getRoot().to<JsonObject>();
if (effect->SerializeSettingsToJSON(jsonObject))
{
AddCORSHeaderAndSendResponse(pRequest, response.release());
return;
}
jsonBufferSize += JSON_BUFFER_INCREMENT;
debugV("JSON response buffer overflow! Increased buffer to %zu bytes", jsonBufferSize);
} while (true);
}
void CWebServer::GetEffectSettings(AsyncWebServerRequest * pRequest)
{
debugV("GetEffectSettings");
std::shared_ptr<LEDStripEffect> effect;
if (!CheckAndGetSettingsEffect(pRequest, effect))
return;
SendEffectSettingsResponse(pRequest, effect);
}
bool CWebServer::ApplyEffectSettings(AsyncWebServerRequest * pRequest, std::shared_ptr<LEDStripEffect> & effect)
{
bool settingChanged = false;
for (auto& settingSpecWrapper : effect->GetSettingSpecs())
{
const String& settingName = settingSpecWrapper.get().Name;
settingChanged = PushPostParamIfPresent<String>(pRequest, settingName, [&](auto value) { return effect->SetSetting(settingName, value); })
|| settingChanged;
}
return settingChanged;
}
void CWebServer::SetEffectSettings(AsyncWebServerRequest * pRequest)
{
debugV("SetEffectSettings");
std::shared_ptr<LEDStripEffect> effect;
if (!CheckAndGetSettingsEffect(pRequest, effect, true))
return;
if (ApplyEffectSettings(pRequest, effect))
SaveEffectManagerConfig();
SendEffectSettingsResponse(pRequest, effect);
}
// Validate and set one setting. If no validator is available in settingValidators for the setting, validation is skipped.
// Requests containing more than one known setting are malformed and rejected.
void CWebServer::ValidateAndSetSetting(AsyncWebServerRequest * pRequest)
{
String paramName;
for (auto& settingSpecWrapper : LoadDeviceSettingSpecs())
{
auto& settingSpec = settingSpecWrapper.get();
if (pRequest->hasParam(settingSpec.Name, true))
{
if (paramName.isEmpty())
paramName = settingSpec.Name;
else
// We found multiple known settings in the request, which we don't allow
{
AddCORSHeaderAndSendBadRequest(pRequest, "Malformed request");
return;
}
}
}
// No known setting in the request, so we can stop processing and go on with our business
if (paramName.isEmpty())
{
AddCORSHeaderAndSendOKResponse(pRequest);
return;
}
auto validator = settingValidators.find(paramName);
if (validator != settingValidators.end())
{
const String ¶mValue = pRequest->getParam(paramName, true)->value();
bool isValid;
String validationMessage;
std::tie(isValid, validationMessage) = validator->second(paramValue);
if (!isValid)
{
AddCORSHeaderAndSendBadRequest(pRequest, validationMessage);
return;
}
}
// Process the setting as per usual
SetSettingsIfPresent(pRequest);
AddCORSHeaderAndSendOKResponse(pRequest);
}
// Reset effect config, device config and/or the board itself
void CWebServer::Reset(AsyncWebServerRequest * pRequest)
{
bool boardResetRequested = IsPostParamTrue(pRequest, "board");
bool deviceConfigResetRequested = IsPostParamTrue(pRequest, "deviceConfig");
bool effectsConfigResetRequested = IsPostParamTrue(pRequest, "effectsConfig");
// We can now let the requester know we're taking care of things without making them wait longer
AddCORSHeaderAndSendOKResponse(pRequest);
if (boardResetRequested)
{
// Flush any pending writes and make sure nothing is written after. We do this to make sure
// that what needs saving is written, but no further writes take place after any requested
// config resets have happened.
g_ptrSystem->JSONWriter().FlushWrites(true);
// Give the device a few seconds to finish the requested writes - this also gives AsyncWebServer
// time to push out the response to the request before the device resets
delay(3000);
}
if (deviceConfigResetRequested)
{
debugI("Removing DeviceConfig");
g_ptrSystem->DeviceConfig().RemovePersisted();
}
if (effectsConfigResetRequested)
{
debugI("Removing EffectManager config");
RemoveEffectManagerConfig();
}
if (boardResetRequested)
{
debugW("Resetting device at API request!");
throw std::runtime_error("Resetting device at API request");
}
}