2020-05-14 22:23:45 +00:00
|
|
|
#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;
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static void read(LightState& settings, JsonObject& root) {
|
2020-05-14 22:23:45 +00:00
|
|
|
root["led_on"] = settings.ledOn;
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
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;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static void haRead(LightState& settings, JsonObject& root) {
|
2020-05-14 22:23:45 +00:00
|
|
|
root["state"] = settings.ledOn ? ON_STATE : OFF_STATE;
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static StateUpdateResult haUpdate(JsonObject& root, LightState& lightState) {
|
2020-05-14 22:23:45 +00:00
|
|
|
String state = root["state"];
|
2020-05-29 19:18:43 +00:00
|
|
|
// 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;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|