From c9004e13cbbf1be15a571b9ac8088c16abae29e0 Mon Sep 17 00:00:00 2001 From: feeling Date: Thu, 4 Feb 2021 14:01:29 +0100 Subject: [PATCH] updateNaming --- platformio.ini | 7 +++--- src/FanMqttSettingsService.cpp | 16 +++++++++++++ src/FanMqttSettingsService.h | 41 ++++++++++++++++++++++++++++++++++ src/FanStateService.cpp | 13 ++++++----- src/FanStateService.h | 24 ++++++++++++-------- src/LightStateService.cpp | 1 + src/main.cpp | 31 ++++++++++++------------- 7 files changed, 100 insertions(+), 33 deletions(-) create mode 100644 src/FanMqttSettingsService.cpp create mode 100644 src/FanMqttSettingsService.h diff --git a/platformio.ini b/platformio.ini index 44dfb98..b7ce9d9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/FanMqttSettingsService.cpp b/src/FanMqttSettingsService.cpp new file mode 100644 index 0000000..691d44f --- /dev/null +++ b/src/FanMqttSettingsService.cpp @@ -0,0 +1,16 @@ +#include + +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(); +} diff --git a/src/FanMqttSettingsService.h b/src/FanMqttSettingsService.h new file mode 100644 index 0000000..9a07e89 --- /dev/null +++ b/src/FanMqttSettingsService.h @@ -0,0 +1,41 @@ +#ifndef FanMqttSettingsService_h +#define FanMqttSettingsService_h + +#include +#include +#include + +#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 { + public: + FanMqttSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); + void begin(); + + private: + HttpEndpoint _httpEndpoint; + FSPersistence _fsPersistence; +}; + +#endif // end FanMqttSettingsService_h diff --git a/src/FanStateService.cpp b/src/FanStateService.cpp index 074ab04..605fafb 100644 --- a/src/FanStateService.cpp +++ b/src/FanStateService.cpp @@ -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) { @@ -65,6 +66,6 @@ void FanStateService::compute() { this->Tin = this->sensor_intake.getTempCByIndex(0); this->Tout = this->sensor_intake.getTempCByIndex(1); float curRpm = 60000/period*this->RPM/2; // Hall sensor = 2 tick per rotation - Serial.print("Intake: " + (String) this->Tin + " - " + (String) this->Tout + " ["+ (String) curRpm + " RPM ]\n"); + Serial.print("Intake: " + (String) this->Tin + " - " + (String) this->Tout + " [ "+ (String) curRpm + " RPM ]\n"); this->RPM=0; } \ No newline at end of file diff --git a/src/FanStateService.h b/src/FanStateService.h index 9102230..ffa498b 100644 --- a/src/FanStateService.h +++ b/src/FanStateService.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include @@ -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 { +class FanStateService : public StatefulService { public: unsigned long lastpoll; float Tin; @@ -133,7 +135,7 @@ class FanStateService : public StatefulService { 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 { int getTachGPIO(); private: - HttpEndpoint _httpEndpoint; - FSPersistence _fsPersistence; + HttpEndpoint _httpEndpoint; + FSPersistence _fsPersistence; + + FanMqttSettingsService* _fanMQTTSettingsService; + AsyncMqttClient* _mqttClient; + MqttPubSub _mqttPubSub; void registerConfig(); void onConfigUpdated(); diff --git a/src/LightStateService.cpp b/src/LightStateService.cpp index 8169622..0d6a05a 100644 --- a/src/LightStateService.cpp +++ b/src/LightStateService.cpp @@ -1,5 +1,6 @@ #include + LightStateService::LightStateService(AsyncWebServer* server, SecurityManager* securityManager, AsyncMqttClient* mqttClient, diff --git a/src/main.cpp b/src/main.cpp index 354c280..82775c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include */ #include - +#include #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