updateNaming
This commit is contained in:
@@ -12,7 +12,9 @@
|
|||||||
extra_configs =
|
extra_configs =
|
||||||
factory_settings.ini
|
factory_settings.ini
|
||||||
features.ini
|
features.ini
|
||||||
default_envs = esp12e
|
default_envs =
|
||||||
|
esp12e
|
||||||
|
node32s
|
||||||
|
|
||||||
[env]
|
[env]
|
||||||
build_flags =
|
build_flags =
|
||||||
@@ -25,8 +27,7 @@ build_flags =
|
|||||||
lib_compat_mode = strict
|
lib_compat_mode = strict
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
extra_scripts =
|
extra_scripts = pre:scripts/build_interface.py
|
||||||
pre:scripts/build_interface.py
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
ArduinoJson@>=6.0.0,<7.0.0
|
ArduinoJson@>=6.0.0,<7.0.0
|
||||||
ESP Async WebServer@>=1.2.0,<2.0.0
|
ESP Async WebServer@>=1.2.0,<2.0.0
|
||||||
|
|||||||
16
src/FanMqttSettingsService.cpp
Normal file
16
src/FanMqttSettingsService.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <FanMqttSettingsService.h>
|
||||||
|
|
||||||
|
FanMqttSettingsService::FanMqttSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
||||||
|
_httpEndpoint(FanMqttSettings::read,
|
||||||
|
FanMqttSettings::update,
|
||||||
|
this,
|
||||||
|
server,
|
||||||
|
LIGHT_BROKER_SETTINGS_PATH,
|
||||||
|
securityManager,
|
||||||
|
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||||
|
_fsPersistence(FanMqttSettings::read, FanMqttSettings::update, this, fs, LIGHT_BROKER_SETTINGS_FILE) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void FanMqttSettingsService::begin() {
|
||||||
|
_fsPersistence.readFromFS();
|
||||||
|
}
|
||||||
41
src/FanMqttSettingsService.h
Normal file
41
src/FanMqttSettingsService.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef FanMqttSettingsService_h
|
||||||
|
#define FanMqttSettingsService_h
|
||||||
|
|
||||||
|
#include <HttpEndpoint.h>
|
||||||
|
#include <FSPersistence.h>
|
||||||
|
#include <SettingValue.h>
|
||||||
|
|
||||||
|
#define LIGHT_BROKER_SETTINGS_FILE "/config/brokerSettings.json"
|
||||||
|
#define LIGHT_BROKER_SETTINGS_PATH "/rest/brokerSettings"
|
||||||
|
|
||||||
|
class FanMqttSettings {
|
||||||
|
public:
|
||||||
|
String mqttPath;
|
||||||
|
String name;
|
||||||
|
String uniqueId;
|
||||||
|
|
||||||
|
static void read(FanMqttSettings& settings, JsonObject& root) {
|
||||||
|
root["mqtt_path"] = settings.mqttPath;
|
||||||
|
root["name"] = settings.name;
|
||||||
|
root["unique_id"] = settings.uniqueId;
|
||||||
|
}
|
||||||
|
|
||||||
|
static StateUpdateResult update(JsonObject& root, FanMqttSettings& settings) {
|
||||||
|
settings.mqttPath = root["mqtt_path"] | SettingValue::format("homeassistant/fan/#{unique_id}");
|
||||||
|
settings.name = root["name"] | SettingValue::format("light-#{unique_id}");
|
||||||
|
settings.uniqueId = root["unique_id"] | SettingValue::format("light-#{unique_id}");
|
||||||
|
return StateUpdateResult::CHANGED;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class FanMqttSettingsService : public StatefulService<FanMqttSettings> {
|
||||||
|
public:
|
||||||
|
FanMqttSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
|
void begin();
|
||||||
|
|
||||||
|
private:
|
||||||
|
HttpEndpoint<FanMqttSettings> _httpEndpoint;
|
||||||
|
FSPersistence<FanMqttSettings> _fsPersistence;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // end FanMqttSettingsService_h
|
||||||
@@ -4,16 +4,17 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
FanStateService::FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
FanStateService::FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, AsyncMqttClient* mqttClient, FanMqttSettingsService* fanMQTTSettingsService) :
|
||||||
_httpEndpoint(FanSettings::read,
|
_httpEndpoint(FanState::read,
|
||||||
FanSettings::update,
|
FanState::update,
|
||||||
this,
|
this,
|
||||||
server,
|
server,
|
||||||
FAN_SETTINGS_ENDPOINT_PATH,
|
FAN_SETTINGS_ENDPOINT_PATH,
|
||||||
securityManager,
|
securityManager,
|
||||||
AuthenticationPredicates::IS_AUTHENTICATED),
|
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||||
_fsPersistence(FanSettings::read,
|
_mqttPubSub(FanState::haRead, FanState::haUpdate, this, mqttClient),
|
||||||
FanSettings::update,
|
_fsPersistence(FanState::read,
|
||||||
|
FanState::update,
|
||||||
this,
|
this,
|
||||||
fs,
|
fs,
|
||||||
FAN_SETTINGS_FILE) {
|
FAN_SETTINGS_FILE) {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <FSPersistence.h>
|
#include <FSPersistence.h>
|
||||||
#include <HttpEndpoint.h>
|
#include <HttpEndpoint.h>
|
||||||
|
#include <FanMqttSettingsService.h>
|
||||||
|
#include <MqttPubSub.h>
|
||||||
|
|
||||||
#include <OneWire.h>
|
#include <OneWire.h>
|
||||||
#include <DallasTemperature.h>
|
#include <DallasTemperature.h>
|
||||||
@@ -33,7 +35,7 @@
|
|||||||
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
||||||
|
|
||||||
|
|
||||||
class FanSettings {
|
class FanState {
|
||||||
public:
|
public:
|
||||||
bool fanStatus;
|
bool fanStatus;
|
||||||
int fanSpeed;
|
int fanSpeed;
|
||||||
@@ -47,7 +49,7 @@ class FanSettings {
|
|||||||
float sensorInTemp;
|
float sensorInTemp;
|
||||||
float sensorOutTemp;
|
float sensorOutTemp;
|
||||||
|
|
||||||
static void read(FanSettings& settings, JsonObject& root) {
|
static void read(FanState& settings, JsonObject& root) {
|
||||||
root["oneWire_gpio"] = settings.OneWireGPIO;
|
root["oneWire_gpio"] = settings.OneWireGPIO;
|
||||||
root["sensor_in_temp"] = settings.sensorInTemp;
|
root["sensor_in_temp"] = settings.sensorInTemp;
|
||||||
root["sensor_out_temp"] = settings.sensorOutTemp;
|
root["sensor_out_temp"] = settings.sensorOutTemp;
|
||||||
@@ -67,7 +69,7 @@ class FanSettings {
|
|||||||
String y = root["oneWire_gpio"]; Serial.println(" ** Fan read [oneWire_gpio : " + y + "]");
|
String y = root["oneWire_gpio"]; Serial.println(" ** Fan read [oneWire_gpio : " + y + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static StateUpdateResult update(JsonObject& root, FanSettings& fanState) {
|
static StateUpdateResult update(JsonObject& root, FanState& fanState) {
|
||||||
boolean newFanState = root["fan_status"] | DEFAULT_FAN_STATE;
|
boolean newFanState = root["fan_status"] | DEFAULT_FAN_STATE;
|
||||||
int newFanPwmGPIO = root["fan_pwm_gpio"] | DEFAULT_FAN_PWM_GPIO;
|
int newFanPwmGPIO = root["fan_pwm_gpio"] | DEFAULT_FAN_PWM_GPIO;
|
||||||
int newFanTachGPIO = root["fan_tach_gpio"] | DEFAULT_FAN_TACH_GPIO;
|
int newFanTachGPIO = root["fan_tach_gpio"] | DEFAULT_FAN_TACH_GPIO;
|
||||||
@@ -97,13 +99,13 @@ class FanSettings {
|
|||||||
return StateUpdateResult::UNCHANGED;
|
return StateUpdateResult::UNCHANGED;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void haRead(FanSettings& settings, JsonObject& root) {
|
static void haRead(FanState& settings, JsonObject& root) {
|
||||||
root["state"] = settings.fanStatus ? ON_STATE : OFF_STATE;
|
root["state"] = settings.fanStatus ? ON_STATE : OFF_STATE;
|
||||||
String s = root["state"];
|
String s = root["state"];
|
||||||
Serial.println(" ** Fan haRead [" + s + "]");
|
Serial.println(" ** Fan haRead [" + s + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static StateUpdateResult haUpdate(JsonObject& root, FanSettings& fanState) {
|
static StateUpdateResult haUpdate(JsonObject& root, FanState& fanState) {
|
||||||
String state = root["state"];
|
String state = root["state"];
|
||||||
String s = root["state"];
|
String s = root["state"];
|
||||||
Serial.println(" ** Fan haUpdate [" + s + "]");
|
Serial.println(" ** Fan haUpdate [" + s + "]");
|
||||||
@@ -123,7 +125,7 @@ class FanSettings {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class FanStateService : public StatefulService<FanSettings> {
|
class FanStateService : public StatefulService<FanState> {
|
||||||
public:
|
public:
|
||||||
unsigned long lastpoll;
|
unsigned long lastpoll;
|
||||||
float Tin;
|
float Tin;
|
||||||
@@ -133,7 +135,7 @@ class FanStateService : public StatefulService<FanSettings> {
|
|||||||
unsigned int RPM;
|
unsigned int RPM;
|
||||||
|
|
||||||
|
|
||||||
FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, AsyncMqttClient* mqttClient, FanMqttSettingsService* fanMQTTSettingsService);
|
||||||
void begin();
|
void begin();
|
||||||
void save();
|
void save();
|
||||||
void compute();
|
void compute();
|
||||||
@@ -141,8 +143,12 @@ class FanStateService : public StatefulService<FanSettings> {
|
|||||||
int getTachGPIO();
|
int getTachGPIO();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HttpEndpoint<FanSettings> _httpEndpoint;
|
HttpEndpoint<FanState> _httpEndpoint;
|
||||||
FSPersistence<FanSettings> _fsPersistence;
|
FSPersistence<FanState> _fsPersistence;
|
||||||
|
|
||||||
|
FanMqttSettingsService* _fanMQTTSettingsService;
|
||||||
|
AsyncMqttClient* _mqttClient;
|
||||||
|
MqttPubSub<FanState> _mqttPubSub;
|
||||||
|
|
||||||
void registerConfig();
|
void registerConfig();
|
||||||
void onConfigUpdated();
|
void onConfigUpdated();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <LightStateService.h>
|
#include <LightStateService.h>
|
||||||
|
|
||||||
|
|
||||||
LightStateService::LightStateService(AsyncWebServer* server,
|
LightStateService::LightStateService(AsyncWebServer* server,
|
||||||
SecurityManager* securityManager,
|
SecurityManager* securityManager,
|
||||||
AsyncMqttClient* mqttClient,
|
AsyncMqttClient* mqttClient,
|
||||||
|
|||||||
31
src/main.cpp
31
src/main.cpp
@@ -4,7 +4,7 @@
|
|||||||
#include <LightStateService.h>
|
#include <LightStateService.h>
|
||||||
*/
|
*/
|
||||||
#include <FanStateService.h>
|
#include <FanStateService.h>
|
||||||
|
#include <FanMqttSettingsService.h>
|
||||||
|
|
||||||
|
|
||||||
#define ONE_WIRE_BUS 14
|
#define ONE_WIRE_BUS 14
|
||||||
@@ -15,7 +15,20 @@
|
|||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
ESP8266React esp8266React(&server);
|
ESP8266React esp8266React(&server);
|
||||||
FanStateService fanStateService = FanStateService(&server,esp8266React.getFS(),esp8266React.getSecurityManager());
|
|
||||||
|
FanMqttSettingsService fanMqttSettingsService = FanMqttSettingsService(
|
||||||
|
&server,
|
||||||
|
esp8266React.getFS(),
|
||||||
|
esp8266React.getSecurityManager()
|
||||||
|
);
|
||||||
|
FanStateService fanStateService = FanStateService (
|
||||||
|
&server,
|
||||||
|
esp8266React.getFS(),
|
||||||
|
esp8266React.getSecurityManager(),
|
||||||
|
esp8266React.getMqttClient(),
|
||||||
|
&fanMqttSettingsService
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ICACHE_RAM_ATTR handleInterrupt() {
|
void ICACHE_RAM_ATTR handleInterrupt() {
|
||||||
@@ -41,28 +54,16 @@ void setup() {
|
|||||||
// start the light service
|
// start the light service
|
||||||
lightMqttSettingsService.begin();
|
lightMqttSettingsService.begin();
|
||||||
*/
|
*/
|
||||||
|
fanMqttSettingsService.begin();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// pinMode(14,INPUT); // 14 = D5
|
|
||||||
// attachInterrupt(digitalPinToInterrupt(14), handleInterrupt, RISING);
|
|
||||||
|
|
||||||
Serial.println("================= SETUP ================== ");
|
|
||||||
Serial.println("Configure pin " + (String)fanStateService.getTachGPIO());
|
|
||||||
pinMode(fanStateService.getTachGPIO(),INPUT);
|
pinMode(fanStateService.getTachGPIO(),INPUT);
|
||||||
attachInterrupt(fanStateService.getTachGPIO(),handleInterrupt,RISING);
|
attachInterrupt(fanStateService.getTachGPIO(),handleInterrupt,RISING);
|
||||||
|
|
||||||
Serial.println("================= END ================== ");
|
|
||||||
|
|
||||||
|
|
||||||
// start the server
|
// start the server
|
||||||
server.begin();
|
server.begin();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// run the framework's loop function
|
// run the framework's loop function
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user