From 709146a824ba3ec848826a2cbf60bebbbcf399f7 Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 17 Oct 2020 16:57:11 +0200 Subject: [PATCH] new system command called report --- src/emsdevice.cpp | 25 ++++++++++- src/emsdevice.h | 24 +++++----- src/system.cpp | 110 +++++++++++++++++++++++++++++++++++++++++----- src/system.h | 3 +- 4 files changed, 139 insertions(+), 23 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index f69b81e11..9a2e74bbf 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -290,6 +290,29 @@ void EMSdevice::show_telegram_handlers(uuid::console::Shell & shell) { shell.println(); } +// list all the telegram type IDs for this device, outputting to a string (max size 200) +char * EMSdevice::show_telegram_handlers(char * result) { + strlcpy(result, "", 200); + + if (telegram_functions_.size() == 0) { + return result; + } + + char str[10]; + uint8_t i = 0; + size_t size = telegram_functions_.size(); + for (const auto & tf : telegram_functions_) { + snprintf_P(str, sizeof(str), PSTR("0x%02X"), tf.telegram_type_id_); + strlcat(result, str, 200); + if (++i < size) { + strlcat(result, " ", 200); + } + } + + return result; +} + + // list all the mqtt handlers for this device void EMSdevice::show_mqtt_handlers(uuid::console::Shell & shell) { Mqtt::show_topic_handlers(shell, this->device_type_); @@ -377,7 +400,7 @@ void EMSdevice::print_value(uuid::console::Shell & shell, uint8_t padding, const void EMSdevice::print_value(uuid::console::Shell & shell, uint8_t padding, const __FlashStringHelper * name, const char * value) { uint8_t i = padding; while (i-- > 0) { - shell.print(F(" ")); + shell.print(F_(1space)); } shell.printfln(PSTR("%s: %s"), uuid::read_flash_string(name).c_str(), value); diff --git a/src/emsdevice.h b/src/emsdevice.h index 2e912d6ae..b5bc52a8a 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -123,8 +123,9 @@ class EMSdevice { std::string to_string() const; std::string to_string_short() const; - void show_telegram_handlers(uuid::console::Shell & shell); - void show_mqtt_handlers(uuid::console::Shell & shell); + void show_telegram_handlers(uuid::console::Shell & shell); + char * show_telegram_handlers(char * result); + void show_mqtt_handlers(uuid::console::Shell & shell); using process_function_p = std::function)>; void register_telegram_type(const uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, process_function_p cb); @@ -190,13 +191,13 @@ class EMSdevice { uint8_t i = padding; while (i-- > 0) { - shell.print(F(" ")); + shell.print(F_(1space)); } shell.printf(PSTR("%s: %s"), uuid::read_flash_string(name).c_str(), buffer); if (suffix != nullptr) { - shell.print(' '); + shell.print(F_(1space)); shell.println(uuid::read_flash_string(suffix).c_str()); } else { shell.println(); @@ -207,13 +208,13 @@ class EMSdevice { static void print_value(uuid::console::Shell & shell, uint8_t padding, const __FlashStringHelper * name, const char * value); enum Brand : uint8_t { - NO_BRAND, // 0 - BOSCH, // 1 - JUNKERS, // 2 - BUDERUS, // 3 - NEFIT, // 4 - SIEGER, // 5 - WORCESTER // 11 + NO_BRAND = 0, // 0 + BOSCH, // 1 + JUNKERS, // 2 + BUDERUS, // 3 + NEFIT, // 4 + SIEGER, // 5 + WORCESTER // 11 }; enum DeviceType : uint8_t { @@ -228,6 +229,7 @@ class EMSdevice { SWITCH, CONTROLLER, CONNECT, + GENERIC, UNKNOWN }; diff --git a/src/system.cpp b/src/system.cpp index b63079f6e..1bd39cec2 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -152,6 +152,7 @@ void System::start() { Command::add(EMSdevice::DeviceType::SYSTEM, settings.ems_bus_id, F("pin"), System::command_pin); Command::add(EMSdevice::DeviceType::SYSTEM, settings.ems_bus_id, F("send"), System::command_send); Command::add_with_json(EMSdevice::DeviceType::SYSTEM, F("info"), System::command_info); + Command::add_with_json(EMSdevice::DeviceType::SYSTEM, F("report"), System::command_report); }); syslog_init(); // init SysLog @@ -243,8 +244,8 @@ void System::show_mem(const char * note) { // send periodic MQTT message with system information void System::send_heartbeat() { // don't send heartbeat if WiFi is not connected - int rssid = wifi_quality(); - if (rssid == -1) { + int rssi = wifi_quality(); + if (rssi == -1) { return; } @@ -265,7 +266,7 @@ void System::send_heartbeat() { doc["status"] = "disconnected"; } - doc["rssid"] = rssid; + doc["rssi"] = rssi; doc["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3); doc["uptime_sec"] = uuid::get_uptime_sec(); doc["mqttpublishfails"] = Mqtt::publish_fails(); @@ -481,11 +482,11 @@ void System::show_system(uuid::console::Shell & shell) { EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { shell.println(); shell.printfln(F("Syslog:")); - shell.print(" "); + shell.print(F_(1space)); shell.printfln(F_(host_fmt), !settings.syslog_host.isEmpty() ? settings.syslog_host.c_str() : uuid::read_flash_string(F_(unset)).c_str()); - shell.print(" "); + shell.print(F_(1space)); shell.printfln(F_(log_level_fmt), uuid::log::format_level_lowercase(static_cast(settings.syslog_level))); - shell.print(" "); + shell.print(F_(1space)); shell.printfln(F_(mark_interval_fmt), settings.syslog_mark_interval); }); @@ -617,18 +618,18 @@ void System::console_commands(Shell & shell, unsigned int context) { flash_string_vector{F_(set)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { - shell.print(" "); + shell.print(F_(1space)); shell.printfln(F_(hostname_fmt), wifiSettings.hostname.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : wifiSettings.hostname.c_str()); }); EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { - shell.print(" "); + shell.print(F_(1space)); shell.printfln(F_(wifi_ssid_fmt), wifiSettings.ssid.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : wifiSettings.ssid.c_str()); - shell.print(" "); + shell.print(F_(1space)); shell.printfln(F_(wifi_password_fmt), wifiSettings.ssid.isEmpty() ? F_(unset) : F_(asterisks)); }); }); @@ -846,7 +847,7 @@ bool System::check_upgrade() { // value and id are ignored bool System::command_info(const char * value, const int8_t id, JsonObject & output) { #ifdef EMSESP_STANDALONE - output["test"] = "testing"; + output["test"] = "testing info command"; #else EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) { char s[7]; @@ -938,5 +939,94 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & outp return true; } +// export debug information +// http://ems-esp/api?device=system&cmd=report +bool System::command_report(const char * value, const int8_t id, JsonObject & output) { + JsonObject node; + + node = output.createNestedObject("System"); + + node["version"] = EMSESP_APP_VERSION; + node["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3); + node["freemem"] = free_mem(); +#if defined(ESP8266) + node["fragmem"] = ESP.getHeapFragmentation(); +#endif + + node = output.createNestedObject("Settings"); + + EMSESP::esp8266React.getMqttSettingsService()->read([&](MqttSettings & settings) { + node["publish_time_boiler"] = settings.publish_time_boiler; + node["publish_time_thermostat"] = settings.publish_time_thermostat; + node["publish_time_solar"] = settings.publish_time_solar; + node["publish_time_mixing"] = settings.publish_time_mixing; + node["publish_time_other"] = settings.publish_time_other; + node["publish_time_sensor"] = settings.publish_time_sensor; + node["mqtt_format"] = settings.mqtt_format; + node["mqtt_qos"] = settings.mqtt_qos; + char s[7]; + node["mqtt_retain"] = Helpers::render_boolean(s, settings.mqtt_retain); + }); + + EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { + char s[7]; + node["tx_mode"] = settings.tx_mode; + node["ems_bus_id"] = settings.ems_bus_id; + node["master_thermostat"] = settings.master_thermostat; + node["rx_gpio"] = settings.rx_gpio; + node["tx_gpio"] = settings.tx_gpio; + node["dallas_gpio"] = settings.dallas_gpio; + node["dallas_parasite"] = Helpers::render_boolean(s, settings.dallas_parasite); + node["led_gpio"] = settings.led_gpio; + node["hide_led"] = Helpers::render_boolean(s, settings.hide_led); + node["api_enabled"] = Helpers::render_boolean(s, settings.api_enabled); + node["bool_format"] = settings.bool_format; + node["analog_enabled"] = settings.analog_enabled; + }); + + node = output.createNestedObject("Status"); + + switch (EMSESP::bus_status()) { + case EMSESP::BUS_STATUS_OFFLINE: + node["bus"] = (F("disconnected")); + break; + case EMSESP::BUS_STATUS_TX_ERRORS: + node["bus"] = (F("connected, instable tx")); + break; + case EMSESP::BUS_STATUS_CONNECTED: + default: + node["bus"] = (F("connected")); + break; + } + + if (EMSESP::bus_status() != EMSESP::BUS_STATUS_OFFLINE) { + node["bus protocol"] = EMSbus::is_ht3() ? F("HT3") : F("Buderus"); + node["#telegrams received"] = EMSESP::rxservice_.telegram_count(); + node["#read requests sent"] = EMSESP::txservice_.telegram_read_count(); + node["#write requests sent"] = EMSESP::txservice_.telegram_write_count(); + node["#incomplete telegrams"] = EMSESP::rxservice_.telegram_error_count(); + node["#tx fails"] = TxService::MAXIMUM_TX_RETRIES, EMSESP::txservice_.telegram_fail_count(); + node["rx line quality"] = EMSESP::rxservice_.quality(); + node["tx line quality"] = EMSESP::txservice_.quality(); + node["#MQTT publish fails"] = Mqtt::publish_fails(); + node["#dallas sensors"] = EMSESP::sensor_devices().size(); + } + + JsonArray devices2 = output.createNestedArray("Devices"); + + for (const auto & device_class : EMSFactory::device_handlers()) { + for (const auto & emsdevice : EMSESP::emsdevices) { + if ((emsdevice) && (emsdevice->device_type() == device_class.first)) { + JsonObject obj = devices2.createNestedObject(); + obj["type"] = emsdevice->device_type_name(); + obj["name"] = emsdevice->to_string(); + char result[200]; + obj["handlers"] = emsdevice->show_telegram_handlers(result); + } + } + } + + return true; +} } // namespace emsesp diff --git a/src/system.h b/src/system.h index 728ab7a3d..3f87d07d5 100644 --- a/src/system.h +++ b/src/system.h @@ -47,10 +47,11 @@ class System { static void format(uuid::console::Shell & shell); static void console_commands(Shell & shell, unsigned int context); + static bool command_pin(const char * value, const int8_t id); static bool command_send(const char * value, const int8_t id); - static bool command_info(const char * value, const int8_t id, JsonObject & output); + static bool command_report(const char * value, const int8_t id, JsonObject & output); static uint8_t free_mem(); static void upload_status(bool in_progress);