updated service
This commit is contained in:
28
src/FanStateService.cpp
Normal file
28
src/FanStateService.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <FanStateService.h>
|
||||||
|
|
||||||
|
FanStateService::FanStateService(AsyncWebServer* server,
|
||||||
|
SecurityManager* securityManager) :
|
||||||
|
_httpEndpoint(FanState::read,
|
||||||
|
FanState::update,
|
||||||
|
this,
|
||||||
|
server,
|
||||||
|
FAN_SETTINGS_ENDPOINT_PATH,
|
||||||
|
securityManager,
|
||||||
|
AuthenticationPredicates::IS_AUTHENTICATED) {
|
||||||
|
pinMode(FAN_PIN, OUTPUT);
|
||||||
|
// configure settings service update handler to update LED state
|
||||||
|
addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void FanStateService::begin() {
|
||||||
|
_state.fanStatus = DEFAULT_FAN_STATE;
|
||||||
|
Serial.print("Starting The Fan Service");
|
||||||
|
onConfigUpdated();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FanStateService::onConfigUpdated() {
|
||||||
|
Serial.print(" ** Fan UPDATE ");
|
||||||
|
digitalWrite(FAN_PIN, _state.fanStatus ? FAN_ON : FAN_OFF);
|
||||||
|
}
|
||||||
|
|
||||||
87
src/FanStateService.h
Normal file
87
src/FanStateService.h
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#ifndef FanStateService_h
|
||||||
|
#define FanStateService_h
|
||||||
|
|
||||||
|
|
||||||
|
#include <HttpEndpoint.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define FAN_PIN 13
|
||||||
|
#define PRINT_DELAY 5000
|
||||||
|
|
||||||
|
#define DEFAULT_FAN_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 FAN_ON 0x1
|
||||||
|
#define FAN_OFF 0x0
|
||||||
|
#elif defined(ESP8266)
|
||||||
|
#define FAN_ON 0x0
|
||||||
|
#define FAN_OFF 0x1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
||||||
|
|
||||||
|
|
||||||
|
class FanState {
|
||||||
|
public:
|
||||||
|
bool fanStatus;
|
||||||
|
int fanSpeed;
|
||||||
|
|
||||||
|
static void read(FanState& settings, JsonObject& root) {
|
||||||
|
root["fan_status"] = settings.fanStatus;
|
||||||
|
String s = root["fan_status"];
|
||||||
|
Serial.print(" ** Fan read [" + s + "]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static StateUpdateResult update(JsonObject& root, FanState& fanState) {
|
||||||
|
boolean newState = root["fan_status"] | DEFAULT_FAN_STATE;
|
||||||
|
Serial.print(" ** Fan update [" + (String)newState + "]\n");
|
||||||
|
if (fanState.fanStatus != newState) {
|
||||||
|
fanState.fanStatus = newState;
|
||||||
|
return StateUpdateResult::CHANGED;
|
||||||
|
}
|
||||||
|
return StateUpdateResult::UNCHANGED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void haRead(FanState& settings, JsonObject& root) {
|
||||||
|
root["state"] = settings.fanStatus ? ON_STATE : OFF_STATE;
|
||||||
|
String s = root["state"];
|
||||||
|
Serial.print(" ** Fan haRead [" + s + "] \n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static StateUpdateResult haUpdate(JsonObject& root, FanState& fanState) {
|
||||||
|
String state = root["state"];
|
||||||
|
String s = root["state"];
|
||||||
|
Serial.print(" ** Fan haUpdate [" + s + "] \n");
|
||||||
|
// 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 (fanState.fanStatus != newState) {
|
||||||
|
fanState.fanStatus = newState;
|
||||||
|
return StateUpdateResult::CHANGED;
|
||||||
|
}
|
||||||
|
return StateUpdateResult::UNCHANGED;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class FanStateService : public StatefulService<FanState> {
|
||||||
|
public:
|
||||||
|
FanStateService(AsyncWebServer* server, SecurityManager* securityManager);
|
||||||
|
void begin();
|
||||||
|
|
||||||
|
private:
|
||||||
|
HttpEndpoint<FanState> _httpEndpoint;
|
||||||
|
|
||||||
|
void registerConfig();
|
||||||
|
void onConfigUpdated();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
13
src/main.cpp
13
src/main.cpp
@@ -1,18 +1,27 @@
|
|||||||
#include <ESP8266React.h>
|
#include <ESP8266React.h>
|
||||||
|
/*
|
||||||
#include <LightMqttSettingsService.h>
|
#include <LightMqttSettingsService.h>
|
||||||
#include <LightStateService.h>
|
#include <LightStateService.h>
|
||||||
|
*/
|
||||||
|
#include <FanStateService.h>
|
||||||
|
|
||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
ESP8266React esp8266React(&server);
|
ESP8266React esp8266React(&server);
|
||||||
|
/*
|
||||||
LightMqttSettingsService lightMqttSettingsService =
|
LightMqttSettingsService lightMqttSettingsService =
|
||||||
LightMqttSettingsService(&server, esp8266React.getFS(), esp8266React.getSecurityManager());
|
LightMqttSettingsService(&server, esp8266React.getFS(), esp8266React.getSecurityManager());
|
||||||
|
|
||||||
LightStateService lightStateService = LightStateService(&server,
|
LightStateService lightStateService = LightStateService(&server,
|
||||||
esp8266React.getSecurityManager(),
|
esp8266React.getSecurityManager(),
|
||||||
esp8266React.getMqttClient(),
|
esp8266React.getMqttClient(),
|
||||||
&lightMqttSettingsService);
|
&lightMqttSettingsService);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
FanStateService fanStateService = FanStateService(&server,esp8266React.getSecurityManager());
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial and filesystem
|
// start serial and filesystem
|
||||||
Serial.begin(SERIAL_BAUD_RATE);
|
Serial.begin(SERIAL_BAUD_RATE);
|
||||||
@@ -21,10 +30,12 @@ void setup() {
|
|||||||
esp8266React.begin();
|
esp8266React.begin();
|
||||||
|
|
||||||
// load the initial light settings
|
// load the initial light settings
|
||||||
lightStateService.begin();
|
fanStateService.begin();
|
||||||
|
|
||||||
|
/*
|
||||||
// start the light service
|
// start the light service
|
||||||
lightMqttSettingsService.begin();
|
lightMqttSettingsService.begin();
|
||||||
|
*/
|
||||||
|
|
||||||
// start the server
|
// start the server
|
||||||
server.begin();
|
server.begin();
|
||||||
|
|||||||
Reference in New Issue
Block a user