From 458fb7d936ec9d266cfce275f999dd629fb82e2f Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 2 Mar 2021 18:49:50 +0300 Subject: [PATCH] webui: send alert messages directly --- code/espurna/nofuss.cpp | 6 +-- code/espurna/ota_arduinoota.cpp | 6 +-- code/espurna/sensor.cpp | 8 ---- code/espurna/web.h | 6 +-- code/espurna/ws.cpp | 30 +++++++------- code/html/custom.js | 72 +++++++++++---------------------- code/html/index.html | 19 --------- 7 files changed, 48 insertions(+), 99 deletions(-) diff --git a/code/espurna/nofuss.cpp b/code/espurna/nofuss.cpp index f382e873..fcfbf638 100644 --- a/code/espurna/nofuss.cpp +++ b/code/espurna/nofuss.cpp @@ -132,9 +132,9 @@ void nofussSetup() { DEBUG_MSG_P(PSTR(" New version: %s\n"), (char *) NoFUSSClient.getNewVersion().c_str()); DEBUG_MSG_P(PSTR(" Firmware: %s\n"), (char *) NoFUSSClient.getNewFirmware().c_str()); DEBUG_MSG_P(PSTR(" File System: %s\n"), (char *) NoFUSSClient.getNewFileSystem().c_str()); - #if WEB_SUPPORT - wsSend_P(PSTR("{\"message\": 1}")); - #endif +#if WEB_SUPPORT + wsSend_P(PSTR("{\"message\": \"Automatic OTA started.\"}")); +#endif // Disabling EEPROM rotation to prevent writing to EEPROM after the upgrade eepromRotate(false); diff --git a/code/espurna/ota_arduinoota.cpp b/code/espurna/ota_arduinoota.cpp index 9e1987fd..3491df3d 100644 --- a/code/espurna/ota_arduinoota.cpp +++ b/code/espurna/ota_arduinoota.cpp @@ -42,9 +42,9 @@ void _arduinoOtaOnStart() { DEBUG_MSG_P(PSTR("[OTA] Start\n")); - #if WEB_SUPPORT - wsSend_P(PSTR("{\"message\": 2}")); - #endif +#if WEB_SUPPORT + wsSend_P(PSTR("{\"message\": \"OTA update started.\"}")); +#endif } diff --git a/code/espurna/sensor.cpp b/code/espurna/sensor.cpp index 6f1b247a..7a851f1c 100644 --- a/code/espurna/sensor.cpp +++ b/code/espurna/sensor.cpp @@ -1310,14 +1310,6 @@ void _sensorWebSocketOnVisible(JsonObject& root) { void _sensorWebSocketMagnitudesConfig(JsonObject& root) { - // retrieve per-type ...Correction settings, when available - _magnitudeForEachCounted([&root](unsigned char type) { - if (_magnitudeCanUseCorrection(type)) { - auto key = String(_magnitudeSettingsPrefix(type)) + F("Correction"); - root[key] = getSetting(key, _magnitudeCorrection(type)); - } - }); - JsonObject& magnitudes = root.createNestedObject("magnitudesConfig"); uint8_t size = 0; diff --git a/code/espurna/web.h b/code/espurna/web.h index 671a3d99..e5128961 100644 --- a/code/espurna/web.h +++ b/code/espurna/web.h @@ -20,9 +20,9 @@ Copyright (C) 2016-2019 by Xose PĂ©rez struct AsyncWebPrintConfig { const char* const mimeType; - const size_t backlogCountMax; - const size_t backlogSizeMax; - const decltype(millis()) backlogTimeout; + size_t backlogCountMax; + size_t backlogSizeMax; + decltype(millis()) backlogTimeout; }; struct AsyncWebPrint : public Print { diff --git a/code/espurna/ws.cpp b/code/espurna/ws.cpp index 85bf05d5..670b9522 100644 --- a/code/espurna/ws.cpp +++ b/code/espurna/ws.cpp @@ -330,7 +330,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { JsonObject& root = jsonBuffer.parseObject((char *) payload); if (!root.success()) { DEBUG_MSG_P(PSTR("[WEBSOCKET] JSON parsing error\n")); - wsSend_P(client_id, PSTR("{\"message\": 3}")); + wsSend_P(client_id, PSTR("{\"message\": \"Cannot parse the data!\"}")); return; } @@ -366,9 +366,9 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { if (data.success()) { if (strcmp(action, "restore") == 0) { if (settingsRestoreJson(data)) { - wsSend_P(client_id, PSTR("{\"message\": 5}")); + wsSend_P(client_id, PSTR("{\"message\": \"Changes saved, you should be able to reboot now.\"}")); } else { - wsSend_P(client_id, PSTR("{\"message\": 4}")); + wsSend_P(client_id, PSTR("{\"message\": \"Could not restore the configuration, see the debug log for more information.\"}")); } return; } @@ -407,7 +407,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { wsSend_P(client_id, PSTR("{\"action\": \"reload\"}")); } } else { - wsSend_P(client_id, PSTR("{\"message\": 7}")); + wsSend_P(client_id, PSTR("{\"message\": \"Passwords do not match!\"}")); } continue; } @@ -445,11 +445,11 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { // Persist settings saveSettings(); - wsSend_P(client_id, PSTR("{\"message\": 8}")); + wsSend_P(client_id, PSTR("{\"saved\": true, \"message\": \"Changes saved.\"}")); } else { - wsSend_P(client_id, PSTR("{\"message\": 9}")); + wsSend_P(client_id, PSTR("{\"message\": \"No changes detected.\"}")); } @@ -516,17 +516,17 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy if (type == WS_EVT_CONNECT) { client->_tempObject = nullptr; + IPAddress ip = client->remoteIP(); - #ifndef NOWSAUTH - if (!_wsAuth(client)) { - wsSend_P(client->id(), PSTR("{\"message\": 10}")); - DEBUG_MSG_P(PSTR("[WEBSOCKET] Validation check failed\n")); - client->close(); - return; - } - #endif +#ifndef NOWSAUTH + if (!_wsAuth(client)) { + wsSend_P(client->id(), PSTR("{\"action\": \"reload\", \"message\": \"Session expired.\"}")); + DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u session expired, ip: %d.%d.%d.%d\n"), client->id(), ip[0], ip[1], ip[2], ip[3]); + client->close(); + return; + } +#endif - IPAddress ip = client->remoteIP(); DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n"), client->id(), ip[0], ip[1], ip[2], ip[3], server->url()); _wsConnected(client->id()); _wsResetUpdateTimer(); diff --git a/code/html/custom.js b/code/html/custom.js index 96c68e03..9ad4c1b1 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -2,7 +2,6 @@ var debug = false; var websock; var password = false; var maxNetworks; -var messages = []; var free_size = 0; var urls = {}; @@ -36,22 +35,6 @@ var MagnitudeTypePrefixes = {}; var MagnitudePrefixTypes = {}; //endRemoveIf(!sensor) -// ----------------------------------------------------------------------------- -// Messages -// ----------------------------------------------------------------------------- - -function initMessages() { - messages[1] = "Remote update started"; - messages[2] = "OTA update started"; - messages[3] = "Error parsing data!"; - messages[4] = "The file does not look like a valid configuration backup or is corrupted"; - messages[5] = "Changes saved. You should reboot your board now"; - messages[7] = "Passwords do not match!"; - messages[8] = "Changes saved"; - messages[9] = "No changes detected"; - messages[10] = "Session expired, please reload page..."; -} - // ----------------------------------------------------------------------------- // Utils // ----------------------------------------------------------------------------- @@ -720,21 +703,22 @@ function waitForSave(){ function doUpdate() { + // Since we have 2-page config, make sure we select the active one var forms = $(".form-settings"); if (validateForm(forms)) { - // Get data sendConfig(getData(forms)); - // Empty special fields +//removeIf(!sensor) + // Energy reset is handled via these keys + // TODO: replace these with actions, not settings $(".pwrExpected").val(0); $("input[name='snsResetCalibration']").prop("checked", false); $("input[name='pwrResetCalibration']").prop("checked", false); $("input[name='pwrResetE']").prop("checked", false); +//endRemoveIf(!sensor) - // Change handling numChanged = 0; - waitForSave(); } @@ -1619,7 +1603,9 @@ function processData(data) { // --------------------------------------------------------------------- if ("action" === key) { - if ("reload" === data.action) { doReload(1000); } + if ("reload" === data.action) { + doReload(1000); + } return; } @@ -1864,23 +1850,6 @@ function processData(data) { //removeIf(!sensor) - { - var position = key.indexOf("Correction"); - if (position > 0 && position === key.length - 10) { - var template = $("#magnitudeCorrectionTemplate > div")[0]; - var elem = $(template).clone(); - - var prefix = key.slice(0, position); - $("label", elem).html(MagnitudeNames[MagnitudePrefixTypes[prefix]]); - $("input", elem).attr("name", key).val(value); - - setOriginalsFromValues($("input", elem)); - elem.appendTo("#magnitude-corrections"); - moduleVisible("magnitude-corrections"); - return; - } - } - if ("snsErrors" === key) { for (var index in value) { var type = value[index][0]; @@ -2088,12 +2057,13 @@ function processData(data) { // General // --------------------------------------------------------------------- - // Messages + if ("saved" === key) { + configurationSaved = value; + return; + } + if ("message" === key) { - if (value == 8) { - configurationSaved = true; - } - window.alert(messages[value]); + window.alert(value); return; } @@ -2298,10 +2268,17 @@ function connectToURL(url) { if (websock) { websock.close(); } websock = new WebSocket(urls.ws.href); websock.onmessage = function(evt) { - var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t")); - if (data) { - processData(data); + var data = {}; + try { + data = JSON.parse(evt.data + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/\t/g, "\\t")); + } catch (e) { + console.log(e); } + + processData(data); }; websock.onclose = function(evt) { clearInterval(ws_pingpong); @@ -2333,7 +2310,6 @@ function connectToCurrentURL() { $(function() { - initMessages(); createCheckboxes(); setInterval(function() { keepTime(); }, 1000); diff --git a/code/html/index.html b/code/html/index.html index 5f7f78d6..0963f4ca 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -1813,15 +1813,6 @@
Move this switch to ON and press "Save" to set energy count to 0.
-
- Value corrections -
- Correction value is added to the measured value which may be inaccurate due to many factors. The value can be negative. -
-
-
-
- @@ -1917,16 +1908,6 @@ -
-
- - -
-
-
-
-
-
LED #