X-Git-Url: https://git.rvb.name/esp-clock.git/blobdiff_plain/7fff81f521f6b9d6f277d972f59411452fb4a677..e6a4483ee334f8a5879b55ecc8da6255ad565d41:/web.cpp?ds=sidebyside diff --git a/web.cpp b/web.cpp index 7b7f1bc..13e7916 100644 --- a/web.cpp +++ b/web.cpp @@ -3,6 +3,8 @@ #include #include #include +#include "ArduinoJson.h" +#include "AsyncJson.h" bool isApEnabled = false; bool isWebStarted = false; @@ -40,18 +42,24 @@ void reportChange(const __FlashStringHelper* name) { } } +void reportMessage(const __FlashStringHelper* msg) { + char buf[256]; + strcpy_P(buf, (PGM_P)msg); + events.send(buf,"message",millis()); +} + void sendInitial(AsyncEventSourceClient *client) { String mac = WiFi.macAddress(); char buf[256]; sprintf(buf,"{\"_mac\":\"%s\",\"_weather\":\"%s\"}", mac.c_str(), weatherData); - client->send(buf,"keepalive",millis()); + client->send(buf,"update",millis()); mac = String(); } void sendWeather() { char buf[256]; sprintf(buf,"{\"_weather\":\"%s\"}",weatherData); - events.send(buf,"keepalive",millis()); + events.send(buf,"update",millis()); } void sendKeepalive() { @@ -86,7 +94,7 @@ void sendKeepalive() { } else { sprintf(buf,"{\"_uptime\":\"%d м %d с\", \"_date\":\"%02d.%2d.%04d\", \"_time\":\"%02d:%02d\",\"_heap\":\"%d б\", \"_rssi\":\"%d\", \"_last_sync\":\"%s\", \"_changed\":%s}", mins, uptime, dd, mm, yy, hh, mi, heap, rssi, sync, changed?"true":"false"); } - events.send(buf,"keepalive",millis()); + events.send(buf,"update",millis()); } void apply(const char* name) { @@ -138,7 +146,11 @@ void setupWeb() { } if(request->hasParam("name")) { const char* action = request->getParam("name")->value().c_str(); - if (strcmp(action,"restart") == 0) { + if (strcmp(action,"reset") == 0) { + millisScheduled = millis(); + actionScheduled = "reset"; + request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Сбрасываю настройки и перезагружаюсь\"}"); + } else if (strcmp(action,"restart") == 0) { if (pendingWiFi) { request->send(200,"application/json", "{\"result\":\"FAILED\",\"message\":\"Не применены настройки WiFi\", \"page\":\"wifi\"}"); } else if (pendingAuth) { @@ -170,7 +182,26 @@ void setupWeb() { millisScheduled = millis(); actionScheduled = "save"; } + } else if (strcmp(action,"time") == 0) { + if(request->hasParam("timestamp")) { + unsigned long timestamp = atoi(request->getParam("timestamp")->value().c_str()); + if (timestamp) { + timeval tv = { timestamp, 0 }; + settimeofday(&tv, nullptr); + if (isRTCEnabled) { + Serial.println(F("Время установлено вручную")); + RTC.adjust(DateTime(timestamp)); + } + } + request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Устанавливаю время\"}"); + } else { + request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Not all parameters set\"}"); + } + } else { + request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Unsupported action\"}"); } + } else { + request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Not all parameters set\"}"); } }); @@ -277,6 +308,42 @@ void setupWeb() { } }); + AsyncCallbackJsonWebHandler* configUploadHandler = new AsyncCallbackJsonWebHandler("/config/put", [](AsyncWebServerRequest *request, JsonVariant &json) { + cfg.clear(); + // first - set values + for( JsonPair kv : json.as() ) { + const char* name = kv.key().c_str(); + if (kv.value().is()) { + cfg.setValue(name, kv.value().as()); + } else if (kv.value().is()) { + cfg.setValue(name, kv.value().as()); + } else if (kv.value().is()) { + cfg.setValue(name, kv.value().as()); + } else if (kv.value().is()) { + cfg.setValue(name, kv.value().as()); + } else { + Serial.print(F("Неопознанный тип значения параметра ")); Serial.print(name); Serial.print(": "); Serial.println(kv.value().as().c_str()); + cfg.clear(); + setupConfig(); + request->send(500, "text/plain", "Unknown parameter type"); + } + } + // second - handle all changes + for( JsonPair kv : json.as() ) { + apply(kv.key().c_str()); + } + pendingWiFi = false; + pendingAuth = false; + saveConfig(); + message(F("Применены сохраненные настройки"),5); + reportMessage(F("Применены сохраненные настройки")); + millisScheduled = millis() + 10000; + actionScheduled = "restart"; + request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Настройки восстановлены из резервной копии\"}"); + }); + + server.addHandler(configUploadHandler).setAuthentication(auth_user,auth_pwd); + server.serveStatic("ui", LittleFS, "/ui.json").setAuthentication(auth_user,auth_pwd); server.serveStatic("/", LittleFS, "/web/").setDefaultFile("index.html").setAuthentication(auth_user,auth_pwd); @@ -309,7 +376,10 @@ void tickWeb() { if (actionScheduled && millis()>millisScheduled+300) { Serial.print(F("Запланированная операция ")); Serial.println(actionScheduled); // - if (strcmp(actionScheduled,"restart") == 0) { + if (strcmp(actionScheduled,"reset") == 0) { + server.end(); + reset(); + } else if (strcmp(actionScheduled,"restart") == 0) { server.end(); reboot(); } else if (strcmp(actionScheduled,"auth") == 0) { @@ -348,6 +418,7 @@ void tickWeb() { if (!pendingWiFi && !pendingAuth && cfg.getTimestamp() && cfg.getTimestamp() < now - CFG_AUTOSAVE) { saveConfig(); + reportMessage(F("Настройки сохранены")); Serial.println(F("Настройки сохранены")); } }