added system command wifi reconnect

This commit is contained in:
proddy
2020-07-26 22:35:33 +02:00
parent b7e72fd18f
commit 5c354af1ba
3 changed files with 45 additions and 28 deletions

View File

@@ -88,6 +88,7 @@ system
set wifi hostname <name> set wifi hostname <name>
set wifi password set wifi password
set wifi ssid <name> set wifi ssid <name>
wifi reconnect
boiler boiler
comfort <hot |eco | intelligent> comfort <hot |eco | intelligent>

View File

@@ -24,6 +24,7 @@
MAKE_PSTR_WORD(passwd) MAKE_PSTR_WORD(passwd)
MAKE_PSTR_WORD(hostname) MAKE_PSTR_WORD(hostname)
MAKE_PSTR_WORD(wifi) MAKE_PSTR_WORD(wifi)
MAKE_PSTR_WORD(reconnect)
MAKE_PSTR_WORD(ssid) MAKE_PSTR_WORD(ssid)
MAKE_PSTR_WORD(heartbeat) MAKE_PSTR_WORD(heartbeat)
MAKE_PSTR_WORD(users) MAKE_PSTR_WORD(users)
@@ -143,7 +144,6 @@ void System::mqtt_commands(const char * message) {
void System::restart() { void System::restart() {
LOG_NOTICE("Restarting system..."); LOG_NOTICE("Restarting system...");
Shell::loop_all(); Shell::loop_all();
EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // forces a save
delay(1000); // wait a second delay(1000); // wait a second
#if defined(ESP8266) #if defined(ESP8266)
ESP.reset(); ESP.reset();
@@ -152,10 +152,19 @@ void System::restart() {
#endif #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 fs
// format the FS. Wipes everything. // format the FS. Wipes everything.
void System::format(uuid::console::Shell & shell) { 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.logger().warning(msg);
shell.flush(); shell.flush();
@@ -486,6 +495,13 @@ void System::console_commands(Shell & shell, unsigned int context) {
restart(); restart();
}); });
EMSESPShell::commands->add_command(ShellContext::SYSTEM,
CommandFlags::ADMIN,
flash_string_vector{F_(wifi), F_(reconnect)},
[](Shell & shell __attribute__((unused)), const std::vector<std::string> & arguments __attribute__((unused))) {
wifi_reconnect();
});
EMSESPShell::commands->add_command(ShellContext::SYSTEM, EMSESPShell::commands->add_command(ShellContext::SYSTEM,
CommandFlags::ADMIN, CommandFlags::ADMIN,
flash_string_vector{F_(format)}, flash_string_vector{F_(format)},
@@ -562,25 +578,24 @@ void System::console_commands(Shell & shell, unsigned int context) {
wifiSettings.ssid = arguments.front().c_str(); wifiSettings.ssid = arguments.front().c_str();
return StateUpdateResult::CHANGED; 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, EMSESPShell::commands
->add_command(ShellContext::SYSTEM,
CommandFlags::ADMIN, CommandFlags::ADMIN,
flash_string_vector{F_(set), F_(wifi), F_(password)}, flash_string_vector{F_(set), F_(wifi), F_(password)},
[](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) { [](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) {
shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) { shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) {
if (completed) { if (completed) {
shell.enter_password(F_(new_password_prompt2), shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) {
[password1](Shell & shell, bool completed, const std::string & password2) {
if (completed) { if (completed) {
if (password1 == password2) { if (password1 == password2) {
EMSESP::esp8266React.getWiFiSettingsService()->updateWithoutPropagation( EMSESP::esp8266React.getWiFiSettingsService()->updateWithoutPropagation([&](WiFiSettings & wifiSettings) {
[&](WiFiSettings & wifiSettings) {
wifiSettings.password = password2.c_str(); wifiSettings.password = password2.c_str();
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
}); });
shell.println("You will need to restart to apply the new WiFi changes"); shell.println("Use `wifi reconnect` to apply the new settings");
} else { } else {
shell.println(F("Passwords do not match")); shell.println(F("Passwords do not match"));
} }

View File

@@ -98,6 +98,7 @@ class System {
static void show_system(uuid::console::Shell & shell); static void show_system(uuid::console::Shell & shell);
static void show_users(uuid::console::Shell & shell); static void show_users(uuid::console::Shell & shell);
static void wifi_reconnect();
static int8_t wifi_quality(); static int8_t wifi_quality();
bool system_healthy_ = false; bool system_healthy_ = false;