From f29e139cbb7e4a4d539cba6e894ef4a6acd312d6 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 May 2023 09:22:00 +0300 Subject: WIP: big refactoring --- platformio/common/libs/main/homekit/main.cpp | 21 ++++++++++++++++++++- platformio/common/libs/main/homekit/main.h | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'platformio/common/libs/main/homekit') diff --git a/platformio/common/libs/main/homekit/main.cpp b/platformio/common/libs/main/homekit/main.cpp index fd08925..816c764 100644 --- a/platformio/common/libs/main/homekit/main.cpp +++ b/platformio/common/libs/main/homekit/main.cpp @@ -6,7 +6,12 @@ namespace homekit::main { +#ifndef CONFIG_TARGET_ESP01 +#ifndef CONFIG_NO_RECOVERY enum WorkingMode working_mode = WorkingMode::NORMAL; +#endif +#endif + static const uint16_t recovery_boot_detection_ms = 2000; static const uint8_t recovery_boot_delay_ms = 100; @@ -22,8 +27,10 @@ static StopWatch blinkStopWatch; #endif #ifndef CONFIG_TARGET_ESP01 +#ifndef CONFIG_NO_RECOVERY static DNSServer* dnsServer = nullptr; #endif +#endif static void onWifiConnected(const WiFiEventStationModeGotIP& event); static void onWifiDisconnected(const WiFiEventStationModeDisconnected& event); @@ -45,6 +52,7 @@ static void wifiConnect() { } #ifndef CONFIG_TARGET_ESP01 +#ifndef CONFIG_NO_RECOVERY static void wifiHotspot() { led::mcu_led->on(); @@ -71,13 +79,16 @@ static void waitForRecoveryPress() { } } #endif +#endif void setup() { WiFi.disconnect(); +#ifndef CONFIG_NO_RECOVERY #ifndef CONFIG_TARGET_ESP01 homekit::main::waitForRecoveryPress(); #endif +#endif #ifdef DEBUG Serial.begin(115200); @@ -95,25 +106,31 @@ void setup() { } #ifndef CONFIG_TARGET_ESP01 +#ifndef CONFIG_NO_RECOVERY switch (working_mode) { case WorkingMode::RECOVERY: wifiHotspot(); break; case WorkingMode::NORMAL: +#endif #endif wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnected); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnected); wifiConnect(); +#ifndef CONFIG_NO_RECOVERY #ifndef CONFIG_TARGET_ESP01 break; } #endif +#endif } void loop(LoopConfig* config) { +#ifndef CONFIG_NO_RECOVERY #ifndef CONFIG_TARGET_ESP01 if (working_mode == WorkingMode::NORMAL) { +#endif #endif if (wifi_state == WiFiConnectionState::WAITING) { PRINT("."); @@ -166,6 +183,7 @@ void loop(LoopConfig* config) { } #endif } +#ifndef CONFIG_NO_RECOVERY #ifndef CONFIG_TARGET_ESP01 } else { if (dnsServer != nullptr) @@ -176,6 +194,7 @@ void loop(LoopConfig* config) { httpServer->loop(); } #endif +#endif } static void onWifiConnected(const WiFiEventStationModeGotIP& event) { @@ -191,4 +210,4 @@ static void onWifiDisconnected(const WiFiEventStationModeDisconnected& event) { wifiTimer.once(2, wifiConnect); } -} \ No newline at end of file +} diff --git a/platformio/common/libs/main/homekit/main.h b/platformio/common/libs/main/homekit/main.h index a503dd0..78a0695 100644 --- a/platformio/common/libs/main/homekit/main.h +++ b/platformio/common/libs/main/homekit/main.h @@ -10,8 +10,10 @@ #include #include #ifndef CONFIG_TARGET_ESP01 +#ifndef CONFIG_NO_RECOVERY #include #endif +#endif #include #include @@ -20,6 +22,7 @@ namespace homekit::main { #ifndef CONFIG_TARGET_ESP01 +#ifndef CONFIG_NO_RECOVERY enum class WorkingMode { RECOVERY, // AP mode, http server with configuration NORMAL, // MQTT client @@ -27,6 +30,7 @@ enum class WorkingMode { extern enum WorkingMode working_mode; #endif +#endif enum class WiFiConnectionState { WAITING = 0, -- cgit v1.2.3 From a6d8ba93056c1a4e243d56da447e241b2504fae2 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:20:37 +0300 Subject: move files again --- platformio/common/libs/main/homekit/main.cpp | 213 --------------------------- platformio/common/libs/main/homekit/main.h | 52 ------- 2 files changed, 265 deletions(-) delete mode 100644 platformio/common/libs/main/homekit/main.cpp delete mode 100644 platformio/common/libs/main/homekit/main.h (limited to 'platformio/common/libs/main/homekit') diff --git a/platformio/common/libs/main/homekit/main.cpp b/platformio/common/libs/main/homekit/main.cpp deleted file mode 100644 index 816c764..0000000 --- a/platformio/common/libs/main/homekit/main.cpp +++ /dev/null @@ -1,213 +0,0 @@ -#include "./main.h" -#include -#include -#include -#include - -namespace homekit::main { - -#ifndef CONFIG_TARGET_ESP01 -#ifndef CONFIG_NO_RECOVERY -enum WorkingMode working_mode = WorkingMode::NORMAL; -#endif -#endif - -static const uint16_t recovery_boot_detection_ms = 2000; -static const uint8_t recovery_boot_delay_ms = 100; - -static volatile enum WiFiConnectionState wifi_state = WiFiConnectionState::WAITING; -static void* service = nullptr; -static WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler; -static Ticker wifiTimer; -static mqtt::MqttDiagnosticsModule* mqttDiagModule; -static mqtt::MqttOtaModule* mqttOtaModule; - -#if MQTT_BLINK -static StopWatch blinkStopWatch; -#endif - -#ifndef CONFIG_TARGET_ESP01 -#ifndef CONFIG_NO_RECOVERY -static DNSServer* dnsServer = nullptr; -#endif -#endif - -static void onWifiConnected(const WiFiEventStationModeGotIP& event); -static void onWifiDisconnected(const WiFiEventStationModeDisconnected& event); - -static void wifiConnect() { - const char *ssid, *psk, *hostname; - auto cfg = config::read(); - wifi::getConfig(cfg, &ssid, &psk, &hostname); - - PRINTF("Wi-Fi STA creds: ssid=%s, psk=%s, hostname=%s\n", ssid, psk, hostname); - - wifi_state = WiFiConnectionState::WAITING; - - WiFi.mode(WIFI_STA); - WiFi.hostname(hostname); - WiFi.begin(ssid, psk); - - PRINT("connecting to wifi.."); -} - -#ifndef CONFIG_TARGET_ESP01 -#ifndef CONFIG_NO_RECOVERY -static void wifiHotspot() { - led::mcu_led->on(); - - auto scanResults = wifi::scan(); - - WiFi.mode(WIFI_AP); - WiFi.softAP(wifi::AP_SSID); - - dnsServer = new DNSServer(); - dnsServer->start(53, "*", WiFi.softAPIP()); - - service = new HttpServer(scanResults); - ((HttpServer*)service)->start(); -} - -static void waitForRecoveryPress() { - pinMode(CONFIG_FLASH_GPIO, INPUT_PULLUP); - for (uint16_t i = 0; i < recovery_boot_detection_ms; i += recovery_boot_delay_ms) { - delay(recovery_boot_delay_ms); - if (digitalRead(CONFIG_FLASH_GPIO) == LOW) { - working_mode = WorkingMode::RECOVERY; - break; - } - } -} -#endif -#endif - - -void setup() { - WiFi.disconnect(); -#ifndef CONFIG_NO_RECOVERY -#ifndef CONFIG_TARGET_ESP01 - homekit::main::waitForRecoveryPress(); -#endif -#endif - -#ifdef DEBUG - Serial.begin(115200); -#endif - - auto cfg = config::read(); - if (config::isDirty(cfg)) { - PRINTLN("config is dirty, erasing..."); - config::erase(cfg); -#ifdef CONFIG_TARGET_NODEMCU - led::board_led->blink(10, 50); -#else - led::mcu_led->blink(10, 50); -#endif - } - -#ifndef CONFIG_TARGET_ESP01 -#ifndef CONFIG_NO_RECOVERY - switch (working_mode) { - case WorkingMode::RECOVERY: - wifiHotspot(); - break; - - case WorkingMode::NORMAL: -#endif -#endif - wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnected); - wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnected); - wifiConnect(); -#ifndef CONFIG_NO_RECOVERY -#ifndef CONFIG_TARGET_ESP01 - break; - } -#endif -#endif -} - -void loop(LoopConfig* config) { -#ifndef CONFIG_NO_RECOVERY -#ifndef CONFIG_TARGET_ESP01 - if (working_mode == WorkingMode::NORMAL) { -#endif -#endif - if (wifi_state == WiFiConnectionState::WAITING) { - PRINT("."); - led::mcu_led->blink(2, 50); - delay(1000); - return; - } - - if (wifi_state == WiFiConnectionState::JUST_CONNECTED) { -#ifdef CONFIG_TARGET_NODEMCU - led::board_led->blink(3, 300); -#else - led::mcu_led->blink(3, 300); -#endif - wifi_state = WiFiConnectionState::CONNECTED; - - if (service == nullptr) { - service = new mqtt::Mqtt(); - mqttDiagModule = new mqtt::MqttDiagnosticsModule(); - mqttOtaModule = new mqtt::MqttOtaModule(); - - ((mqtt::Mqtt*)service)->addModule(mqttDiagModule); - ((mqtt::Mqtt*)service)->addModule(mqttOtaModule); - - if (config != nullptr) - config->onMqttCreated(*(mqtt::Mqtt*)service); - } - - ((mqtt::Mqtt*)service)->connect(); -#if MQTT_BLINK - blinkStopWatch.save(); -#endif - } - - auto mqtt = (mqtt::Mqtt*)service; - if (static_cast(wifi_state) >= 1 && mqtt != nullptr) { - mqtt->loop(); - - if (mqttOtaModule != nullptr && mqttOtaModule->isReadyToRestart()) { - mqtt->disconnect(); - } - -#if MQTT_BLINK - // periodically blink board led - if (blinkStopWatch.elapsed(5000)) { -#ifdef CONFIG_TARGET_NODEMCU - board_led->blink(1, 10); -#endif - blinkStopWatch.save(); - } -#endif - } -#ifndef CONFIG_NO_RECOVERY -#ifndef CONFIG_TARGET_ESP01 - } else { - if (dnsServer != nullptr) - dnsServer->processNextRequest(); - - auto httpServer = (HttpServer*)service; - if (httpServer != nullptr) - httpServer->loop(); - } -#endif -#endif -} - -static void onWifiConnected(const WiFiEventStationModeGotIP& event) { - PRINTF("connected (%s)\n", WiFi.localIP().toString().c_str()); - wifi_state = WiFiConnectionState::JUST_CONNECTED; -} - -static void onWifiDisconnected(const WiFiEventStationModeDisconnected& event) { - PRINTLN("disconnected from wi-fi"); - wifi_state = WiFiConnectionState::WAITING; - if (service != nullptr) - ((mqtt::Mqtt*)service)->disconnect(); - wifiTimer.once(2, wifiConnect); -} - -} diff --git a/platformio/common/libs/main/homekit/main.h b/platformio/common/libs/main/homekit/main.h deleted file mode 100644 index 78a0695..0000000 --- a/platformio/common/libs/main/homekit/main.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef HOMEKIT_LIB_MAIN_H -#define HOMEKIT_LIB_MAIN_H - -#include -#include -#include -#include -#include - -#include -#include -#ifndef CONFIG_TARGET_ESP01 -#ifndef CONFIG_NO_RECOVERY -#include -#endif -#endif -#include -#include - -#include - -namespace homekit::main { - -#ifndef CONFIG_TARGET_ESP01 -#ifndef CONFIG_NO_RECOVERY -enum class WorkingMode { - RECOVERY, // AP mode, http server with configuration - NORMAL, // MQTT client -}; - -extern enum WorkingMode working_mode; -#endif -#endif - -enum class WiFiConnectionState { - WAITING = 0, - JUST_CONNECTED = 1, - CONNECTED = 2 -}; - - -struct LoopConfig { - std::function onMqttCreated; -}; - - -void setup(); -void loop(LoopConfig* config); - -} - -#endif //HOMEKIT_LIB_MAIN_H -- cgit v1.2.3