From f26f9f67533c02792dadecd00f4e7ead6ee06dcb Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 18 Aug 2024 06:40:27 +0200 Subject: [PATCH] move code to handle network events from anonymous to class method --- src/Config.h | 2 +- src/NukiNetwork.cpp | 5 +- src/WebCfgServer.cpp | 15 +++- src/networkDevices/EthernetDevice.cpp | 109 ++++++++++++++------------ src/networkDevices/EthernetDevice.h | 1 + 5 files changed, 76 insertions(+), 56 deletions(-) diff --git a/src/Config.h b/src/Config.h index d777c11..d621c64 100644 --- a/src/Config.h +++ b/src/Config.h @@ -4,7 +4,7 @@ #define NUKI_HUB_VERSION "9.01" #define NUKI_HUB_BUILD "unknownbuildnr" -#define NUKI_HUB_DATE "2024-08-17" +#define NUKI_HUB_DATE "2024-08-18" #define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json" diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index c4d18cb..252b127 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -161,7 +161,10 @@ void NukiNetwork::setupDevice() break; case 11: Log->println(F("Custom LAN Module")); - if(_preferences->getInt(preference_network_custom_phy, 0) > 0) _networkDeviceType = NetworkDeviceType::CUSTOM; + if(_preferences->getInt(preference_network_custom_phy, 0) > 0) + { + _networkDeviceType = NetworkDeviceType::CUSTOM; + } else { Log->println(F("Custom LAN Module not setup correctly, falling back to Wi-Fi")); diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index 78e076f..fe6acf3 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -2593,9 +2593,18 @@ void WebCfgServer::buildHtml(AsyncWebServerRequest *request) buildNavigationMenuEntry(response, "GPIO Configuration", "/gpiocfg"); buildNavigationMenuEntry(response, "Firmware update", "/ota"); buildNavigationMenuEntry(response, "Import/Export Configuration", "/impexpcfg"); - if(_preferences->getInt(preference_network_hardware, 0) == 11) buildNavigationMenuEntry(response, "Custom Ethernet Configuration", "/custntw"); - if(_preferences->getBool(preference_publish_debug_info, false)) buildNavigationMenuEntry(response, "Advanced Configuration", "/advanced"); - if(_preferences->getBool(preference_webserial_enabled, false)) buildNavigationMenuEntry(response, "Open Webserial", "/webserial"); + if(_preferences->getInt(preference_network_hardware, 0) == 11) + { + buildNavigationMenuEntry(response, "Custom Ethernet Configuration", "/custntw"); + } + if (_preferences->getBool(preference_publish_debug_info, false)) + { + buildNavigationMenuEntry(response, "Advanced Configuration", "/advanced"); + } + if(_preferences->getBool(preference_webserial_enabled, false)) + { + buildNavigationMenuEntry(response, "Open Webserial", "/webserial"); + } #ifndef CONFIG_IDF_TARGET_ESP32H2 if(_allowRestartToPortal) buildNavigationMenuEntry(response, "Configure Wi-Fi", "/wifi"); #endif diff --git a/src/networkDevices/EthernetDevice.cpp b/src/networkDevices/EthernetDevice.cpp index 061c05a..0ebed07 100644 --- a/src/networkDevices/EthernetDevice.cpp +++ b/src/networkDevices/EthernetDevice.cpp @@ -143,56 +143,7 @@ void EthernetDevice::initialize() Network.onEvent([&](arduino_event_id_t event, arduino_event_info_t info) { - switch (event) { - case ARDUINO_EVENT_ETH_START: - Log->println("ETH Started"); - ETH.setHostname(_hostname.c_str()); - break; - case ARDUINO_EVENT_ETH_CONNECTED: - Log->println("ETH Connected"); - if(!localIP().equals("0.0.0.0")) - { - _connected = true; - } - break; - case ARDUINO_EVENT_ETH_GOT_IP: - Log->printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif)); - Log->println(ETH); - - // For RMII devices, this check is handled in the update() method. - if(_useSpi && !_ipConfiguration->dhcpEnabled() && _ipConfiguration->ipAddress() != ETH.localIP()) - { - Log->printf("Static IP not used, retrying to set static IP"); - ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer()); - ETH.begin(_type, _phy_addr, _cs, _irq, _rst, SPI); - } - - _connected = true; - if(_preferences->getBool(preference_ntw_reconfigure, false)) - { - _preferences->putBool(preference_ntw_reconfigure, false); - } - break; - case ARDUINO_EVENT_ETH_LOST_IP: - Log->println("ETH Lost IP"); - _connected = false; - onDisconnected(); - break; - case ARDUINO_EVENT_ETH_DISCONNECTED: - Log->println("ETH Disconnected"); - _connected = false; - onDisconnected(); - break; - case ARDUINO_EVENT_ETH_STOP: - Log->println("ETH Stopped"); - _connected = false; - onDisconnected(); - break; - default: - Log->print("ETH Event: "); - Log->println(event); - break; - } + onNetworkEvent(event, info); }); } else @@ -221,6 +172,62 @@ void EthernetDevice::update() } +void EthernetDevice::onNetworkEvent(arduino_event_id_t event, arduino_event_info_t info) +{ + switch (event) { + case ARDUINO_EVENT_ETH_START: + Log->println("ETH Started"); + ETH.setHostname(_hostname.c_str()); + break; + case ARDUINO_EVENT_ETH_CONNECTED: + Log->println("ETH Connected"); + if(!localIP().equals("0.0.0.0")) + { + _connected = true; + } + break; + case ARDUINO_EVENT_ETH_GOT_IP: + Log->printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif)); + Log->println(ETH); + + // For RMII devices, this check is handled in the update() method. + if(_useSpi && !_ipConfiguration->dhcpEnabled() && _ipConfiguration->ipAddress() != ETH.localIP()) + { + Log->printf("Static IP not used, retrying to set static IP"); + ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer()); + ETH.begin(_type, _phy_addr, _cs, _irq, _rst, SPI); + } + + _connected = true; + if(_preferences->getBool(preference_ntw_reconfigure, false)) + { + _preferences->putBool(preference_ntw_reconfigure, false); + } + break; + case ARDUINO_EVENT_ETH_LOST_IP: + Log->println("ETH Lost IP"); + _connected = false; + onDisconnected(); + break; + case ARDUINO_EVENT_ETH_DISCONNECTED: + Log->println("ETH Disconnected"); + _connected = false; + onDisconnected(); + break; + case ARDUINO_EVENT_ETH_STOP: + Log->println("ETH Stopped"); + _connected = false; + onDisconnected(); + break; + default: + Log->print("ETH Event: "); + Log->println(event); + break; + } +} + + + void EthernetDevice::reconfigure() { delay(200); @@ -266,4 +273,4 @@ String EthernetDevice::localIP() String EthernetDevice::BSSIDstr() { return ""; -} \ No newline at end of file +} diff --git a/src/networkDevices/EthernetDevice.h b/src/networkDevices/EthernetDevice.h index 015b5e5..1760138 100644 --- a/src/networkDevices/EthernetDevice.h +++ b/src/networkDevices/EthernetDevice.h @@ -67,6 +67,7 @@ private: void init(); void onDisconnected(); void waitForIpAddressWithTimeout(); + void onNetworkEvent(arduino_event_id_t event, arduino_event_info_t info); bool _connected = false; bool _restartOnDisconnect = false;