72 lines
1.8 KiB
C
72 lines
1.8 KiB
C
|
#ifndef LightStateService_h
|
||
|
#define LightStateService_h
|
||
|
|
||
|
#include <LightMqttSettingsService.h>
|
||
|
|
||
|
#include <HttpEndpoint.h>
|
||
|
#include <MqttPubSub.h>
|
||
|
#include <WebSocketTxRx.h>
|
||
|
|
||
|
#define BLINK_LED 2
|
||
|
#define PRINT_DELAY 5000
|
||
|
|
||
|
#define DEFAULT_LED_STATE false
|
||
|
#define OFF_STATE "OFF"
|
||
|
#define ON_STATE "ON"
|
||
|
|
||
|
// Note that the built-in LED is on when the pin is low on most NodeMCU boards.
|
||
|
// This is because the anode is tied to VCC and the cathode to the GPIO 4 (Arduino pin 2).
|
||
|
#ifdef ESP32
|
||
|
#define LED_ON 0x1
|
||
|
#define LED_OFF 0x0
|
||
|
#elif defined(ESP8266)
|
||
|
#define LED_ON 0x0
|
||
|
#define LED_OFF 0x1
|
||
|
#endif
|
||
|
|
||
|
#define LIGHT_SETTINGS_ENDPOINT_PATH "/rest/lightState"
|
||
|
#define LIGHT_SETTINGS_SOCKET_PATH "/ws/lightState"
|
||
|
|
||
|
class LightState {
|
||
|
public:
|
||
|
bool ledOn;
|
||
|
|
||
|
static void serialize(LightState& settings, JsonObject& root) {
|
||
|
root["led_on"] = settings.ledOn;
|
||
|
}
|
||
|
|
||
|
static void deserialize(JsonObject& root, LightState& settings) {
|
||
|
settings.ledOn = root["led_on"] | DEFAULT_LED_STATE;
|
||
|
}
|
||
|
|
||
|
static void haSerialize(LightState& settings, JsonObject& root) {
|
||
|
root["state"] = settings.ledOn ? ON_STATE : OFF_STATE;
|
||
|
}
|
||
|
|
||
|
static void haDeserialize(JsonObject& root, LightState& settings) {
|
||
|
String state = root["state"];
|
||
|
settings.ledOn = strcmp(ON_STATE, state.c_str()) ? false : true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class LightStateService : public StatefulService<LightState> {
|
||
|
public:
|
||
|
LightStateService(AsyncWebServer* server,
|
||
|
SecurityManager* securityManager,
|
||
|
AsyncMqttClient* mqttClient,
|
||
|
LightMqttSettingsService* lightMqttSettingsService);
|
||
|
void begin();
|
||
|
|
||
|
private:
|
||
|
HttpEndpoint<LightState> _httpEndpoint;
|
||
|
MqttPubSub<LightState> _mqttPubSub;
|
||
|
WebSocketTxRx<LightState> _webSocket;
|
||
|
AsyncMqttClient* _mqttClient;
|
||
|
LightMqttSettingsService* _lightMqttSettingsService;
|
||
|
|
||
|
void registerConfig();
|
||
|
void onConfigUpdated();
|
||
|
};
|
||
|
|
||
|
#endif
|