From fb9d5637bee9179ba9d2ae6c3ecb0b623334ba3d Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Tue, 23 Apr 2024 20:34:02 +0200 Subject: [PATCH] Removes the lightdemo --- esp32/src/LightMqttSettingsService.cpp | 38 --------- esp32/src/LightMqttSettingsService.h | 59 -------------- esp32/src/LightStateService.cpp | 107 ------------------------- esp32/src/LightStateService.h | 107 ------------------------- esp32/src/main.cpp | 16 ---- 5 files changed, 327 deletions(-) delete mode 100644 esp32/src/LightMqttSettingsService.cpp delete mode 100644 esp32/src/LightMqttSettingsService.h delete mode 100644 esp32/src/LightStateService.cpp delete mode 100644 esp32/src/LightStateService.h diff --git a/esp32/src/LightMqttSettingsService.cpp b/esp32/src/LightMqttSettingsService.cpp deleted file mode 100644 index 8ff2d04..0000000 --- a/esp32/src/LightMqttSettingsService.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/** - * ESP32 SvelteKit - * - * A simple, secure and extensible framework for IoT projects for ESP32 platforms - * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. - * https://github.com/theelims/ESP32-sveltekit - * - * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims - * - * All Rights Reserved. This software may be modified and distributed under - * the terms of the LGPL v3 license. See the LICENSE file for details. - **/ - -#include - -LightMqttSettingsService::LightMqttSettingsService(PsychicHttpServer *server, - FS *fs, - SecurityManager *securityManager) : _httpEndpoint(LightMqttSettings::read, - LightMqttSettings::update, - this, - server, - LIGHT_BROKER_SETTINGS_PATH, - securityManager, - AuthenticationPredicates::IS_AUTHENTICATED), - _fsPersistence(LightMqttSettings::read, - LightMqttSettings::update, - this, - fs, - LIGHT_BROKER_SETTINGS_FILE) -{ -} - -void LightMqttSettingsService::begin() -{ - _httpEndpoint.begin(); - _fsPersistence.readFromFS(); -} diff --git a/esp32/src/LightMqttSettingsService.h b/esp32/src/LightMqttSettingsService.h deleted file mode 100644 index ec53909..0000000 --- a/esp32/src/LightMqttSettingsService.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef LightMqttSettingsService_h -#define LightMqttSettingsService_h - -/** - * ESP32 SvelteKit - * - * A simple, secure and extensible framework for IoT projects for ESP32 platforms - * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. - * https://github.com/theelims/ESP32-sveltekit - * - * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims - * - * All Rights Reserved. This software may be modified and distributed under - * the terms of the LGPL v3 license. See the LICENSE file for details. - **/ - -#include -#include -#include - -#define LIGHT_BROKER_SETTINGS_FILE "/config/brokerSettings.json" -#define LIGHT_BROKER_SETTINGS_PATH "/rest/brokerSettings" - -class LightMqttSettings -{ -public: - String mqttPath; - String name; - String uniqueId; - - static void read(LightMqttSettings &settings, JsonObject &root) - { - root["mqtt_path"] = settings.mqttPath; - root["name"] = settings.name; - root["unique_id"] = settings.uniqueId; - } - - static StateUpdateResult update(JsonObject &root, LightMqttSettings &settings) - { - settings.mqttPath = root["mqtt_path"] | SettingValue::format("homeassistant/light/#{unique_id}"); - settings.name = root["name"] | SettingValue::format("light-#{unique_id}"); - settings.uniqueId = root["unique_id"] | SettingValue::format("light-#{unique_id}"); - return StateUpdateResult::CHANGED; - } -}; - -class LightMqttSettingsService : public StatefulService -{ -public: - LightMqttSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager); - void begin(); - -private: - HttpEndpoint _httpEndpoint; - FSPersistence _fsPersistence; -}; - -#endif // end LightMqttSettingsService_h diff --git a/esp32/src/LightStateService.cpp b/esp32/src/LightStateService.cpp deleted file mode 100644 index 629888d..0000000 --- a/esp32/src/LightStateService.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/** - * ESP32 SvelteKit - * - * A simple, secure and extensible framework for IoT projects for ESP32 platforms - * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. - * https://github.com/theelims/ESP32-sveltekit - * - * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims - * - * All Rights Reserved. This software may be modified and distributed under - * the terms of the LGPL v3 license. See the LICENSE file for details. - **/ - -#include - -LightStateService::LightStateService(PsychicHttpServer *server, - EventSocket *socket, - SecurityManager *securityManager, - PsychicMqttClient *mqttClient, - LightMqttSettingsService *lightMqttSettingsService) : _httpEndpoint(LightState::read, - LightState::update, - this, - server, - LIGHT_SETTINGS_ENDPOINT_PATH, - securityManager, - AuthenticationPredicates::IS_AUTHENTICATED), - _eventEndpoint(LightState::read, - LightState::update, - this, - socket, - LIGHT_SETTINGS_EVENT, - LIGHT_SETTINGS_MAX_BUFFER_SIZE), - _mqttEndpoint(LightState::homeAssistRead, - LightState::homeAssistUpdate, - this, - mqttClient), - _webSocketServer(LightState::read, - LightState::update, - this, - server, - LIGHT_SETTINGS_SOCKET_PATH, - securityManager, - AuthenticationPredicates::IS_AUTHENTICATED), - _mqttClient(mqttClient), - _lightMqttSettingsService(lightMqttSettingsService) -{ - // configure led to be output - pinMode(LED_BUILTIN, OUTPUT); - - // configure MQTT callback - _mqttClient->onConnect(std::bind(&LightStateService::registerConfig, this)); - - // configure update handler for when the light settings change - _lightMqttSettingsService->addUpdateHandler([&](const String &originId) - { registerConfig(); }, - false); - - // configure settings service update handler to update LED state - addUpdateHandler([&](const String &originId) - { onConfigUpdated(); }, - false); -} - -void LightStateService::begin() -{ - _httpEndpoint.begin(); - _eventEndpoint.begin(); - _state.ledOn = DEFAULT_LED_STATE; - onConfigUpdated(); -} - -void LightStateService::onConfigUpdated() -{ - digitalWrite(LED_BUILTIN, _state.ledOn ? 1 : 0); -} - -void LightStateService::registerConfig() -{ - if (!_mqttClient->connected()) - { - return; - } - String configTopic; - String subTopic; - String pubTopic; - - DynamicJsonDocument doc(256); - _lightMqttSettingsService->read([&](LightMqttSettings &settings) - { - configTopic = settings.mqttPath + "/config"; - subTopic = settings.mqttPath + "/set"; - pubTopic = settings.mqttPath + "/state"; - doc["~"] = settings.mqttPath; - doc["name"] = settings.name; - doc["unique_id"] = settings.uniqueId; }); - doc["cmd_t"] = "~/set"; - doc["stat_t"] = "~/state"; - doc["schema"] = "json"; - doc["brightness"] = false; - - String payload; - serializeJson(doc, payload); - _mqttClient->publish(configTopic.c_str(), 0, false, payload.c_str()); - - _mqttEndpoint.configureTopics(pubTopic, subTopic); -} diff --git a/esp32/src/LightStateService.h b/esp32/src/LightStateService.h deleted file mode 100644 index 1536581..0000000 --- a/esp32/src/LightStateService.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef LightStateService_h -#define LightStateService_h - -/** - * ESP32 SvelteKit - * - * A simple, secure and extensible framework for IoT projects for ESP32 platforms - * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. - * https://github.com/theelims/ESP32-sveltekit - * - * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims - * - * All Rights Reserved. This software may be modified and distributed under - * the terms of the LGPL v3 license. See the LICENSE file for details. - **/ - -#include - -#include -#include -#include -#include -#include - -#define DEFAULT_LED_STATE false -#define OFF_STATE "OFF" -#define ON_STATE "ON" - -#define LIGHT_SETTINGS_ENDPOINT_PATH "/rest/lightState" -#define LIGHT_SETTINGS_SOCKET_PATH "/ws/lightState" -#define LIGHT_SETTINGS_EVENT "led" -#define LIGHT_SETTINGS_MAX_BUFFER_SIZE 256 - -class LightState -{ -public: - bool ledOn; - - static void read(LightState &settings, JsonObject &root) - { - root["led_on"] = settings.ledOn; - } - - static StateUpdateResult update(JsonObject &root, LightState &lightState) - { - boolean newState = root["led_on"] | DEFAULT_LED_STATE; - if (lightState.ledOn != newState) - { - lightState.ledOn = newState; - return StateUpdateResult::CHANGED; - } - return StateUpdateResult::UNCHANGED; - } - - static void homeAssistRead(LightState &settings, JsonObject &root) - { - root["state"] = settings.ledOn ? ON_STATE : OFF_STATE; - } - - static StateUpdateResult homeAssistUpdate(JsonObject &root, LightState &lightState) - { - String state = root["state"]; - // parse new led state - boolean newState = false; - if (state.equals(ON_STATE)) - { - newState = true; - } - else if (!state.equals(OFF_STATE)) - { - return StateUpdateResult::ERROR; - } - // change the new state, if required - if (lightState.ledOn != newState) - { - lightState.ledOn = newState; - return StateUpdateResult::CHANGED; - } - return StateUpdateResult::UNCHANGED; - } -}; - -class LightStateService : public StatefulService -{ -public: - LightStateService(PsychicHttpServer *server, - EventSocket *socket, - SecurityManager *securityManager, - PsychicMqttClient *mqttClient, - LightMqttSettingsService *lightMqttSettingsService); - - void begin(); - -private: - HttpEndpoint _httpEndpoint; - EventEndpoint _eventEndpoint; - MqttEndpoint _mqttEndpoint; - WebSocketServer _webSocketServer; - PsychicMqttClient *_mqttClient; - LightMqttSettingsService *_lightMqttSettingsService; - - void registerConfig(); - void onConfigUpdated(); -}; - -#endif diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 33f4d97..d179725 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1,8 +1,6 @@ #define CAMERA_MODEL_AI_THINKER #include -#include -#include #include #define SERIAL_BAUD_RATE 115200 @@ -11,16 +9,6 @@ DRAM_ATTR PsychicHttpServer server; DRAM_ATTR ESP32SvelteKit esp32sveltekit(&server, 120); -LightMqttSettingsService lightMqttSettingsService = LightMqttSettingsService(&server, - esp32sveltekit.getFS(), - esp32sveltekit.getSecurityManager()); - -LightStateService lightStateService = LightStateService(&server, - esp32sveltekit.getSocket(), - esp32sveltekit.getSecurityManager(), - esp32sveltekit.getMqttClient(), - &lightMqttSettingsService); - /* * Camera settings service * Camera state service @@ -55,10 +43,6 @@ void setup() // Wire.begin(SDA, SCL); // InitializeCamera(); - - lightStateService.begin(); - - lightMqttSettingsService.begin(); } void loop()