From 6b327e3ab33eaa68ce06910ef018a8256b7b55d9 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Wed, 21 Apr 2021 07:44:28 +0200 Subject: [PATCH] move system commands to main --- src/console.cpp | 165 ++++++++++++++++++++++++++++++++++++++++++------ src/console.h | 3 +- src/system.cpp | 158 ++-------------------------------------------- src/system.h | 7 +- 4 files changed, 157 insertions(+), 176 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index d03f0c3c9..00ec04d0c 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -54,9 +54,8 @@ void EMSESPShell::stopped() { logger().log(LogLevel::DEBUG, LogFacility::CONSOLE, F("User session closed on console %s"), console_name().c_str()); // remove all custom contexts - commands->remove_all_commands(); - - console_commands_loaded_ = false; // make sure they get reloaded next time a console is opened + // commands->remove_all_commands(); + // console_commands_loaded_ = false; // make sure they get reloaded next time a console is opened } // show welcome banner @@ -99,9 +98,8 @@ void EMSESPShell::add_console_commands() { if (console_commands_loaded_) { return; } - + console_commands_loaded_ = true; // just in case, remove everything - // commands->remove_context_commands(ShellContext::MAIN); commands->remove_all_commands(); commands->add_command(ShellContext::MAIN, CommandFlags::USER, flash_string_vector{F_(show)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { @@ -212,6 +210,7 @@ void EMSESPShell::add_console_commands() { shell.printfln(F_(bus_id_fmt), settings.ems_bus_id); char buffer[4]; shell.printfln(F_(master_thermostat_fmt), settings.master_thermostat == 0 ? uuid::read_flash_string(F_(auto)).c_str() : Helpers::hextoa(buffer, settings.master_thermostat)); + shell.printfln(F_(board_profile_fmt), settings.board_profile.c_str()); }); }); @@ -409,14 +408,8 @@ void EMSESPShell::add_console_commands() { return {}; }); - // System context menu - commands->add_command(ShellContext::MAIN, CommandFlags::USER, flash_string_vector{F_(system)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { - System::console_commands(shell, ShellContext::SYSTEM); - }); - Console::load_standard_commands(ShellContext::MAIN); - - console_commands_loaded_ = true; + Console::load_system_commands(ShellContext::MAIN); } std::string EMSESPShell::hostname_text() { @@ -426,24 +419,27 @@ std::string EMSESPShell::hostname_text() { // remove commands from the current context to save memory before exiting bool EMSESPShell::exit_context() { unsigned int current_context = context(); - commands->remove_context_commands(current_context); if (current_context == ShellContext::MAIN) { Shell::stop(); return true; } - return Shell::exit_context(); + // commands->remove_context_commands(current_context); + // return Shell::exit_context(); + return false; } +/* // enter a custom context (sub-menu) void Console::enter_custom_context(Shell & shell, unsigned int context) { - load_standard_commands(context); + // load_standard_commands(context); // don't go into the new context if it's already the root (to prevent double loading) if (context != ShellContext::MAIN) { shell.enter_context(context); } } +*/ // each custom context has the common commands like log, help, exit, su etc void Console::load_standard_commands(unsigned int context) { @@ -504,8 +500,6 @@ void Console::load_standard_commands(unsigned int context) { }); EMSESPShell::commands->add_command(context, CommandFlags::USER, flash_string_vector{F_(exit)}, [=](Shell & shell, const std::vector & arguments __attribute__((unused))) { - // delete MAIN console stuff first to save memory - EMSESPShell::commands->remove_context_commands(context); shell.exit_context(); }); @@ -538,6 +532,139 @@ void Console::load_standard_commands(unsigned int context) { }); } +// console commands to add +void Console::load_system_commands(unsigned int context) { + EMSESPShell::commands->add_command(context, + CommandFlags::ADMIN, + flash_string_vector{F_(restart)}, + [](Shell & shell __attribute__((unused)), const std::vector & arguments __attribute__((unused))) { EMSESP::system_.restart(); }); + + EMSESPShell::commands->add_command(context, + CommandFlags::ADMIN, + flash_string_vector{F_(wifi), F_(reconnect)}, + [](Shell & shell __attribute__((unused)), const std::vector & arguments __attribute__((unused))) { EMSESP::system_.wifi_reconnect(); }); + + EMSESPShell::commands->add_command(ShellContext::MAIN, CommandFlags::ADMIN, flash_string_vector{F_(format)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { + shell.enter_password(F_(password_prompt), [=](Shell & shell, bool completed, const std::string & password) { + if (completed) { + EMSESP::esp8266React.getSecuritySettingsService()->read([&](SecuritySettings & securitySettings) { + if (securitySettings.jwtSecret.equals(password.c_str())) { + EMSESP::system_.format(shell); + } else { + shell.println(F("incorrect password")); + } + }); + } + }); + }); + + EMSESPShell::commands->add_command(context, CommandFlags::ADMIN, flash_string_vector{F_(passwd)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { + shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) { + if (completed) { + shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) { + if (completed) { + if (password1 == password2) { + EMSESP::esp8266React.getSecuritySettingsService()->update( + [&](SecuritySettings & securitySettings) { + securitySettings.jwtSecret = password2.c_str(); + return StateUpdateResult::CHANGED; + }, + "local"); + shell.println(F("su password updated")); + } else { + shell.println(F("Passwords do not match")); + } + } + }); + } + }); + }); + + EMSESPShell::commands->add_command(context, CommandFlags::USER, flash_string_vector{F_(show), F_(system)}, [=](Shell & shell, const std::vector & arguments __attribute__((unused))) { + EMSESP::system_.show_system(shell); + shell.println(); + }); + + EMSESPShell::commands->add_command(context, + CommandFlags::ADMIN, + flash_string_vector{F_(set), F_(hostname)}, + flash_string_vector{F_(name_mandatory)}, + [](Shell & shell, const std::vector & arguments) { + shell.println("The network connection will be reset..."); + Shell::loop_all(); + delay(1000); // wait a second + EMSESP::esp8266React.getNetworkSettingsService()->update( + [&](NetworkSettings & networkSettings) { + networkSettings.hostname = arguments.front().c_str(); + return StateUpdateResult::CHANGED; + }, + "local"); + }); + + EMSESPShell::commands->add_command(context, + CommandFlags::ADMIN, + flash_string_vector{F_(set), F_(wifi), F_(ssid)}, + flash_string_vector{F_(name_mandatory)}, + [](Shell & shell, const std::vector & arguments) { + EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) { + networkSettings.ssid = arguments.front().c_str(); + return StateUpdateResult::CHANGED; + }); + shell.println("Use `wifi reconnect` to save and apply the new settings"); + }); + + EMSESPShell::commands->add_command(context, CommandFlags::ADMIN, flash_string_vector{F_(set), F_(wifi), F_(password)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { + shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) { + if (completed) { + shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) { + if (completed) { + if (password1 == password2) { + EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) { + networkSettings.password = password2.c_str(); + return StateUpdateResult::CHANGED; + }); + shell.println("Use `wifi reconnect` to save and apply the new settings"); + } else { + shell.println(F("Passwords do not match")); + } + } + }); + } + }); + }); + + EMSESPShell::commands->add_command(context, + CommandFlags::ADMIN, + flash_string_vector{F_(set), F_(board_profile)}, + flash_string_vector{F_(name_mandatory)}, + [](Shell & shell, const std::vector & arguments) { + std::vector data; // led, dallas, rx, tx, button + std::string board_profile = Helpers::toUpper(arguments.front()); + if (!EMSESP::system_.load_board_profile(data, board_profile)) { + shell.println(F("Invalid board profile (S32, E32, MH-ET, NODEMCU, OLIMEX, TLK110, LAN8720, CUSTOM)")); + return; + } + EMSESP::webSettingsService.update( + [&](WebSettings & settings) { + settings.board_profile = board_profile.c_str(); + settings.led_gpio = data[0]; + settings.dallas_gpio = data[1]; + settings.rx_gpio = data[2]; + settings.tx_gpio = data[3]; + settings.pbutton_gpio = data[4]; + return StateUpdateResult::CHANGED; + }, + "local"); + shell.printfln("Loaded board profile %s (%d,%d,%d,%d,%d)", board_profile.c_str(), data[0], data[1], data[2], data[3], data[4]); + EMSESP::system_.network_init(true); + }); + EMSESPShell::commands->add_command(context, CommandFlags::ADMIN, flash_string_vector{F_(show), F_(users)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { + EMSESP::system_.show_users(shell); + }); + +} + + // prompt, change per context std::string EMSESPShell::context_text() { switch (static_cast(context())) { @@ -570,7 +697,7 @@ EMSESPStreamConsole::EMSESPStreamConsole(Stream & stream, bool local) , uuid::console::StreamConsole(stream) , EMSESPShell() , name_(uuid::read_flash_string(F("Serial"))) - , pty_(std::numeric_limits::max()) + , pty_(SIZE_MAX) , addr_() , port_(0) { } @@ -606,8 +733,6 @@ EMSESPStreamConsole::~EMSESPStreamConsole() { #endif ptys_[pty_] = false; ptys_.shrink_to_fit(); - - EMSESPShell::commands->remove_all_commands(); } } diff --git a/src/console.h b/src/console.h index 1ceafd631..fd6c3e916 100644 --- a/src/console.h +++ b/src/console.h @@ -136,8 +136,9 @@ class Console { uuid::log::Level log_level(); - static void enter_custom_context(Shell & shell, unsigned int context); + // static void enter_custom_context(Shell & shell, unsigned int context); static void load_standard_commands(unsigned int context); + static void load_system_commands(unsigned int context); }; } // namespace emsesp diff --git a/src/system.cpp b/src/system.cpp index 41e978443..f5722d829 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -463,7 +463,7 @@ void System::send_heartbeat() { doc["adc"] = analog_; } - Mqtt::publish(F("heartbeat"), doc.as()); // send to MQTT with retain off. This will add to MQTT queue. + Mqtt::publish(F_(heartbeat), doc.as()); // send to MQTT with retain off. This will add to MQTT queue. } // measure and moving average adc @@ -655,12 +655,12 @@ void System::show_users(uuid::console::Shell & shell) { } void System::show_system(uuid::console::Shell & shell) { - shell.printfln(F("Uptime: %s"), uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3).c_str()); + shell.printfln(F("Uptime: %s"), uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3).c_str()); #ifndef EMSESP_STANDALONE - shell.printfln(F("SDK version: %s"), ESP.getSdkVersion()); - shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz()); - shell.printfln(F("Free heap: %lu bytes"), (uint32_t)ESP.getFreeHeap()); + shell.printfln(F("SDK version: %s"), ESP.getSdkVersion()); + shell.printfln(F("CPU frequency: %lu MHz"), ESP.getCpuFreqMHz()); + shell.printfln(F("Free heap: %lu bytes"), (uint32_t)ESP.getFreeHeap()); shell.println(); switch (WiFi.status()) { @@ -676,7 +676,7 @@ void System::show_system(uuid::console::Shell & shell) { shell.printfln(F("WiFi: Network scan complete")); break; - case WL_CONNECTED: { + case WL_CONNECTED: shell.printfln(F("WiFi: Connected")); shell.printfln(F("SSID: %s"), WiFi.SSID().c_str()); shell.printfln(F("BSSID: %s"), WiFi.BSSIDstr().c_str()); @@ -686,7 +686,7 @@ void System::show_system(uuid::console::Shell & shell) { shell.printfln(F("IPv4 address: %s/%s"), uuid::printable_to_string(WiFi.localIP()).c_str(), uuid::printable_to_string(WiFi.subnetMask()).c_str()); shell.printfln(F("IPv4 gateway: %s"), uuid::printable_to_string(WiFi.gatewayIP()).c_str()); shell.printfln(F("IPv4 nameserver: %s"), uuid::printable_to_string(WiFi.dnsIP()).c_str()); - } break; + break; case WL_CONNECT_FAILED: shell.printfln(F("WiFi: Connection failed")); @@ -738,150 +738,6 @@ void System::show_system(uuid::console::Shell & shell) { #endif } -// console commands to add -void System::console_commands(Shell & shell, unsigned int context) { - EMSESPShell::commands->add_command(ShellContext::SYSTEM, - CommandFlags::ADMIN, - flash_string_vector{F_(restart)}, - [](Shell & shell __attribute__((unused)), const std::vector & arguments __attribute__((unused))) { EMSESP::system_.restart(); }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, - CommandFlags::ADMIN, - flash_string_vector{F_(wifi), F_(reconnect)}, - [](Shell & shell __attribute__((unused)), const std::vector & arguments __attribute__((unused))) { EMSESP::system_.wifi_reconnect(); }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::ADMIN, flash_string_vector{F_(format)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { - shell.enter_password(F_(password_prompt), [=](Shell & shell, bool completed, const std::string & password) { - if (completed) { - EMSESP::esp8266React.getSecuritySettingsService()->read([&](SecuritySettings & securitySettings) { - if (securitySettings.jwtSecret.equals(password.c_str())) { - EMSESP::system_.format(shell); - } else { - shell.println(F("incorrect password")); - } - }); - } - }); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::ADMIN, flash_string_vector{F_(passwd)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { - shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) { - if (completed) { - shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) { - if (completed) { - if (password1 == password2) { - EMSESP::esp8266React.getSecuritySettingsService()->update( - [&](SecuritySettings & securitySettings) { - securitySettings.jwtSecret = password2.c_str(); - return StateUpdateResult::CHANGED; - }, - "local"); - shell.println(F("su password updated")); - } else { - shell.println(F("Passwords do not match")); - } - } - }); - } - }); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::USER, flash_string_vector{F_(show)}, [=](Shell & shell, const std::vector & arguments __attribute__((unused))) { - EMSESP::system_.show_system(shell); - shell.println(); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, - CommandFlags::ADMIN, - flash_string_vector{F_(set), F_(hostname)}, - flash_string_vector{F_(name_mandatory)}, - [](Shell & shell, const std::vector & arguments) { - shell.println("The network connection will be reset..."); - Shell::loop_all(); - delay(1000); // wait a second - EMSESP::esp8266React.getNetworkSettingsService()->update( - [&](NetworkSettings & networkSettings) { - networkSettings.hostname = arguments.front().c_str(); - return StateUpdateResult::CHANGED; - }, - "local"); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, - CommandFlags::ADMIN, - flash_string_vector{F_(set), F_(wifi), F_(ssid)}, - flash_string_vector{F_(name_mandatory)}, - [](Shell & shell, const std::vector & arguments) { - EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) { - networkSettings.ssid = arguments.front().c_str(); - return StateUpdateResult::CHANGED; - }); - shell.println("Use `wifi reconnect` to save and apply the new settings"); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::ADMIN, flash_string_vector{F_(set), F_(wifi), F_(password)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { - shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) { - if (completed) { - shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) { - if (completed) { - if (password1 == password2) { - EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) { - networkSettings.password = password2.c_str(); - return StateUpdateResult::CHANGED; - }); - shell.println("Use `wifi reconnect` to save and apply the new settings"); - } else { - shell.println(F("Passwords do not match")); - } - } - }); - } - }); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, - CommandFlags::ADMIN, - flash_string_vector{F_(set), F_(board_profile)}, - flash_string_vector{F_(name_mandatory)}, - [](Shell & shell, const std::vector & arguments) { - std::vector data; // led, dallas, rx, tx, button - std::string board_profile = Helpers::toUpper(arguments.front()); - if (!load_board_profile(data, board_profile)) { - shell.println(F("Invalid board profile")); - return; - } - EMSESP::webSettingsService.update( - [&](WebSettings & settings) { - settings.board_profile = board_profile.c_str(); - settings.led_gpio = data[0]; - settings.dallas_gpio = data[1]; - settings.rx_gpio = data[2]; - settings.tx_gpio = data[3]; - settings.pbutton_gpio = data[4]; - return StateUpdateResult::CHANGED; - }, - "local"); - shell.printfln("Loaded board profile %s (%d,%d,%d,%d,%d)", board_profile.c_str(), data[0], data[1], data[2], data[3], data[4]); - EMSESP::system_.network_init(true); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::USER, flash_string_vector{F_(set)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { - EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { - shell.printfln(F_(hostname_fmt), networkSettings.hostname.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : networkSettings.hostname.c_str()); - shell.printfln(F_(wifi_ssid_fmt), networkSettings.ssid.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : networkSettings.ssid.c_str()); - shell.printfln(F_(wifi_password_fmt), networkSettings.ssid.isEmpty() ? F_(unset) : F_(asterisks)); - }); - EMSESP::webSettingsService.read([&](WebSettings & settings) { shell.printfln(F_(board_profile_fmt), settings.board_profile.c_str()); }); - }); - - EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::ADMIN, flash_string_vector{F_(show), F_(users)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { - EMSESP::system_.show_users(shell); - }); - - // enter the context - Console::enter_custom_context(shell, context); -} - // upgrade from previous versions of EMS-ESP // returns true if an upgrade was done bool System::check_upgrade() { diff --git a/src/system.h b/src/system.h index c3401dab1..57282df1a 100644 --- a/src/system.h +++ b/src/system.h @@ -48,7 +48,6 @@ class System { void loop(); // commands - 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_publish(const char * value, const int8_t id); @@ -103,6 +102,9 @@ class System { return true; #endif } + void show_system(uuid::console::Shell & shell); + void wifi_reconnect(); + void show_users(uuid::console::Shell & shell); private: static uuid::log::Logger logger_; @@ -135,9 +137,6 @@ class System { void system_check(); void measure_analog(); - void show_system(uuid::console::Shell & shell); - void show_users(uuid::console::Shell & shell); - void wifi_reconnect(); int8_t wifi_quality(); bool system_healthy_ = false;