Browse Source

webui: send alert messages directly

dev
Maxim Prokhorov 3 years ago
parent
commit
458fb7d936
7 changed files with 48 additions and 99 deletions
  1. +3
    -3
      code/espurna/nofuss.cpp
  2. +3
    -3
      code/espurna/ota_arduinoota.cpp
  3. +0
    -8
      code/espurna/sensor.cpp
  4. +3
    -3
      code/espurna/web.h
  5. +15
    -15
      code/espurna/ws.cpp
  6. +24
    -48
      code/html/custom.js
  7. +0
    -19
      code/html/index.html

+ 3
- 3
code/espurna/nofuss.cpp View File

@ -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);


+ 3
- 3
code/espurna/ota_arduinoota.cpp View File

@ -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
}


+ 0
- 8
code/espurna/sensor.cpp View File

@ -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;


+ 3
- 3
code/espurna/web.h View File

@ -20,9 +20,9 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
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 {


+ 15
- 15
code/espurna/ws.cpp View File

@ -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();


+ 24
- 48
code/html/custom.js View File

@ -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);


+ 0
- 19
code/html/index.html View File

@ -1813,15 +1813,6 @@
<div class="pure-u-1 pure-u-lg-3-4 hint">Move this switch to ON and press "Save" to set energy count to 0.</div>
</div>
<div class="pure-g module module-magnitude-corrections">
<legend>Value corrections</legend>
<div class="pure-u-1 pure-u-lg-3-4 hint more">
Correction value is added to the measured value which may be inaccurate due to many factors. The value can be negative.
</div>
<div class="pure-form pure-form-aligned" id="magnitude-corrections">
</div>
</div>
</fieldset>
</div>
@ -1917,16 +1908,6 @@
<!-- Templates -->
<div id="magnitudeCorrectionTemplate" class="template">
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4"></label>
<input name="correction" class="pure-u-1 pure-u-lg-1-4" type="number" step="0.1" data="0"></input>
<div class="pure-u-0 pure-u-lg-1-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint"></div>
</div>
</div>
<div id="ledConfigTemplate" class="template">
<div class="pure-form pure-form-aligned">
<legend class="pure-u-1 pure-u-lg-1-4">LED #<span class="id"></span></legend>


Loading…
Cancel
Save