From 05baec85b7ae5c1812bf04d2241d02b8fc8607e3 Mon Sep 17 00:00:00 2001 From: proddy Date: Fri, 26 Dec 2025 10:10:27 +0100 Subject: [PATCH] implement txenabled system command - #2850 --- lib_standalone/emsuart_standalone.h | 6 ++++++ src/core/emsesp.cpp | 4 ++-- src/core/helpers.cpp | 1 + src/core/locale_common.h | 2 ++ src/core/locale_translations.h | 1 + src/core/shuntingYard.cpp | 26 ++++++++++++++--------- src/core/system.cpp | 33 ++++++++++++++++++++--------- src/core/system.h | 1 + 8 files changed, 52 insertions(+), 22 deletions(-) diff --git a/lib_standalone/emsuart_standalone.h b/lib_standalone/emsuart_standalone.h index dfab52a5c..6146f7372 100644 --- a/lib_standalone/emsuart_standalone.h +++ b/lib_standalone/emsuart_standalone.h @@ -21,6 +21,12 @@ #include "Arduino.h" +#define EMS_TXMODE_OFF 0 +#define EMS_TXMODE_DEFAULT 1 +#define EMS_TXMODE_EMSPLUS 2 +#define EMS_TXMODE_HT3 3 +#define EMS_TXMODE_HW 4 + namespace emsesp { #define EMS_TX_STATUS_ERR 0 diff --git a/src/core/emsesp.cpp b/src/core/emsesp.cpp index be82dd158..e7add50b0 100644 --- a/src/core/emsesp.cpp +++ b/src/core/emsesp.cpp @@ -1813,9 +1813,9 @@ void EMSESP::shell_prompt() { void EMSESP::loop() { uuid::loop(); // store system uptime - // does LED and checks system health, and syslog service + // handles LED and checks system health, and syslog service if (system_.loop()) { - return; // LED flashing is active + return; // LED flashing is active, skip the rest of the loop } esp32React.loop(); // web services diff --git a/src/core/helpers.cpp b/src/core/helpers.cpp index 207167ac8..9bd6d3d27 100644 --- a/src/core/helpers.cpp +++ b/src/core/helpers.cpp @@ -696,6 +696,7 @@ bool Helpers::value2string(const char * value, std::string & value_s) { // checks to see if a string (usually a command or payload cmd) looks like a boolean // on, off, true, false, 1, 0 +// uses translated words for on/off bool Helpers::value2bool(const char * value, bool & value_b) { if ((value == nullptr) || (strlen(value) == 0)) { return false; diff --git a/src/core/locale_common.h b/src/core/locale_common.h index 26ec0eb5c..c9c2de3c1 100644 --- a/src/core/locale_common.h +++ b/src/core/locale_common.h @@ -42,6 +42,7 @@ MAKE_WORD(system) MAKE_WORD(fetch) MAKE_WORD(restart) MAKE_WORD(format) +MAKE_WORD(txenabled) MAKE_WORD(raw) MAKE_WORD(watch) MAKE_WORD(syslog) @@ -168,6 +169,7 @@ MAKE_WORD_CUSTOM(password_prompt, "Password: ") MAKE_WORD_CUSTOM(unset, "") MAKE_WORD_CUSTOM(enable_mandatory, "") MAKE_WORD_CUSTOM(service_mandatory, "") +MAKE_WORD_CUSTOM(txenabled_cmd, "enable/disable TX") // more common names that don't need translations MAKE_NOTRANSLATION(1x3min, "1x3min") diff --git a/src/core/locale_translations.h b/src/core/locale_translations.h index b6470c8de..85d50d4ac 100644 --- a/src/core/locale_translations.h +++ b/src/core/locale_translations.h @@ -71,6 +71,7 @@ MAKE_WORD_TRANSLATION(changeloglevel_cmd, "change log level", "Ändere Protokoll MAKE_WORD_TRANSLATION(fetch_cmd, "refresh all EMS values", "Aktualisiere alle EMS-Werte", "Verversen alle EMS waardes", "uppdatera alla EMS-värden", "odśwież wszystkie wartości EMS", "oppfrisk alle EMS verdier", "", "Bütün EMS değerlerini yenile", "aggiornare tutti i valori EMS", "obnoviť všetky hodnoty EMS", "aktualizovat všechny EMS hodnoty") // TODO translate MAKE_WORD_TRANSLATION(restart_cmd, "restart EMS-ESP", "Neustart", "opnieuw opstarten", "starta om EMS-ESP", "uruchom ponownie EMS-ESP", "restart EMS-ESP", "redémarrer EMS-ESP", "EMS-ESPyi yeniden başlat", "riavvia EMS-ESP", "reštart EMS-ESP", "restartovat EMS-ESP") // TODO translate MAKE_WORD_TRANSLATION(format_cmd, "factory reset EMS-ESP", "EMS-ESP auf Werkseinstellungen zurücksetzen", "fabriksåterställ EMS-ESP", "", "", "", "", "", "", "továrenske nastavenie EMS-ESP", "tovární nastavení EMS-ESP") // TODO translate +MAKE_WORD_TRANSLATION(txenabled_cmd, "enable/disable TX", "TX aktivieren/deaktivieren", "TX inschakelen/uitschakelen", "aktivera/inaktivera TX", "włącz/wyłącz TX", "aktiver/deaktiver TX", "", "TX'i etkinleştir/devre dışı bırak", "abilita/disabilita TX", "povoliť/zakázať TX", "povolit/zakázat TX") // TODO translate MAKE_WORD_TRANSLATION(watch_cmd, "watch incoming telegrams", "Beobachte eingehende Telegramme", "inkomende telegrammen bekijken", "visa inkommande telegram", "obserwuj przyczodzące telegramy", "se innkommende telegrammer", "", "Gelen telegramları", "guardare i telegrammi in arrivo", "sledovať prichádzajúce telegramy", "sledovat příchozí telegramy") // TODO translate MAKE_WORD_TRANSLATION(publish_cmd, "publish all to MQTT", "Publiziere MQTT", "publiceer alles naar MQTT", "publicera allt till MQTT", "opublikuj wszystko na MQTT", "Publiser alt til MQTT", "", "Hepsini MQTTye gönder", "pubblica tutto su MQTT", "zverejniť všetko na MQTT", "publikovat vše do MQTT") // TODO translate MAKE_WORD_TRANSLATION(system_info_cmd, "show system info", "Zeige Systeminformationen", "toon systeemstatus", "visa systeminformation", "pokaż status systemu", "vis system status", "", "Sistem Durumunu Göster", "visualizza stati di sistema", "zobraziť stav systému", "zobrazit informace o systému") // TODO translate diff --git a/src/core/shuntingYard.cpp b/src/core/shuntingYard.cpp index 703befea0..62e738bfb 100644 --- a/src/core/shuntingYard.cpp +++ b/src/core/shuntingYard.cpp @@ -399,17 +399,23 @@ std::string commands(std::string & expr, bool quotes) { // checks for logic value int to_logic(const std::string & s) { - if (s.empty()) { - return -1; + bool value_b; + if (!Helpers::value2bool(s.c_str(), value_b)) { + return -1; // invalid } - auto l = Helpers::toLower(s); - if (s[0] == '1' || l == "on" || l == "true") { - return 1; - } - if (s[0] == '0' || l == "off" || l == "false") { - return 0; - } - return -1; + return value_b; + + // if (s.empty()) { + // return -1; + // } + // auto l = Helpers::toLower(s); + // if (s[0] == '1' || l == "on" || l == "true") { + // return 1; + // } + // if (s[0] == '0' || l == "off" || l == "false") { + // return 0; + // } + // return -1; } // number to string, remove trailing zeros diff --git a/src/core/system.cpp b/src/core/system.cpp index c4bafad06..1860db32b 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -1019,6 +1019,7 @@ void System::commands_init() { Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, FL_(fetch_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(restart), System::command_restart, FL_(restart_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(format), System::command_format, FL_(format_cmd), CommandFlag::ADMIN_ONLY); + Command::add(EMSdevice::DeviceType::SYSTEM, F_(txenabled), System::command_txenabled, FL_(txenabled_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(watch), System::command_watch, FL_(watch_cmd)); Command::add(EMSdevice::DeviceType::SYSTEM, F_(message), System::command_message, FL_(message_cmd)); #if defined(EMSESP_TEST) @@ -1588,16 +1589,6 @@ bool System::command_service(const char * cmd, const char * value) { }); EMSESP::system_.analog_enabled(b); ok = true; - } else if (!strcmp(cmd, "settings/txmode")) { // TODO check - LOG_INFO("Setting TX mode to OFF"); // TODO remove this - EMSESP::webSettingsService.update([&](WebSettings & settings) { - // settings.tx_mode = EMS_TXMODE_OFF; - settings.tx_mode = 0; // TODO remove this - - return StateUpdateResult::CHANGED; - }); - EMSbus::tx_mode(EMS_TXMODE_OFF); - ok = true; } else if (!strcmp(cmd, "mqtt/enabled")) { EMSESP::esp32React.getMqttSettingsService()->update([&](MqttSettings & Settings) { Settings.enabled = b; @@ -2419,6 +2410,28 @@ bool System::load_board_profile(std::vector & data, const std::string & return true; } +// txenabled command - temporarily pause the TX, setting Txmode to 0 +bool System::command_txenabled(const char * value, const int8_t id) { + bool arg; + if (!Helpers::value2bool(value, arg)) { + return false; // argument not recognized + } + if (arg) { + // if the TX mode is already off, revert back to the saved setting + if (EMSbus::tx_mode() == EMS_TXMODE_OFF) { + EMSESP::webSettingsService.read([&](WebSettings & settings) { + EMSbus::tx_mode(settings.tx_mode); + LOG_INFO("TX mode restored"); + }); + } else { + // otherwise set it to off + EMSbus::tx_mode(EMS_TXMODE_OFF); + LOG_INFO("TX mode set to OFF"); + } + } + return true; +} + // format command - factory reset, removing all config files bool System::command_format(const char * value, const int8_t id) { #if !defined(EMSESP_STANDALONE) && !defined(EMSESP_DEBUG) diff --git a/src/core/system.h b/src/core/system.h index 48ffa410e..6a0f4549c 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -103,6 +103,7 @@ class System { static bool command_info(const char * value, const int8_t id, JsonObject output); static bool command_response(const char * value, const int8_t id, JsonObject output); static bool command_service(const char * cmd, const char * value); + static bool command_txenabled(const char * value, const int8_t id); static bool get_value_info(JsonObject root, const char * cmd); static void get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val);