tidy up telnet messages

This commit is contained in:
Paul
2019-10-18 15:19:26 +02:00
parent c251599e25
commit 0ac4ad3f37
5 changed files with 79 additions and 65 deletions

View File

@@ -199,7 +199,10 @@ uint32_t MyESP::_getInitialFreeHeap() {
// called when WiFi is connected, and used to start OTA, MQTT // called when WiFi is connected, and used to start OTA, MQTT
void MyESP::_wifiCallback(justwifi_messages_t code, char * parameter) { void MyESP::_wifiCallback(justwifi_messages_t code, char * parameter) {
if ((code == MESSAGE_CONNECTED)) { if (code == MESSAGE_CONNECTED) {
myDebug_P(PSTR("[WIFI] Connected to SSID %s (hostname: %s, IP: %s)"), WiFi.SSID().c_str(), _getESPhostname().c_str(), WiFi.localIP().toString().c_str());
/*
myDebug_P(PSTR("[WIFI] SSID %s"), WiFi.SSID().c_str()); myDebug_P(PSTR("[WIFI] SSID %s"), WiFi.SSID().c_str());
myDebug_P(PSTR("[WIFI] CH %d"), WiFi.channel()); myDebug_P(PSTR("[WIFI] CH %d"), WiFi.channel());
myDebug_P(PSTR("[WIFI] RSSI %d"), WiFi.RSSI()); myDebug_P(PSTR("[WIFI] RSSI %d"), WiFi.RSSI());
@@ -209,10 +212,11 @@ void MyESP::_wifiCallback(justwifi_messages_t code, char * parameter) {
myDebug_P(PSTR("[WIFI] MASK %s"), WiFi.subnetMask().toString().c_str()); myDebug_P(PSTR("[WIFI] MASK %s"), WiFi.subnetMask().toString().c_str());
myDebug_P(PSTR("[WIFI] DNS %s"), WiFi.dnsIP().toString().c_str()); myDebug_P(PSTR("[WIFI] DNS %s"), WiFi.dnsIP().toString().c_str());
myDebug_P(PSTR("[WIFI] HOST %s"), _getESPhostname().c_str()); myDebug_P(PSTR("[WIFI] HOST %s"), _getESPhostname().c_str());
*/
// start OTA // start OTA
ArduinoOTA.begin(); // moved to support esp32 ArduinoOTA.begin(); // moved to support esp32
myDebug_P(PSTR("[OTA] listening to %s.local:%u"), ArduinoOTA.getHostname().c_str(), OTA_PORT); myDebug_P(PSTR("[OTA] Listening to firmware updates on %s.local:%u"), ArduinoOTA.getHostname().c_str(), OTA_PORT);
/* /*
// show reason for the restart if any // show reason for the restart if any
@@ -232,11 +236,11 @@ void MyESP::_wifiCallback(justwifi_messages_t code, char * parameter) {
// if we don't want Serial anymore, turn it off // if we don't want Serial anymore, turn it off
if (!_general_serial) { if (!_general_serial) {
myDebug_P(PSTR("[SYSTEM] Disabling serial port communication.")); myDebug_P(PSTR("[SYSTEM] Disabling serial port communication"));
SerialAndTelnet.flush(); // flush so all buffer is printed to serial SerialAndTelnet.flush(); // flush so all buffer is printed to serial
setUseSerial(false); setUseSerial(false);
} else { } else {
myDebug_P(PSTR("[SYSTEM] Serial port communication is enabled.")); myDebug_P(PSTR("[SYSTEM] Serial port communication is enabled"));
} }
_wifi_connected = true; _wifi_connected = true;
@@ -263,11 +267,11 @@ void MyESP::_wifiCallback(justwifi_messages_t code, char * parameter) {
// if we don't want Serial anymore, turn it off // if we don't want Serial anymore, turn it off
if (!_general_serial) { if (!_general_serial) {
myDebug_P(PSTR("[SYSTEM] Disabling serial port communication.")); myDebug_P(PSTR("[SYSTEM] Disabling serial port communication"));
SerialAndTelnet.flush(); // flush so all buffer is printed to serial SerialAndTelnet.flush(); // flush so all buffer is printed to serial
setUseSerial(false); setUseSerial(false);
} else { } else {
myDebug_P(PSTR("[SYSTEM] Serial port communication is enabled.")); myDebug_P(PSTR("[SYSTEM] Serial port communication is enabled"));
} }
_wifi_connected = true; _wifi_connected = true;
@@ -279,7 +283,7 @@ void MyESP::_wifiCallback(justwifi_messages_t code, char * parameter) {
} }
if (code == MESSAGE_CONNECTING) { if (code == MESSAGE_CONNECTING) {
myDebug_P(PSTR("[WIFI] Connecting to %s"), parameter); myDebug_P(PSTR("[WIFI] Connecting to %s..."), parameter);
_wifi_connected = false; _wifi_connected = false;
} }
@@ -413,7 +417,7 @@ bool MyESP::mqttPublish(const char * topic, const char * payload) {
// MQTT onConnect - when a connect is established // MQTT onConnect - when a connect is established
void MyESP::_mqttOnConnect() { void MyESP::_mqttOnConnect() {
myDebug_P(PSTR("[MQTT] is connected")); myDebug_P(PSTR("[MQTT] MQTT connected established"));
_mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MIN; _mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MIN;
_mqtt_last_connection = millis(); _mqtt_last_connection = millis();
@@ -590,17 +594,8 @@ void MyESP::setTelnet(telnetcommand_callback_f callback_cmd, telnet_callback_f c
} }
void MyESP::_telnetConnected() { void MyESP::_telnetConnected() {
myDebug_P(PSTR("[TELNET] Telnet connection established")); myDebug_P(PSTR("[TELNET] Connected to %s version %s. Type ? for commands."), _app_name, _app_version);
_consoleShowHelp(); // Show the initial message //_consoleShowHelp(); // Show the initial message
#ifdef CRASH
// show crash dump if just restarted after a fatal crash
uint32_t crash_time;
EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
if ((crash_time) && (crash_time != 0xFFFFFFFF)) {
myDebug_P(PSTR("[SYSTEM] There is stack data available from the last system crash. Use 'crash dump' to view and 'crash clear' to reset"));
}
#endif
// call callback // call callback
if (_telnet_callback_f) { if (_telnet_callback_f) {
@@ -609,7 +604,7 @@ void MyESP::_telnetConnected() {
} }
void MyESP::_telnetDisconnected() { void MyESP::_telnetDisconnected() {
myDebug_P(PSTR("[TELNET] Telnet connection closed")); // myDebug_P(PSTR("[TELNET] Telnet connection closed"));
if (_telnet_callback_f) { if (_telnet_callback_f) {
(_telnet_callback_f)(TELNET_EVENT_DISCONNECT); // call callback (_telnet_callback_f)(TELNET_EVENT_DISCONNECT); // call callback
} }
@@ -631,7 +626,7 @@ void MyESP::_telnet_setup() {
// Show help of commands // Show help of commands
void MyESP::_consoleShowHelp() { void MyESP::_consoleShowHelp() {
myDebug_P(PSTR("")); myDebug_P(PSTR(""));
myDebug_P(PSTR("* Connected to: %s version %s"), _app_name, _app_version); myDebug_P(PSTR("* %s version %s"), _app_name, _app_version);
if (isAPmode()) { if (isAPmode()) {
myDebug_P(PSTR("* Device is in AP mode with SSID %s"), jw.getAPSSID().c_str()); myDebug_P(PSTR("* Device is in AP mode with SSID %s"), jw.getAPSSID().c_str());
@@ -649,8 +644,15 @@ void MyESP::_consoleShowHelp() {
myDebug_P(PSTR("* Commands:")); myDebug_P(PSTR("* Commands:"));
myDebug_P(PSTR("* ?/help=show commands, CTRL-D/quit=close telnet session")); myDebug_P(PSTR("* ?/help=show commands, CTRL-D/quit=close telnet session"));
myDebug_P(PSTR("* set, system, restart, mqttlog")); myDebug_P(PSTR("* set, system, restart, mqttlog"));
#ifdef CRASH #ifdef CRASH
myDebug_P(PSTR("* crash <dump | clear | test [n]>")); myDebug_P(PSTR("* crash <dump | clear | test [n]>"));
// show crash dump if just restarted after a fatal crash
uint32_t crash_time;
EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
if ((crash_time) && (crash_time != 0xFFFFFFFF)) {
myDebug_P(PSTR("[SYSTEM] There is stack data available from the last system crash. Use 'crash dump' to view and 'crash clear' to reset"));
}
#endif #endif
// call callback function // call callback function
@@ -1507,27 +1509,27 @@ char * MyESP::_mqttTopic(const char * topic) {
} }
// validates a file in SPIFFS, loads it into the json buffer and returns true if ok // validates a file in SPIFFS, loads it into the json buffer and returns true if ok
bool MyESP::_fs_validateConfigFile(const char * filename, size_t maxsize, JsonDocument & doc) { size_t MyESP::_fs_validateConfigFile(const char * filename, size_t maxsize, JsonDocument & doc) {
// see if we can open it // see if we can open it
File file = SPIFFS.open(filename, "r"); File file = SPIFFS.open(filename, "r");
if (!file) { if (!file) {
myDebug_P(PSTR("[FS] File %s not found"), filename); myDebug_P(PSTR("[FS] File %s not found"), filename);
return false; return 0;
} }
// check size // check size
size_t size = file.size(); size_t size = file.size();
myDebug_P(PSTR("[FS] Checking file %s (%d bytes)"), filename, size); // remove for debugging // myDebug_P(PSTR("[FS] Checking file %s (%d bytes)"), filename, size); // remove for debugging
if (size > maxsize) { if (size > maxsize) {
file.close(); file.close();
myDebug_P(PSTR("[FS] File %s size %d is too large (max %d)"), filename, size, maxsize); myDebug_P(PSTR("[FS] Error. File %s size %d is too large (max %d)"), filename, size, maxsize);
return false; return 0;
} else if (size == 0) { } else if (size == 0) {
file.close(); file.close();
myDebug_P(PSTR("[FS] Corrupted file %s"), filename); myDebug_P(PSTR("[FS] Error. Corrupted file %s"), filename);
return false; return 0;
} }
// check integrity by reading file from SPIFFS into the char array // check integrity by reading file from SPIFFS into the char array
@@ -1536,53 +1538,53 @@ bool MyESP::_fs_validateConfigFile(const char * filename, size_t maxsize, JsonDo
size_t real_size = file.readBytes(buffer, size); size_t real_size = file.readBytes(buffer, size);
if (real_size != size) { if (real_size != size) {
file.close(); file.close();
myDebug_P(PSTR("[FS] Error, file %s sizes don't match (%d/%d), looks corrupted"), filename, real_size, size); myDebug_P(PSTR("[FS] Error. File %s sizes don't match (%d/%d), looks corrupted"), filename, real_size, size);
delete[] buffer; delete[] buffer;
return false; return 0;
} }
// now read into the given json // now read into the given json
DeserializationError error = deserializeJson(doc, buffer); DeserializationError error = deserializeJson(doc, buffer);
if (error) { if (error) {
myDebug_P(PSTR("[FS] Failed to deserialize json, error %s"), error.c_str()); myDebug_P(PSTR("[FS] Error. Failed to deserialize json, error %s"), error.c_str());
delete[] buffer; delete[] buffer;
return false; return 0;
} }
// serializeJsonPretty(doc, Serial); // enable for debugging // serializeJsonPretty(doc, Serial); // enable for debugging
file.close(); file.close();
delete[] buffer; delete[] buffer;
return true; return size;
} }
// validates the event log file in SPIFFS // validates the event log file in SPIFFS
// returns true if all OK // returns true if all OK
bool MyESP::_fs_validateLogFile(const char * filename) { size_t MyESP::_fs_validateLogFile(const char * filename) {
// exit if we have disabled logging // exit if we have disabled logging
if (!_general_log_events) { if (!_general_log_events) {
return true; return 0;
} }
// see if we can open it // see if we can open it
File eventlog = SPIFFS.open(filename, "r"); File eventlog = SPIFFS.open(filename, "r");
if (!eventlog) { if (!eventlog) {
myDebug_P(PSTR("[FS] File %s not found"), filename); myDebug_P(PSTR("[FS] File %s not found"), filename);
return false; return 0;
} }
// check sizes // check sizes
size_t size = eventlog.size(); size_t size = eventlog.size();
size_t maxsize = ESP.getFreeHeap() - 2000; // reserve some buffer size_t maxsize = ESP.getFreeHeap() - 2000; // reserve some buffer
myDebug_P(PSTR("[FS] Checking file %s (%d/%d bytes)"), filename, size, maxsize); // remove for debugging // myDebug_P(PSTR("[FS] Checking file %s (%d/%d bytes)"), filename, size, maxsize); // remove for debugging
if (size > maxsize) { if (size > maxsize) {
eventlog.close(); eventlog.close();
myDebug_P(PSTR("[FS] File %s size %d is too large"), filename, size); myDebug_P(PSTR("[FS] File %s size %d is too large"), filename, size);
return false; return 0;
} else if (size == 0) { } else if (size == 0) {
eventlog.close(); eventlog.close();
myDebug_P(PSTR("[FS] Corrupted file %s"), filename); myDebug_P(PSTR("[FS] Corrupted file %s"), filename);
return false; return 0;
} }
/* /*
@@ -1642,7 +1644,12 @@ bool MyESP::_fs_validateLogFile(const char * filename) {
} }
eventlog.close(); eventlog.close();
return !abort;
if (abort) {
return 0;
}
return size;
} }
// format File System // format File System
@@ -1674,8 +1681,9 @@ bool MyESP::_fs_loadConfig() {
StaticJsonDocument<MYESP_SPIFFS_MAXSIZE_CONFIG> doc; StaticJsonDocument<MYESP_SPIFFS_MAXSIZE_CONFIG> doc;
// set to true to print out contents of file // set to true to print out contents of file
if (!_fs_validateConfigFile(MYESP_CONFIG_FILE, MYESP_SPIFFS_MAXSIZE_CONFIG, doc)) { size_t size = _fs_validateConfigFile(MYESP_CONFIG_FILE, MYESP_SPIFFS_MAXSIZE_CONFIG, doc);
myDebug_P(PSTR("[FS] Failed to open system config")); if (!size) {
myDebug_P(PSTR("[FS] Error. Failed to open system config"));
return false; return false;
} }
@@ -1717,7 +1725,7 @@ bool MyESP::_fs_loadConfig() {
_ntp_interval = 60; _ntp_interval = 60;
_ntp_enabled = ntp["enabled"]; _ntp_enabled = ntp["enabled"];
myDebug_P(PSTR("[FS] System config loaded")); myDebug_P(PSTR("[FS] System config loaded (%d bytes)"), size);
return true; return true;
} }
@@ -1784,8 +1792,9 @@ bool MyESP::fs_setSettingValue(bool * setting, const char * value, bool value_de
bool MyESP::_fs_loadCustomConfig() { bool MyESP::_fs_loadCustomConfig() {
StaticJsonDocument<MYESP_SPIFFS_MAXSIZE_CONFIG> doc; StaticJsonDocument<MYESP_SPIFFS_MAXSIZE_CONFIG> doc;
if (!_fs_validateConfigFile(MYESP_CUSTOMCONFIG_FILE, MYESP_SPIFFS_MAXSIZE_CONFIG, doc)) { size_t size = _fs_validateConfigFile(MYESP_CUSTOMCONFIG_FILE, MYESP_SPIFFS_MAXSIZE_CONFIG, doc);
myDebug_P(PSTR("[FS] Failed to open custom config")); if (!size) {
myDebug_P(PSTR("[FS] Error. Failed to open custom config"));
return false; return false;
} }
@@ -1795,7 +1804,7 @@ bool MyESP::_fs_loadCustomConfig() {
myDebug_P(PSTR("[FS] Error reading custom config")); myDebug_P(PSTR("[FS] Error reading custom config"));
return false; return false;
} else { } else {
myDebug_P(PSTR("[FS] Custom config loaded")); myDebug_P(PSTR("[FS] Custom config loaded (%d bytes)"), size);
} }
} }
@@ -1989,8 +1998,9 @@ void MyESP::_fs_setup() {
*/ */
// validate the event log. Sometimes it can can corrupted. // validate the event log. Sometimes it can can corrupted.
if (_fs_validateLogFile(MYESP_EVENTLOG_FILE)) { size_t size = _fs_validateLogFile(MYESP_EVENTLOG_FILE);
myDebug_P(PSTR("[FS] Event log is healthy")); if (size) {
myDebug_P(PSTR("[FS] Event log loaded (%d bytes)"), size);
} else { } else {
myDebug_P(PSTR("[FS] Resetting event log")); myDebug_P(PSTR("[FS] Resetting event log"));
SPIFFS.remove(MYESP_EVENTLOG_FILE); SPIFFS.remove(MYESP_EVENTLOG_FILE);

View File

@@ -9,7 +9,7 @@
#ifndef MyESP_h #ifndef MyESP_h
#define MyESP_h #define MyESP_h
#define MYESP_VERSION "1.2.10" #define MYESP_VERSION "1.2.11"
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
@@ -385,15 +385,15 @@ class MyESP {
bool _changeSetting(uint8_t wc, const char * setting, const char * value); bool _changeSetting(uint8_t wc, const char * setting, const char * value);
// fs and settings // fs and settings
void _fs_setup(); void _fs_setup();
bool _fs_loadConfig(); bool _fs_loadConfig();
bool _fs_loadCustomConfig(); bool _fs_loadCustomConfig();
void _fs_eraseConfig(); void _fs_eraseConfig();
bool _fs_writeConfig(); bool _fs_writeConfig();
bool _fs_createCustomConfig(); bool _fs_createCustomConfig();
bool _fs_sendConfig(); bool _fs_sendConfig();
bool _fs_validateConfigFile(const char * filename, size_t maxsize, JsonDocument & doc); size_t _fs_validateConfigFile(const char * filename, size_t maxsize, JsonDocument & doc);
bool _fs_validateLogFile(const char * filename); size_t _fs_validateLogFile(const char * filename);
fs_loadsave_callback_f _fs_loadsave_callback_f; fs_loadsave_callback_f _fs_loadsave_callback_f;
fs_setlist_callback_f _fs_setlist_callback_f; fs_setlist_callback_f _fs_setlist_callback_f;

View File

@@ -1244,9 +1244,9 @@ void _showCommands(uint8_t event) {
// we set the logging here // we set the logging here
void TelnetCallback(uint8_t event) { void TelnetCallback(uint8_t event) {
if (event == TELNET_EVENT_CONNECT) { if (event == TELNET_EVENT_CONNECT) {
ems_setLogging(EMS_SYS_LOGGING_DEFAULT); ems_setLogging(EMS_SYS_LOGGING_DEFAULT, true);
} else if (event == TELNET_EVENT_DISCONNECT) { } else if (event == TELNET_EVENT_DISCONNECT) {
ems_setLogging(EMS_SYS_LOGGING_NONE); ems_setLogging(EMS_SYS_LOGGING_NONE, true);
} else if ((event == TELNET_EVENT_SHOWCMD) || (event == TELNET_EVENT_SHOWSET)) { } else if ((event == TELNET_EVENT_SHOWCMD) || (event == TELNET_EVENT_SHOWSET)) {
_showCommands(event); _showCommands(event);
} }
@@ -1968,7 +1968,7 @@ void setup() {
Serial.flush(); Serial.flush();
myESP.setUseSerial(false); myESP.setUseSerial(false);
emsuart_init(); // start EMS bus transmissions emsuart_init(); // start EMS bus transmissions
myDebug_P(PSTR("[UART] Opened Rx/Tx connection")); myDebug_P(PSTR("[UART] Rx/Tx connection established"));
if (!EMSESP_Settings.listen_mode) { if (!EMSESP_Settings.listen_mode) {
// go and find the boiler and thermostat types, if not in listen mode // go and find the boiler and thermostat types, if not in listen mode
ems_discoverModels(); ems_discoverModels();

View File

@@ -444,9 +444,13 @@ _EMS_SYS_LOGGING ems_getLogging() {
return EMS_Sys_Status.emsLogging; return EMS_Sys_Status.emsLogging;
} }
void ems_setLogging(_EMS_SYS_LOGGING loglevel) { void ems_setLogging(_EMS_SYS_LOGGING loglevel, bool silent) {
if (loglevel <= EMS_SYS_LOGGING_JABBER) { if (loglevel <= EMS_SYS_LOGGING_JABBER) {
EMS_Sys_Status.emsLogging = loglevel; EMS_Sys_Status.emsLogging = loglevel;
if (silent) {
return; // don't print to telnet/serial
}
if (loglevel == EMS_SYS_LOGGING_NONE) { if (loglevel == EMS_SYS_LOGGING_NONE) {
myDebug_P(PSTR("System Logging set to None")); myDebug_P(PSTR("System Logging set to None"));
} else if (loglevel == EMS_SYS_LOGGING_BASIC) { } else if (loglevel == EMS_SYS_LOGGING_BASIC) {
@@ -2170,7 +2174,7 @@ void _process_Version(_EMS_RxTelegram * EMS_RxTelegram) {
* Figure out the boiler and thermostat types * Figure out the boiler and thermostat types
*/ */
void ems_discoverModels() { void ems_discoverModels() {
myDebug_P(PSTR("Starting auto discover of EMS devices...")); //myDebug_P(PSTR("Starting auto discover of EMS devices..."));
ems_doReadCommand(EMS_TYPE_UBADevices, EMS_ID_BOILER); ems_doReadCommand(EMS_TYPE_UBADevices, EMS_ID_BOILER);
} }

View File

@@ -410,7 +410,7 @@ void ems_setFlowTemp(uint8_t temperature);
void ems_setWarmWaterActivated(bool activated); void ems_setWarmWaterActivated(bool activated);
void ems_setWarmTapWaterActivated(bool activated); void ems_setWarmTapWaterActivated(bool activated);
void ems_setPoll(bool b); void ems_setPoll(bool b);
void ems_setLogging(_EMS_SYS_LOGGING loglevel); void ems_setLogging(_EMS_SYS_LOGGING loglevel, bool silent = false);
void ems_setEmsRefreshed(bool b); void ems_setEmsRefreshed(bool b);
void ems_setWarmWaterModeComfort(uint8_t comfort); void ems_setWarmWaterModeComfort(uint8_t comfort);
void ems_setModels(); void ems_setModels();