diff --git a/src/console.cpp b/src/console.cpp index 7dce00224..419eb8082 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -225,9 +225,8 @@ void Console::load_standard_commands(unsigned int context) { context, CommandFlags::USER, flash_string_vector{F_(log)}, - flash_string_vector{F_(log_level_optional), F_(trace_format_optional), F_(traceid_optional)}, + flash_string_vector{F_(log_level_optional)}, [](Shell & shell, const std::vector & arguments) { - uint16_t watch_id; if (!arguments.empty()) { uuid::log::Level level; @@ -237,41 +236,10 @@ void Console::load_standard_commands(unsigned int context) { shell.printfln(F_(invalid_log_level)); return; } - - // trace logic - if (level == uuid::log::Level::TRACE || level == uuid::log::Level::DEBUG) { - watch_id = LOG_TRACE_WATCH_NONE; // no watch ID set - if (arguments.size() > 1) { - // next argument is raw or full - if (arguments[1] == read_flash_string(F_(raw))) { - emsesp::EMSESP::trace_raw(true); - } else if (arguments[1] == read_flash_string(F_(pretty))) { - emsesp::EMSESP::trace_raw(false); - } else { - emsesp::EMSESP::trace_watch_id(Helpers::hextoint(arguments[1].c_str())); - } - - // get the watch_id if its set - if (arguments.size() == 3) { - emsesp::EMSESP::trace_watch_id(Helpers::hextoint(arguments[2].c_str())); - } - } else { - // it was "log trace" so reset the watch id and switch back to pretty - emsesp::EMSESP::trace_raw(false); - emsesp::EMSESP::trace_watch_id(LOG_TRACE_WATCH_NONE); - } - } } // print out logging settings shell.printfln(F_(log_level_fmt), uuid::log::format_level_uppercase(shell.log_level())); - watch_id = emsesp::EMSESP::trace_watch_id(); - if (watch_id == LOG_TRACE_WATCH_NONE) { - shell.printfln(F("Tracing all telegrams")); - } else { - shell.printfln(F("Tracing only telegrams that match a device ID or telegram type of 0x%02X"), watch_id); - } - shell.printfln(F_(trace_raw_fmt), emsesp::EMSESP::trace_raw() ? F("as raw bytes") : F("in decoded format")); }, [](Shell & shell __attribute__((unused)), const std::vector & arguments __attribute__((unused))) -> std::vector { return uuid::log::levels_lowercase(); diff --git a/src/console.h b/src/console.h index 640326926..69ad657f4 100644 --- a/src/console.h +++ b/src/console.h @@ -42,8 +42,8 @@ using uuid::log::Level; // clang-format off #define LOG_DEBUG(...) if (logger_.enabled(Level::DEBUG)) {logger_.debug(__VA_ARGS__);} -#define LOG_TRACE(...) if (logger_.enabled(Level::TRACE)) {logger_.trace(__VA_ARGS__);} #define LOG_INFO(...) logger_.info(__VA_ARGS__) +#define LOG_TRACE(...) logger_.trace(__VA_ARGS__) #define LOG_NOTICE(...) logger_.notice(__VA_ARGS__) #define LOG_WARNING(...) logger_.warning(__VA_ARGS__) #define LOG_ERROR(...) logger_.err(__VA_ARGS__) @@ -90,7 +90,7 @@ MAKE_PSTR_WORD(restart) MAKE_PSTR_WORD(reconnect) MAKE_PSTR_WORD(format) MAKE_PSTR_WORD(raw) -MAKE_PSTR_WORD(pretty) +MAKE_PSTR_WORD(watch) // context menus MAKE_PSTR_WORD(mqtt) @@ -102,9 +102,6 @@ MAKE_PSTR(degrees_mandatory, "") MAKE_PSTR(asterisks, "********") MAKE_PSTR(n_mandatory, "") MAKE_PSTR(n_optional, "[n]") -MAKE_PSTR(traceid_optional, "[trace ID]") -MAKE_PSTR(trace_raw_fmt, "Displaying telegrams %s") -MAKE_PSTR(trace_format_optional, "[pretty | raw]") MAKE_PSTR(bool_mandatory, "") MAKE_PSTR(typeid_mandatory, "") MAKE_PSTR(deviceid_mandatory, "") diff --git a/src/emsesp.cpp b/src/emsesp.cpp index f015e3568..422f92c21 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -32,6 +32,10 @@ MAKE_PSTR(tx_mode_fmt, "Tx mode = %d") MAKE_PSTR(bus_id_fmt, "Bus ID = %02X") MAKE_PSTR(read_only_fmt, "Read-only mode is %s") +MAKE_PSTR(watchid_optional, "[ID]") +MAKE_PSTR(watch_format_mandatory, "") +MAKE_PSTR(invalid_watch, "Invalid watch type") + MAKE_PSTR(logger_name, "emsesp") namespace emsesp { @@ -56,8 +60,8 @@ Shower EMSESP::shower_; // Shower logic // static/common variables uint8_t EMSESP::actual_master_thermostat_ = EMSESP_DEFAULT_MASTER_THERMOSTAT; // which thermostat leads when multiple found -uint16_t EMSESP::trace_watch_id_ = LOG_TRACE_WATCH_NONE; // for when log is TRACE. 0 means no trace set -bool EMSESP::trace_raw_ = false; // not showing raw when in trace logging +uint16_t EMSESP::watch_id_ = WATCH_NONE; // for when log is TRACE. 0 means no trace set +uint8_t EMSESP::watch_ = 0; // trace off bool EMSESP::tap_water_active_ = false; // for when Boiler states we having running warm water. used in Shower() bool EMSESP::ems_read_only_; uint32_t EMSESP::last_fetch_ = 0; @@ -97,12 +101,12 @@ uint8_t EMSESP::actual_master_thermostat() { } // to watch both type IDs and device IDs -void EMSESP::trace_watch_id(uint16_t trace_watch_id) { +void EMSESP::watch_id(uint16_t watch_id) { // if it's a device ID, which is a single byte, remove the MSB so to support both Buderus and HT3 protocols - if (trace_watch_id <= 0xFF) { - trace_watch_id_ = (trace_watch_id & 0x7F); + if (watch_id <= 0xFF) { + watch_id_ = (watch_id & 0x7F); } else { - trace_watch_id_ = trace_watch_id; + watch_id_ = watch_id; } } @@ -389,10 +393,9 @@ void EMSESP::process_version(std::shared_ptr telegram) { // We also check for common telgram types, like the Version(0x02) // returns false if there are none found bool EMSESP::process_telegram(std::shared_ptr telegram) { - if ((logger_.enabled(Level::TRACE)) && !trace_raw()) { - if ((trace_watch_id_ == LOG_TRACE_WATCH_NONE) || (telegram->src == trace_watch_id_) || (telegram->dest == trace_watch_id_) - || (telegram->type_id == trace_watch_id_)) { - LOG_TRACE(pretty_telegram(telegram).c_str()); + if (watch() == 1) { + if ((watch_id_ == WATCH_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) { + LOG_INFO(pretty_telegram(telegram).c_str()); } } @@ -562,14 +565,14 @@ void EMSESP::send_write_request(const uint16_t type_id, // this is main entry point when data is received on the Rx line, via emsuart library // we check if its a complete telegram or just a single byte (which could be a poll or a return status) void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { + // LOG_DEBUG(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); // check first for echo - //LOG_TRACE(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); uint8_t first_value = data[0]; if (((first_value & 0x7F) == txservice_.ems_bus_id()) && (length > 1)) { - // if we ask ourself at roomcontrol for version e.g. 0B 98 02 ... + // if we ask ourself at roomcontrol for version e.g. 0B 98 02 00 20 Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data); #ifdef EMSESP_DEBUG - rxservice_.add(data, length); // just for logging, if compiled with additional debugging + LOG_DEBUG(F("[DEBUG] Echo: %s"), Helpers::data_to_hex(data, length).c_str()); #endif return; // it's an echo } @@ -613,7 +616,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { // So re-send the last Tx and increment retry count uint8_t retries = txservice_.retry_tx(); // returns 0 if exceeded count #ifdef EMSESP_DEBUG - LOG_DEBUG(F("Last Tx operation failed. Retry #%d. Sent: %s, received: %s"), + LOG_DEBUG(F("[DEBUG] Last Tx operation failed. Retry #%d. Sent: %s, received: %s"), retries, txservice_.last_tx_to_string().c_str(), Helpers::data_to_hex(data, length).c_str()); @@ -823,6 +826,50 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { shell.printfln(F_(read_only_fmt), settings.ems_read_only() ? F_(on) : F_(off)); }); + + + EMSESPShell::commands->add_command(ShellContext::EMS, + CommandFlags::USER, + flash_string_vector{F_(watch)}, + flash_string_vector{F_(watch_format_mandatory), F_(watchid_optional)}, + [](Shell & shell, const std::vector & arguments) { + // get raw/pretty + if (arguments[0] == read_flash_string(F_(raw))) { + emsesp::EMSESP::watch(2); // raw + } else if (arguments[0] == read_flash_string(F_(on))) { + emsesp::EMSESP::watch(1); // on + } else if (arguments[0] == read_flash_string(F_(off))) { + emsesp::EMSESP::watch(0); // off + } else { + shell.printfln(F_(invalid_watch)); + return; + } + + uint16_t watch_id; + if (arguments.size() == 2) { + // get the watch_id if its set + watch_id = Helpers::hextoint(arguments[1].c_str()); + } else { + watch_id = WATCH_NONE; + } + + emsesp::EMSESP::watch_id(watch_id); + + uint8_t watch = emsesp::EMSESP::watch(); + if (watch == 0) { + shell.printfln(F("Watch is off")); + } else if (watch == 1) { + shell.printfln(F("Watching incoming telegrams, displayed in decoded format")); + } else { + shell.printfln(F("Watching incoming telegrams, displayed as raw bytes")); + } + + watch_id = emsesp::EMSESP::watch_id(); + if (watch_id != WATCH_NONE) { + shell.printfln(F("Filtering only telegrams that match a device ID or telegram type of 0x%02X"), watch_id); + } + }); + // enter the context Console::enter_custom_context(shell, context); } @@ -838,7 +885,6 @@ void EMSESP::start() { network_.start(); console_.start(); sensors_.start(); - rxservice_.start(); txservice_.start(); shower_.start(); mqtt_.start(); diff --git a/src/emsesp.h b/src/emsesp.h index 5ebceed26..a2c862df1 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -46,7 +46,7 @@ #include "devices/boiler.h" -#define LOG_TRACE_WATCH_NONE 0 // no watch id set +#define WATCH_NONE 0 // no watch id set namespace emsesp { @@ -99,18 +99,18 @@ class EMSESP { return sensors_.devices(); } - static void trace_watch_id(uint16_t id); + static void watch_id(uint16_t id); - static uint16_t trace_watch_id() { - return trace_watch_id_; + static uint16_t watch_id() { + return watch_id_; } - static void trace_raw(bool trace_raw) { - trace_raw_ = trace_raw; + static void watch(uint8_t watch) { + watch_ = watch; // 0=off, 1=on, 2=raw } - static bool trace_raw() { - return trace_raw_; + static uint8_t watch() { + return watch_; // 0=off, 1=on, 2=raw } static bool tap_water_active() { @@ -131,7 +131,7 @@ class EMSESP { static void fetch_device_values(const uint8_t device_id = 0); - static bool add_device(const uint8_t device_id, const uint8_t product_id, std::string & version, const uint8_t brand); + static bool add_device(const uint8_t device_id, const uint8_t product_id, std::string & version, const uint8_t brand); static Mqtt mqtt_; static System system_; @@ -166,8 +166,8 @@ class EMSESP { static std::vector device_library_; static uint8_t actual_master_thermostat_; - static uint16_t trace_watch_id_; - static bool trace_raw_; + static uint16_t watch_id_; + static uint8_t watch_; static bool tap_water_active_; static bool ems_read_only_; };