Adds template libs

This commit is contained in:
Rune Harlyk
2024-04-22 21:07:56 +02:00
committed by Rune Harlyk
parent 0724705939
commit b804b9df1f
126 changed files with 57429 additions and 13 deletions
+38
View File
@@ -0,0 +1,38 @@
/**
* 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.h>
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();
}
+59
View File
@@ -0,0 +1,59 @@
#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 <HttpEndpoint.h>
#include <FSPersistence.h>
#include <SettingValue.h>
#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<LightMqttSettings>
{
public:
LightMqttSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
void begin();
private:
HttpEndpoint<LightMqttSettings> _httpEndpoint;
FSPersistence<LightMqttSettings> _fsPersistence;
};
#endif // end LightMqttSettingsService_h
+107
View File
@@ -0,0 +1,107 @@
/**
* 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.h>
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);
}
+107
View File
@@ -0,0 +1,107 @@
#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 <LightMqttSettingsService.h>
#include <EventSocket.h>
#include <HttpEndpoint.h>
#include <MqttEndpoint.h>
#include <EventEndpoint.h>
#include <WebSocketServer.h>
#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<LightState>
{
public:
LightStateService(PsychicHttpServer *server,
EventSocket *socket,
SecurityManager *securityManager,
PsychicMqttClient *mqttClient,
LightMqttSettingsService *lightMqttSettingsService);
void begin();
private:
HttpEndpoint<LightState> _httpEndpoint;
EventEndpoint<LightState> _eventEndpoint;
MqttEndpoint<LightState> _mqttEndpoint;
WebSocketServer<LightState> _webSocketServer;
PsychicMqttClient *_mqttClient;
LightMqttSettingsService *_lightMqttSettingsService;
void registerConfig();
void onConfigUpdated();
};
#endif
+18 -6
View File
@@ -1,16 +1,25 @@
#define CAMERA_MODEL_AI_THINKER
#include <ESP32SvelteKit.h>
#include <ActuatorStateService.h>
#include <LightMqttSettingsService.h>
#include <LightStateService.h>
#include <PsychicHttpServer.h>
#define SERIAL_BAUD_RATE 115200
DRAM_ATTR PsychicHttpServer server;
PsychicHttpServer server;
DRAM_ATTR ESP32SvelteKit esp32sveltekit(&server, 125);
ActuatorStateService actuatorStateService = ActuatorStateService(&server, esp32sveltekit.getNotificationEvents(), esp32sveltekit.getSecurityManager());
ESP32SvelteKit esp32sveltekit(&server, 120);
LightMqttSettingsService lightMqttSettingsService = LightMqttSettingsService(&server,
esp32sveltekit.getFS(),
esp32sveltekit.getSecurityManager());
LightStateService lightStateService = LightStateService(&server,
esp32sveltekit.getSocket(),
esp32sveltekit.getSecurityManager(),
esp32sveltekit.getMqttClient(),
&lightMqttSettingsService);
void setup()
{
@@ -18,10 +27,13 @@ void setup()
esp32sveltekit.begin();
actuatorStateService.begin();
lightStateService.begin();
// start the light service
lightMqttSettingsService.begin();
}
void loop()
{
// Delete Arduino loop task, as it is not needed in this example
vTaskDelete(NULL);
}