From 2b029dc089a471d756c7256b46db9dc1c9eb7f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 25 Nov 2017 16:01:28 +0100 Subject: [PATCH] Websocket callbacks for OTA and WIFI --- code/espurna/ota.ino | 9 +++++++-- code/espurna/wifi.ino | 42 ++++++++++++++++++++++++++++++++++++++++++ code/espurna/ws.ino | 37 ++++++------------------------------- 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 03eb0242..457fd576 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -12,15 +12,20 @@ Copyright (C) 2016-2017 by Xose PĂ©rez // OTA // ----------------------------------------------------------------------------- -void otaConfigure() { +void _otaConfigure() { ArduinoOTA.setPort(OTA_PORT); ArduinoOTA.setHostname(getSetting("hostname").c_str()); ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str()); } +// ----------------------------------------------------------------------------- + void otaSetup() { - otaConfigure(); + _otaConfigure(); + #if WEB_SUPPORT + wsOnAfterParseRegister(_otaConfigure); + #endif ArduinoOTA.onStart([]() { DEBUG_MSG_P(PSTR("[OTA] Start\n")); diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index f5f15643..3448cf17 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -143,6 +143,47 @@ void wifiStatus() { } +bool wifiClean(unsigned char num) { + + bool changed = false; + int i = 0; + + // Clean defined settings + while (i < num) { + + // Skip on first non-defined setting + if (!hasSetting("ssid", i)) { + delSetting("ssid", i); + break; + } + + // Delete empty values + if (!hasSetting("pass", i)) delSetting("pass", i); + if (!hasSetting("ip", i)) delSetting("ip", i); + if (!hasSetting("gw", i)) delSetting("gw", i); + if (!hasSetting("mask", i)) delSetting("mask", i); + if (!hasSetting("dns", i)) delSetting("dns", i); + + ++i; + + } + + // Delete all other settings + while (i < WIFI_MAX_NETWORKS) { + changed = hasSetting("ssid", i); + delSetting("ssid", i); + delSetting("pass", i); + delSetting("ip", i); + delSetting("gw", i); + delSetting("mask", i); + delSetting("dns", i); + ++i; + } + + return changed; + +} + // Inject hardcoded networks void wifiInject() { @@ -262,6 +303,7 @@ void wifiSetup() { #if WEB_SUPPORT wsOnSendRegister(_wifiWebSocketOnSend); + wsOnAfterParseRegister(wifiConfigure); #endif } diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 60d68024..feeaca12 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -214,7 +214,6 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { } if (value != getSetting(key)) { - //DEBUG_MSG_P(PSTR("[WEBSOCKET] Storing %s = %s\n", key.c_str(), value.c_str())); setSetting(key, value); save = changed = true; if (key.startsWith("mqtt")) changedMQTT = true; @@ -223,34 +222,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { } if (webMode == WEB_MODE_NORMAL) { - - // Clean wifi networks - int i = 0; - while (i < network) { - if (!hasSetting("ssid", i)) { - delSetting("ssid", i); - break; - } - if (!hasSetting("pass", i)) delSetting("pass", i); - if (!hasSetting("ip", i)) delSetting("ip", i); - if (!hasSetting("gw", i)) delSetting("gw", i); - if (!hasSetting("mask", i)) delSetting("mask", i); - if (!hasSetting("dns", i)) delSetting("dns", i); - ++i; - } - while (i < WIFI_MAX_NETWORKS) { - if (hasSetting("ssid", i)) { - save = changed = true; - } - delSetting("ssid", i); - delSetting("pass", i); - delSetting("ip", i); - delSetting("gw", i); - delSetting("mask", i); - delSetting("dns", i); - ++i; - } - + if (wifiClean(network)) save = changed = true; } // Save settings @@ -261,16 +233,19 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { (_ws_on_after_parse_callbacks[i])(); } - wifiConfigure(); - otaConfigure(); + // This should got to callback as well + // but first change management has to be in place if (changedMQTT) { mqttConfigure(); mqttDisconnect(); } + + // Persist settings saveSettings(); } + if (changed) { wsSend_P(client_id, PSTR("{\"message\": 8}")); } else {