new params

This commit is contained in:
2021-01-12 18:31:19 +01:00
parent 2cfd2afaee
commit 1598d4934a
5 changed files with 130 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { ValidatorForm } from 'react-material-ui-form-validator';
import { ValidatorForm, TextValidator } from 'react-material-ui-form-validator';
import { Typography, Box, Checkbox } from '@material-ui/core';
import SaveIcon from '@material-ui/icons/Save';
@@ -25,7 +25,7 @@ class FanStatusRestController extends Component<FanStatusRestControllerProps> {
render() {
return (
<SectionContent title='REST Controller' titleGutter>
<SectionContent title='Fan Control Settings' titleGutter>
<RestFormLoader
{...this.props}
render={props => (
@@ -48,9 +48,10 @@ function FanStatusRestControllerForm(props: FanStatusRestControllerFormProps) {
<ValidatorForm onSubmit={saveData}>
<Box bgcolor="primary.main" color="primary.contrastText" p={2} mt={2} mb={2}>
<Typography variant="body1">
The form below controls the LED via the RESTful service exposed by the ESP device.
The form below allow you to define the fan behaviour depending of the temperature.
</Typography>
</Box>
<BlockFormControlLabel
control={
<Checkbox
@@ -59,8 +60,60 @@ function FanStatusRestControllerForm(props: FanStatusRestControllerFormProps) {
color="primary"
/>
}
label="Fan State ?"
label="Enable Fan"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:100']}
errorMessages={['Low Temperature required', "Must be a number", "Must be 0 or higher", "Max value is 100"]}
name="temperature_thres_low"
label="Low Temperature Threshold"
fullWidth
variant="outlined"
value={data.temperature_thres_low}
type="number"
onChange={handleValueChange('temperature_thres_low')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:30', 'maxNumber:200']}
errorMessages={['High Temperature required', "Must be a number", "Must be 0 or higher", "Max value is 100"]}
name="temperature_thres_high"
label="High Temperature Threshold"
fullWidth
variant="outlined"
value={data.temperature_thres_high}
type="number"
onChange={handleValueChange('temperature_thres_high')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
errorMessages={['Fan GPIO pin is required', "Must be a number", "Must be 0 or higher", "Max value is 40"]}
name="fan_gpio"
label="Fan GPIO pin"
fullWidth
variant="outlined"
value={data.fan_gpio}
type="number"
onChange={handleValueChange('fan_gpio')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:100']}
errorMessages={['Fan MAX is required', "Must be a number", "Must be 0 or higher", "Max value is 1024"]}
name="fan_max_speed"
label="Fan Max Speed"
fullWidth
variant="outlined"
value={data.fan_max_speed}
type="number"
onChange={handleValueChange('fan_max_speed')}
margin="normal"
/>
<FormActions>
<FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit">
Save

View File

@@ -13,4 +13,6 @@ export interface FanStatus {
temperature_thres_low : number;
temperature_thres_high : number;
fan_status : boolean;
fan_gpio : number;
fan_max_speed : number;
}

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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