updateNaming
This commit is contained in:
@@ -12,7 +12,9 @@
|
||||
extra_configs =
|
||||
factory_settings.ini
|
||||
features.ini
|
||||
default_envs = esp12e
|
||||
default_envs =
|
||||
esp12e
|
||||
node32s
|
||||
|
||||
[env]
|
||||
build_flags =
|
||||
@@ -25,8 +27,7 @@ build_flags =
|
||||
lib_compat_mode = strict
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
extra_scripts =
|
||||
pre:scripts/build_interface.py
|
||||
extra_scripts = pre:scripts/build_interface.py
|
||||
lib_deps =
|
||||
ArduinoJson@>=6.0.0,<7.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) :
|
||||
_httpEndpoint(FanSettings::read,
|
||||
FanSettings::update,
|
||||
FanStateService::FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, AsyncMqttClient* mqttClient, FanMqttSettingsService* fanMQTTSettingsService) :
|
||||
_httpEndpoint(FanState::read,
|
||||
FanState::update,
|
||||
this,
|
||||
server,
|
||||
FAN_SETTINGS_ENDPOINT_PATH,
|
||||
securityManager,
|
||||
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||
_fsPersistence(FanSettings::read,
|
||||
FanSettings::update,
|
||||
_mqttPubSub(FanState::haRead, FanState::haUpdate, this, mqttClient),
|
||||
_fsPersistence(FanState::read,
|
||||
FanState::update,
|
||||
this,
|
||||
fs,
|
||||
FAN_SETTINGS_FILE) {
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include <FSPersistence.h>
|
||||
#include <HttpEndpoint.h>
|
||||
#include <FanMqttSettingsService.h>
|
||||
#include <MqttPubSub.h>
|
||||
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
@@ -33,7 +35,7 @@
|
||||
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
||||
|
||||
|
||||
class FanSettings {
|
||||
class FanState {
|
||||
public:
|
||||
bool fanStatus;
|
||||
int fanSpeed;
|
||||
@@ -47,7 +49,7 @@ class FanSettings {
|
||||
float sensorInTemp;
|
||||
float sensorOutTemp;
|
||||
|
||||
static void read(FanSettings& settings, JsonObject& root) {
|
||||
static void read(FanState& settings, JsonObject& root) {
|
||||
root["oneWire_gpio"] = settings.OneWireGPIO;
|
||||
root["sensor_in_temp"] = settings.sensorInTemp;
|
||||
root["sensor_out_temp"] = settings.sensorOutTemp;
|
||||
@@ -67,7 +69,7 @@ class FanSettings {
|
||||
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;
|
||||
int newFanPwmGPIO = root["fan_pwm_gpio"] | DEFAULT_FAN_PWM_GPIO;
|
||||
int newFanTachGPIO = root["fan_tach_gpio"] | DEFAULT_FAN_TACH_GPIO;
|
||||
@@ -97,13 +99,13 @@ class FanSettings {
|
||||
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;
|
||||
String s = root["state"];
|
||||
Serial.println(" ** Fan haRead [" + s + "]");
|
||||
}
|
||||
|
||||
static StateUpdateResult haUpdate(JsonObject& root, FanSettings& fanState) {
|
||||
static StateUpdateResult haUpdate(JsonObject& root, FanState& fanState) {
|
||||
String state = root["state"];
|
||||
String s = root["state"];
|
||||
Serial.println(" ** Fan haUpdate [" + s + "]");
|
||||
@@ -123,7 +125,7 @@ class FanSettings {
|
||||
}
|
||||
};
|
||||
|
||||
class FanStateService : public StatefulService<FanSettings> {
|
||||
class FanStateService : public StatefulService<FanState> {
|
||||
public:
|
||||
unsigned long lastpoll;
|
||||
float Tin;
|
||||
@@ -133,7 +135,7 @@ class FanStateService : public StatefulService<FanSettings> {
|
||||
unsigned int RPM;
|
||||
|
||||
|
||||
FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||
FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, AsyncMqttClient* mqttClient, FanMqttSettingsService* fanMQTTSettingsService);
|
||||
void begin();
|
||||
void save();
|
||||
void compute();
|
||||
@@ -141,8 +143,12 @@ class FanStateService : public StatefulService<FanSettings> {
|
||||
int getTachGPIO();
|
||||
|
||||
private:
|
||||
HttpEndpoint<FanSettings> _httpEndpoint;
|
||||
FSPersistence<FanSettings> _fsPersistence;
|
||||
HttpEndpoint<FanState> _httpEndpoint;
|
||||
FSPersistence<FanState> _fsPersistence;
|
||||
|
||||
FanMqttSettingsService* _fanMQTTSettingsService;
|
||||
AsyncMqttClient* _mqttClient;
|
||||
MqttPubSub<FanState> _mqttPubSub;
|
||||
|
||||
void registerConfig();
|
||||
void onConfigUpdated();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <LightStateService.h>
|
||||
|
||||
|
||||
LightStateService::LightStateService(AsyncWebServer* server,
|
||||
SecurityManager* securityManager,
|
||||
AsyncMqttClient* mqttClient,
|
||||
|
||||
31
src/main.cpp
31
src/main.cpp
@@ -4,7 +4,7 @@
|
||||
#include <LightStateService.h>
|
||||
*/
|
||||
#include <FanStateService.h>
|
||||
|
||||
#include <FanMqttSettingsService.h>
|
||||
|
||||
|
||||
#define ONE_WIRE_BUS 14
|
||||
@@ -15,7 +15,20 @@
|
||||
AsyncWebServer server(80);
|
||||
|
||||
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() {
|
||||
@@ -41,28 +54,16 @@ void setup() {
|
||||
// start the light service
|
||||
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);
|
||||
attachInterrupt(fanStateService.getTachGPIO(),handleInterrupt,RISING);
|
||||
|
||||
Serial.println("================= END ================== ");
|
||||
|
||||
|
||||
// start the server
|
||||
server.begin();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
// run the framework's loop function
|
||||
|
||||
|
||||
Reference in New Issue
Block a user