From b00405e5b7c1b87c5a939b0eaa4d5eb7fad37a48 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 2 Oct 2019 19:20:55 +0200 Subject: [PATCH] added command log_events --- CHANGELOG.md | 2 ++ src/MyESP.cpp | 65 +++++++++++++++++++++++++++++-------- src/MyESP.h | 5 +-- src/custom.htm | 2 +- src/version.h | 2 +- src/websrc/myesp.htm | 50 ++++++++++++++++++++++++++-- src/websrc/myesp.js | 50 ++++++++++++++++++++++++++-- tools/wsemulator/wserver.js | 3 +- 8 files changed, 155 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6edaacd00..8cd36dc06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - new `mqttlog` command also shows which MQTT topics it is subscribed too - Optimized event log loading in web and added integrity checks on all config and log files during boot - `autodetect quick` +- `log_events` option, now optional to save the log events to SPIFFS ### Fixed @@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Faster detection of EMS devices on bus by using the 0x07 telegram instead of the brute-force scan - Fixes to the default HA climate component .yaml file to support latest Home Assistance ('heat' added) - Update documentation in Wiki on MQTT and troubleshooting +- Slowed down firmware upload via the Web to prevent users rebooting too early ### Removed diff --git a/src/MyESP.cpp b/src/MyESP.cpp index 40e2480d6..ad17db3c3 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -46,6 +46,7 @@ MyESP::MyESP() { _ota_post_callback_f = nullptr; _load_average = 100; // calculated load average _general_serial = true; // serial is set to on as default + _general_log_events = true; // all logs are written to an event log in SPIFFS _have_ntp_time = false; // telnet @@ -665,6 +666,7 @@ void MyESP::_printSetCommands() { myDebug_P(PSTR(" set mqtt_port [value]")); myDebug_P(PSTR(" set ntp_enabled ")); myDebug_P(PSTR(" set serial ")); + myDebug_P(PSTR(" set log_events ")); // call callback function if (_telnet_callback_f) { @@ -722,7 +724,9 @@ void MyESP::_printSetCommands() { #else myDebug_P(PSTR(" serial=%s"), (_general_serial) ? "on" : "off"); #endif + myDebug_P(PSTR(" ntp_enabled=%s"), (_ntp_enabled) ? "on" : "off"); + myDebug_P(PSTR(" log_events=%s"), (_general_log_events) ? "on" : "off"); // print any custom settings if (_fs_setlist_callback_f) { @@ -887,6 +891,21 @@ bool MyESP::_changeSetting(uint8_t wc, const char * setting, const char * value) save_config = false; } } + } else if (strcmp(setting, "log_events") == 0) { + save_config = true; + if (value) { + if (strcmp(value, "on") == 0) { + _general_log_events = true; + save_config = true; + myDebug_P(PSTR("Event logging on")); + } else if (strcmp(value, "off") == 0) { + _general_log_events = false; + save_config = true; + myDebug_P(PSTR("Event logging off")); + } else { + save_config = false; + } + } } else { // finally check for any custom commands if (_fs_setlist_callback_f) { @@ -1599,9 +1618,14 @@ bool MyESP::_fs_validateConfigFile(const char * filename, size_t maxsize, JsonDo return true; } -// validates a log file in SPIFFS +// validates the event log file in SPIFFS // returns true if all OK bool MyESP::_fs_validateLogFile(const char * filename) { + // exit if we have disabled logging + if (!_general_log_events) { + return true; + } + // see if we can open it File eventlog = SPIFFS.open(filename, "r"); if (!eventlog) { @@ -1726,7 +1750,8 @@ bool MyESP::_fs_loadConfig() { JsonObject general = doc["general"]; _general_password = strdup(general["password"] | MYESP_HTTP_PASSWORD); _ws->setAuthentication("admin", _general_password); - _general_hostname = strdup(general["hostname"]); + _general_hostname = strdup(general["hostname"]); + _general_log_events = general["log_events"] | false; // default is off // serial is only on when booting #ifdef FORCE_SERIAL @@ -1808,7 +1833,10 @@ bool MyESP::fs_saveCustomConfig(JsonObject root) { } */ - _writeEvent("INFO", "system", "Custom config stored in the SPIFFS", ""); + if (_general_log_events) { + _writeEvent("INFO", "system", "Custom config stored in the SPIFFS", ""); + } + myDebug_P(PSTR("[FS] custom config saved")); ok = true; } @@ -1841,7 +1869,9 @@ bool MyESP::fs_saveConfig(JsonObject root) { configFile.close(); if (n) { - _writeEvent("INFO", "system", "System config stored in the SPIFFS", ""); + if (_general_log_events) { + _writeEvent("INFO", "system", "System config stored in the SPIFFS", ""); + } myDebug_P(PSTR("[FS] system config saved")); ok = true; } @@ -1924,7 +1954,9 @@ void MyESP::_fs_setup() { if (!SPIFFS.begin()) { myDebug_P(PSTR("[FS] Formatting filesystem...")); if (SPIFFS.format()) { - _writeEvent("WARN", "system", "File system formatted", ""); + if (_general_log_events) { + _writeEvent("WARN", "system", "File system formatted", ""); + } } else { myDebug_P(PSTR("[FS] Failed to format file system")); } @@ -1959,7 +1991,9 @@ void MyESP::_fs_setup() { } else { myDebug_P(PSTR("[FS] Resetting event log")); SPIFFS.remove(MYESP_EVENTLOG_FILE); - _writeEvent("WARN", "system", "Event Log", "Log was reset due to corruption somewhere"); + if (_general_log_events) { + _writeEvent("WARN", "system", "Event Log", "Log was erased due to probable file corruption"); + } } if (_ota_post_callback_f) { @@ -2184,6 +2218,7 @@ void MyESP::crashInfo() { #endif // write a log entry to SPIFFS +// assumes we have "log_events" on void MyESP::_writeEvent(const char * type, const char * src, const char * desc, const char * data) { // this will also create the file if its doesn't exist File eventlog = SPIFFS.open(MYESP_EVENTLOG_FILE, "a"); @@ -2617,8 +2652,9 @@ void MyESP::_webserver_setup() { return; } if (!index) { + ETS_UART_INTR_DISABLE(); // disable all UART interrupts to be safe _writeEvent("INFO", "system", "Firmware update started", ""); - //Serial.printf("[SYSTEM] Firmware update started: %s\n", filename.c_str()); // enable for debugging + //Serial.printf("[SYSTEM] Firmware update started: %s\n", filename.c_str()); // enable for debugging XXX Update.runAsync(true); if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) { _writeEvent("ERRO", "system", "Not enough space to update", ""); @@ -2634,7 +2670,6 @@ void MyESP::_webserver_setup() { if (final) { if (Update.end(true)) { _writeEvent("INFO", "system", "Firmware update finished", ""); - //Serial.printf("[SYSTEM] Firmware update finished: %uB\n", index + len); // enable for debugging _shouldRestart = !Update.hasError(); } else { _writeEvent("ERRO", "system", "Firmware update failed", ""); @@ -2682,11 +2717,9 @@ void MyESP::_webserver_setup() { //static String remoteIP = (String)address[0] + "." + (String)address[1] + "." + (String)address[2] + "." + (String)address[3]; if (!request->authenticate(MYESP_HTTP_USERNAME, _general_password)) { - //_writeEvent("WARN", "system", "New login attempt", remoteIP); return request->requestAuthentication(); } request->send(200, "text/plain", "Success"); - // _writeEvent("INFO", "system", "Login successful", remoteIP); }); _webServer->rewrite("/", "/index.html"); @@ -2799,7 +2832,9 @@ void MyESP::_bootupSequence() { if (boot_status == MYESP_BOOTSTATUS_BOOTED) { if ((_ntp_enabled) && (now() > 10000) && !_have_ntp_time) { _have_ntp_time = true; - _writeEvent("INFO", "system", "System booted", ""); + if (_general_log_events) { + _writeEvent("INFO", "system", "System booted", ""); + } } return; } @@ -2829,7 +2864,9 @@ void MyESP::_bootupSequence() { // write a log message if we're not using NTP, otherwise wait for the internet time to arrive if (!_ntp_enabled) { - _writeEvent("INFO", "system", "System booted", ""); + if (_general_log_events) { + _writeEvent("INFO", "system", "System booted", ""); + } } } } @@ -2898,7 +2935,9 @@ void MyESP::loop() { } if (_shouldRestart) { - _writeEvent("INFO", "system", "System is restarting", ""); + if (_general_log_events) { + _writeEvent("INFO", "system", "System is restarting", ""); + } myDebug("[SYSTEM] Restarting..."); _deferredReset(500, CUSTOM_RESET_TERMINAL); ESP.restart(); diff --git a/src/MyESP.h b/src/MyESP.h index d01e72cfc..fbef5b9e3 100644 --- a/src/MyESP.h +++ b/src/MyESP.h @@ -400,11 +400,12 @@ class MyESP { char * _app_updateurl; bool _suspendOutput; bool _general_serial; - unsigned long _getUptime(); - char * _getBuildTime(); + bool _general_log_events; char * _buildTime; bool _timerequest; bool _formatreq; + unsigned long _getUptime(); + char * _getBuildTime(); bool _hasValue(char * s); void _printHeap(const char * s); diff --git a/src/custom.htm b/src/custom.htm index 00a652e82..d7ca2187c 100644 --- a/src/custom.htm +++ b/src/custom.htm @@ -130,7 +130,7 @@ diff --git a/src/version.h b/src/version.h index ad562b687..9ef083cc1 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define APP_VERSION "1.9.1b9" +#define APP_VERSION "1.9.1b10" diff --git a/src/websrc/myesp.htm b/src/websrc/myesp.htm index 41f36fabe..c741d7a43 100644 --- a/src/websrc/myesp.htm +++ b/src/websrc/myesp.htm @@ -53,6 +53,30 @@ +
+
+
+
+
+
+
+
+
+

Please wait for about a minute while the firmware uploads...

+
+
+
+
0% +
+
+
+
+
+
+
+
+

General Settings @@ -90,6 +114,21 @@

+ + +
+ +
+
+ + +
+
+

@@ -112,6 +151,8 @@
Event Log
Dates shown in () represent elapsed time in seconds when NTP Time is disabled
+
Event Logging has been disabled. See Settings->General Settings. +

@@ -216,7 +257,8 @@

Wireless Settings -
Enter the wireless network settings here, or use Scan to find nearby networks to join
+
Enter the wireless network settings here, or use Scan to find nearby networks to join +