From 5c354af1ba32d2578d51f187e42034f16352d0b9 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 26 Jul 2020 22:35:33 +0200 Subject: [PATCH] added system command `wifi reconnect` --- README.md | 1 + src/system.cpp | 71 ++++++++++++++++++++++++++++++-------------------- src/system.h | 1 + 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index adf4c9f28..c7d82605a 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ system set wifi hostname set wifi password set wifi ssid + wifi reconnect boiler comfort diff --git a/src/system.cpp b/src/system.cpp index 05e983f8a..bff937bb3 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -24,6 +24,7 @@ MAKE_PSTR_WORD(passwd) MAKE_PSTR_WORD(hostname) MAKE_PSTR_WORD(wifi) +MAKE_PSTR_WORD(reconnect) MAKE_PSTR_WORD(ssid) MAKE_PSTR_WORD(heartbeat) MAKE_PSTR_WORD(users) @@ -143,8 +144,7 @@ void System::mqtt_commands(const char * message) { void System::restart() { LOG_NOTICE("Restarting system..."); Shell::loop_all(); - EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // forces a save - delay(1000); // wait a second + delay(1000); // wait a second #if defined(ESP8266) ESP.reset(); #elif defined(ESP32) @@ -152,10 +152,19 @@ void System::restart() { #endif } +// saves all settings +void System::wifi_reconnect() { + LOG_NOTICE("The wifi will reconnect..."); + Shell::loop_all(); + delay(1000); // wait a second + EMSESP::emsespSettingsService.save(); // local settings + EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password +} + // format fs // format the FS. Wipes everything. void System::format(uuid::console::Shell & shell) { - auto msg = F("Formatting file system. This will also reset all settings to their defaults"); + auto msg = F("Formatting file system. This will reset all settings to their defaults"); shell.logger().warning(msg); shell.flush(); @@ -486,6 +495,13 @@ void System::console_commands(Shell & shell, unsigned int context) { 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))) { + wifi_reconnect(); + }); + EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::ADMIN, flash_string_vector{F_(format)}, @@ -562,33 +578,32 @@ void System::console_commands(Shell & shell, unsigned int context) { wifiSettings.ssid = arguments.front().c_str(); return StateUpdateResult::CHANGED; }); - shell.println("You will need to use the restart command to apply the new WiFi changes"); + shell.println("Use `wifi reconnect` to 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.getWiFiSettingsService()->updateWithoutPropagation( - [&](WiFiSettings & wifiSettings) { - wifiSettings.password = password2.c_str(); - return StateUpdateResult::CHANGED; - }); - shell.println("You will need to restart to apply the new WiFi changes"); - } else { - shell.println(F("Passwords do not match")); - } - } - }); - } - }); - }); + 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.getWiFiSettingsService()->updateWithoutPropagation([&](WiFiSettings & wifiSettings) { + wifiSettings.password = password2.c_str(); + return StateUpdateResult::CHANGED; + }); + shell.println("Use `wifi reconnect` to apply the new settings"); + } else { + shell.println(F("Passwords do not match")); + } + } + }); + } + }); + }); EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::USER, diff --git a/src/system.h b/src/system.h index 3aa20de2e..e46965017 100644 --- a/src/system.h +++ b/src/system.h @@ -98,6 +98,7 @@ class System { static void show_system(uuid::console::Shell & shell); static void show_users(uuid::console::Shell & shell); + static void wifi_reconnect(); static int8_t wifi_quality(); bool system_healthy_ = false;