Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.8 KiB
71 lines
1.8 KiB
#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
|