new params
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
#include <FanStateService.h>
|
||||
|
||||
FanStateService::FanStateService(AsyncWebServer* server,
|
||||
SecurityManager* securityManager) :
|
||||
_httpEndpoint(FanState::read,
|
||||
FanState::update,
|
||||
FanStateService::FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
||||
_httpEndpoint(FanSettings::read,
|
||||
FanSettings::update,
|
||||
this,
|
||||
server,
|
||||
FAN_SETTINGS_ENDPOINT_PATH,
|
||||
securityManager,
|
||||
AuthenticationPredicates::IS_AUTHENTICATED) {
|
||||
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||
_fsPersistence(FanSettings::read,
|
||||
FanSettings::update,
|
||||
this,
|
||||
fs,
|
||||
FAN_SETTINGS_FILE) {
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
// configure settings service update handler to update LED state
|
||||
addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false);
|
||||
@@ -18,11 +22,17 @@ FanStateService::FanStateService(AsyncWebServer* server,
|
||||
void FanStateService::begin() {
|
||||
_state.fanStatus = DEFAULT_FAN_STATE;
|
||||
Serial.print("Starting The Fan Service");
|
||||
_fsPersistence.readFromFS();
|
||||
onConfigUpdated();
|
||||
}
|
||||
|
||||
void FanStateService::onConfigUpdated() {
|
||||
Serial.print(" ** Fan UPDATE ");
|
||||
digitalWrite(FAN_PIN, _state.fanStatus ? FAN_ON : FAN_OFF);
|
||||
//digitalWrite(FAN_PIN, _state.fanStatus ? FAN_ON : FAN_OFF);
|
||||
analogWrite(FAN_PIN, _state.fanMaxSpeed);
|
||||
}
|
||||
|
||||
void FanStateService::save() {
|
||||
Serial.print(" ** Fan Write FS ");
|
||||
_fsPersistence.writeToFS();
|
||||
}
|
||||
@@ -1,16 +1,20 @@
|
||||
#ifndef FanStateService_h
|
||||
#define FanStateService_h
|
||||
|
||||
|
||||
#include <FSPersistence.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"
|
||||
#define DEFAULT_FAN_STATE false
|
||||
#define DEFAULT_FAN_GPIO 13
|
||||
#define DEFAULT_THRES_LOW 25
|
||||
#define DEFAULT_THRES_HIGH 70
|
||||
#define DEFAULT_FAN_MAX_SPEED 1024
|
||||
#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).
|
||||
@@ -22,37 +26,54 @@
|
||||
#define FAN_OFF 0x1
|
||||
#endif
|
||||
|
||||
#define FAN_SETTINGS_FILE "/config/fanSettings.json"
|
||||
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
||||
|
||||
|
||||
class FanState {
|
||||
class FanSettings {
|
||||
public:
|
||||
bool fanStatus;
|
||||
int fanSpeed;
|
||||
int fanGpio;
|
||||
int fanMaxSpeed;
|
||||
int thresLow;
|
||||
int thresHigh;
|
||||
|
||||
static void read(FanState& settings, JsonObject& root) {
|
||||
root["fan_status"] = settings.fanStatus;
|
||||
static void read(FanSettings& settings, JsonObject& root) {
|
||||
root["fan_status"] = settings.fanStatus;
|
||||
root["fan_gpio"] = settings.fanGpio;
|
||||
root["fan_max_speed"] = settings.fanMaxSpeed;
|
||||
root["temperature_thres_low"] = settings.thresLow;
|
||||
root["temperature_thres_high"] = settings.thresHigh;
|
||||
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;
|
||||
static StateUpdateResult update(JsonObject& root, FanSettings& fanState) {
|
||||
boolean newFanState = root["fan_status"] | DEFAULT_FAN_STATE;
|
||||
int newFanGpio = root["fan_gpio"] | DEFAULT_FAN_GPIO;
|
||||
int newMaxSpeed = root["fan_max_speed"] | DEFAULT_FAN_MAX_SPEED;
|
||||
int newThresLow = root["temperature_thres_low"] | DEFAULT_THRES_LOW;
|
||||
int newThresHigh = root["temperature_thres_high"] | DEFAULT_THRES_HIGH;
|
||||
Serial.print(" ** Fan update [" + (String)newFanState + "]\n");
|
||||
if (fanState.fanStatus != newFanState || fanState.fanGpio != newFanGpio ) {
|
||||
fanState.fanStatus = newFanState;
|
||||
fanState.fanGpio = newFanGpio;
|
||||
fanState.fanMaxSpeed = newMaxSpeed;
|
||||
fanState.thresLow = newThresLow;
|
||||
fanState.thresHigh = newThresHigh;
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
return StateUpdateResult::UNCHANGED;
|
||||
}
|
||||
|
||||
static void haRead(FanState& settings, JsonObject& root) {
|
||||
static void haRead(FanSettings& 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) {
|
||||
static StateUpdateResult haUpdate(JsonObject& root, FanSettings& fanState) {
|
||||
String state = root["state"];
|
||||
String s = root["state"];
|
||||
Serial.print(" ** Fan haUpdate [" + s + "] \n");
|
||||
@@ -72,13 +93,15 @@ class FanState {
|
||||
}
|
||||
};
|
||||
|
||||
class FanStateService : public StatefulService<FanState> {
|
||||
class FanStateService : public StatefulService<FanSettings> {
|
||||
public:
|
||||
FanStateService(AsyncWebServer* server, SecurityManager* securityManager);
|
||||
FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||
void begin();
|
||||
void save();
|
||||
|
||||
private:
|
||||
HttpEndpoint<FanState> _httpEndpoint;
|
||||
HttpEndpoint<FanSettings> _httpEndpoint;
|
||||
FSPersistence<FanSettings> _fsPersistence;
|
||||
|
||||
void registerConfig();
|
||||
void onConfigUpdated();
|
||||
|
||||
17
src/main.cpp
17
src/main.cpp
@@ -8,7 +8,7 @@
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
|
||||
AsyncWebServer server(80);
|
||||
ESP8266React esp8266React(&server);
|
||||
// ESP8266React esp8266React(&server);
|
||||
/*
|
||||
LightMqttSettingsService lightMqttSettingsService =
|
||||
LightMqttSettingsService(&server, esp8266React.getFS(), esp8266React.getSecurityManager());
|
||||
@@ -20,7 +20,20 @@ LightStateService lightStateService = LightStateService(&server,
|
||||
|
||||
*/
|
||||
|
||||
FanStateService fanStateService = FanStateService(&server,esp8266React.getSecurityManager());
|
||||
/*
|
||||
#ifdef ESP32
|
||||
SPIFFS.begin(true);
|
||||
ESP8266React EMSESP::esp8266React(&server, &LittleFS);
|
||||
FanStateService fanStateService = FanStateService(&server,&SPIFFS,esp8266React.getSecurityManager());
|
||||
#elif defined(ESP8266)
|
||||
LittleFS.begin();
|
||||
ESP8266React EMSESP::esp8266React(&server, &LittleFS);
|
||||
FanStateService fanStateService = FanStateService(&server,&LittleFS,esp8266React.getSecurityManager());
|
||||
#endif
|
||||
*/
|
||||
ESP8266React esp8266React(&server);
|
||||
FanStateService fanStateService = FanStateService(&server,esp8266React.getFS(),esp8266React.getSecurityManager());
|
||||
|
||||
|
||||
void setup() {
|
||||
// start serial and filesystem
|
||||
|
||||
Reference in New Issue
Block a user