rename classes

This commit is contained in:
proddy
2020-10-22 18:58:14 +02:00
parent 71461eefd5
commit f2cc5b98ba
20 changed files with 221 additions and 226 deletions

View File

@@ -4,9 +4,9 @@
namespace emsesp { namespace emsesp {
class EMSESP { class EMSESP {
public: public:
static System system_; static System system_;
static Mqtt mqtt_; static Mqtt mqtt_;
static Sensor sensor_; static DallasSensor dallassensor_;
}; };
} // namespace emsesp } // namespace emsesp
@@ -169,7 +169,7 @@ void MqttSettingsService::configureMqtt() {
_mqttClient.connect(); _mqttClient.connect();
} }
emsesp::EMSESP::sensor_.reload(); emsesp::EMSESP::dallassensor_.reload();
} }
void MqttSettings::read(MqttSettings & settings, JsonObject & root) { void MqttSettings::read(MqttSettings & settings, JsonObject & root) {

View File

@@ -10,7 +10,7 @@
#include "../../src/system.h" #include "../../src/system.h"
#include "../../src/mqtt.h" #include "../../src/mqtt.h"
#include "../../src/sensor.h" #include "../../src/dallassensor.h"
#define MQTT_RECONNECTION_DELAY 1000 #define MQTT_RECONNECTION_DELAY 1000

View File

@@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "EMSESPAPIService.h" #include "WebAPIService.h"
#include "emsesp.h" #include "emsesp.h"
#include "command.h" #include "command.h"
#include "emsdevice.h" #include "emsdevice.h"
@@ -24,15 +24,15 @@
namespace emsesp { namespace emsesp {
EMSESPAPIService::EMSESPAPIService(AsyncWebServer * server) { WebAPIService::WebAPIService(AsyncWebServer * server) {
server->on(EMSESP_API_SERVICE_PATH, HTTP_GET, std::bind(&EMSESPAPIService::emsespAPIService, this, std::placeholders::_1)); server->on(EMSESP_API_SERVICE_PATH, HTTP_GET, std::bind(&WebAPIService::webAPIService, this, std::placeholders::_1));
} }
// http://ems-esp/api?device=boiler&cmd=wwtemp&data=20&id=1 // http://ems-esp/api?device=boiler&cmd=wwtemp&data=20&id=1
void EMSESPAPIService::emsespAPIService(AsyncWebServerRequest * request) { void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
// see if the API is enabled // see if the API is enabled
bool api_enabled; bool api_enabled;
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { api_enabled = settings.api_enabled; }); EMSESP::webSettingsService.read([&](WebSettings & settings) { api_enabled = settings.api_enabled; });
// must have device and cmd parameters // must have device and cmd parameters
if ((!request->hasParam(F_(device))) || (!request->hasParam(F_(cmd)))) { if ((!request->hasParam(F_(device))) || (!request->hasParam(F_(cmd)))) {

View File

@@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef EMSESPAPIService_h #ifndef WebAPIService_h
#define EMSESPAPIService_h #define WebAPIService_h
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
@@ -27,12 +27,12 @@
namespace emsesp { namespace emsesp {
class EMSESPAPIService { class WebAPIService {
public: public:
EMSESPAPIService(AsyncWebServer * server); WebAPIService(AsyncWebServer * server);
private: private:
void emsespAPIService(AsyncWebServerRequest * request); void webAPIService(AsyncWebServerRequest * request);
}; };
} // namespace emsesp } // namespace emsesp

View File

@@ -16,35 +16,35 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "EMSESPDevicesService.h" #include "WebDevicesService.h"
#include "emsesp.h" #include "emsesp.h"
namespace emsesp { namespace emsesp {
using namespace std::placeholders; // for `_1` etc using namespace std::placeholders; // for `_1` etc
EMSESPDevicesService::EMSESPDevicesService(AsyncWebServer * server, SecurityManager * securityManager) WebDevicesService::WebDevicesService(AsyncWebServer * server, SecurityManager * securityManager)
: _device_dataHandler(DEVICE_DATA_SERVICE_PATH, : _device_dataHandler(DEVICE_DATA_SERVICE_PATH,
securityManager->wrapCallback(std::bind(&EMSESPDevicesService::device_data, this, _1, _2), AuthenticationPredicates::IS_AUTHENTICATED)) { securityManager->wrapCallback(std::bind(&WebDevicesService::device_data, this, _1, _2), AuthenticationPredicates::IS_AUTHENTICATED)) {
server->on(EMSESP_DEVICES_SERVICE_PATH, server->on(EMSESP_DEVICES_SERVICE_PATH,
HTTP_GET, HTTP_GET,
securityManager->wrapRequest(std::bind(&EMSESPDevicesService::all_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); securityManager->wrapRequest(std::bind(&WebDevicesService::all_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED));
server->on(SCAN_DEVICES_SERVICE_PATH, server->on(SCAN_DEVICES_SERVICE_PATH,
HTTP_GET, HTTP_GET,
securityManager->wrapRequest(std::bind(&EMSESPDevicesService::scan_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); securityManager->wrapRequest(std::bind(&WebDevicesService::scan_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED));
_device_dataHandler.setMethod(HTTP_POST); _device_dataHandler.setMethod(HTTP_POST);
_device_dataHandler.setMaxContentLength(256); _device_dataHandler.setMaxContentLength(256);
server->addHandler(&_device_dataHandler); server->addHandler(&_device_dataHandler);
} }
void EMSESPDevicesService::scan_devices(AsyncWebServerRequest * request) { void WebDevicesService::scan_devices(AsyncWebServerRequest * request) {
EMSESP::scan_devices(); EMSESP::scan_devices();
request->send(200); request->send(200);
} }
void EMSESPDevicesService::all_devices(AsyncWebServerRequest * request) { void WebDevicesService::all_devices(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_DEVICE_SIZE); AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_DEVICE_SIZE);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
@@ -63,7 +63,7 @@ void EMSESPDevicesService::all_devices(AsyncWebServerRequest * request) {
} }
JsonArray sensors = root.createNestedArray("sensors"); JsonArray sensors = root.createNestedArray("sensors");
if (!EMSESP::sensor_devices().empty()) { if (EMSESP::have_sensors()) {
uint8_t i = 1; uint8_t i = 1;
char s[8]; char s[8];
for (const auto & sensor : EMSESP::sensor_devices()) { for (const auto & sensor : EMSESP::sensor_devices()) {
@@ -78,7 +78,7 @@ void EMSESPDevicesService::all_devices(AsyncWebServerRequest * request) {
request->send(response); request->send(response);
} }
void EMSESPDevicesService::device_data(AsyncWebServerRequest * request, JsonVariant & json) { void WebDevicesService::device_data(AsyncWebServerRequest * request, JsonVariant & json) {
if (json.is<JsonObject>()) { if (json.is<JsonObject>()) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_DEVICE_SIZE); AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_DEVICE_SIZE);
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE

View File

@@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef EMSESPDevicesService_h #ifndef WebDevicesService_h
#define EMSESPDevicesService_h #define WebDevicesService_h
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
@@ -32,9 +32,9 @@
namespace emsesp { namespace emsesp {
class EMSESPDevicesService { class WebDevicesService {
public: public:
EMSESPDevicesService(AsyncWebServer * server, SecurityManager * securityManager); WebDevicesService(AsyncWebServer * server, SecurityManager * securityManager);
private: private:
void all_devices(AsyncWebServerRequest * request); void all_devices(AsyncWebServerRequest * request);

View File

@@ -16,18 +16,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "EMSESPSettingsService.h" #include "WebSettingsService.h"
#include "emsesp.h" #include "emsesp.h"
namespace emsesp { namespace emsesp {
EMSESPSettingsService::EMSESPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) WebSettingsService::WebSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(EMSESPSettings::read, EMSESPSettings::update, this, server, EMSESP_SETTINGS_SERVICE_PATH, securityManager) : _httpEndpoint(WebSettings::read, WebSettings::update, this, server, EMSESP_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(EMSESPSettings::read, EMSESPSettings::update, this, fs, EMSESP_SETTINGS_FILE) { , _fsPersistence(WebSettings::read, WebSettings::update, this, fs, EMSESP_SETTINGS_FILE) {
addUpdateHandler([&](const String & originId) { onUpdate(); }, false); addUpdateHandler([&](const String & originId) { onUpdate(); }, false);
} }
void EMSESPSettings::read(EMSESPSettings & settings, JsonObject & root) { void WebSettings::read(WebSettings & settings, JsonObject & root) {
root["tx_mode"] = settings.tx_mode; root["tx_mode"] = settings.tx_mode;
root["ems_bus_id"] = settings.ems_bus_id; root["ems_bus_id"] = settings.ems_bus_id;
root["syslog_level"] = settings.syslog_level; root["syslog_level"] = settings.syslog_level;
@@ -47,7 +47,7 @@ void EMSESPSettings::read(EMSESPSettings & settings, JsonObject & root) {
root["analog_enabled"] = settings.analog_enabled; root["analog_enabled"] = settings.analog_enabled;
} }
StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & settings) { StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) {
settings.tx_mode = root["tx_mode"] | EMSESP_DEFAULT_TX_MODE; settings.tx_mode = root["tx_mode"] | EMSESP_DEFAULT_TX_MODE;
settings.ems_bus_id = root["ems_bus_id"] | EMSESP_DEFAULT_EMS_BUS_ID; settings.ems_bus_id = root["ems_bus_id"] | EMSESP_DEFAULT_EMS_BUS_ID;
settings.syslog_level = root["syslog_level"] | EMSESP_DEFAULT_SYSLOG_LEVEL; settings.syslog_level = root["syslog_level"] | EMSESP_DEFAULT_SYSLOG_LEVEL;
@@ -71,19 +71,17 @@ StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & set
// this is called after any of the settings have been persisted to the filesystem // this is called after any of the settings have been persisted to the filesystem
// either via the Web UI or via the Console // either via the Web UI or via the Console
void EMSESPSettingsService::onUpdate() { void WebSettingsService::onUpdate() {
EMSESP::shower_.start(); EMSESP::shower_.start();
Sensor sensor_; // Dallas sensors EMSESP::dallassensor_.start();
sensor_.start();
System::init(); System::init();
} }
void EMSESPSettingsService::begin() { void WebSettingsService::begin() {
_fsPersistence.readFromFS(); _fsPersistence.readFromFS();
} }
void EMSESPSettingsService::save() { void WebSettingsService::save() {
_fsPersistence.writeToFS(); _fsPersistence.writeToFS();
} }

View File

@@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef EMSESPSettingsConfig_h #ifndef WebSettingsConfig_h
#define EMSESPSettingsConfig_h #define WebSettingsConfig_h
#include <HttpEndpoint.h> #include <HttpEndpoint.h>
#include <FSPersistence.h> #include <FSPersistence.h>
@@ -60,7 +60,7 @@
namespace emsesp { namespace emsesp {
class EMSESPSettings { class WebSettings {
public: public:
uint8_t tx_mode; uint8_t tx_mode;
uint8_t ems_bus_id; uint8_t ems_bus_id;
@@ -80,20 +80,20 @@ class EMSESPSettings {
uint8_t bool_format; uint8_t bool_format;
bool analog_enabled; bool analog_enabled;
static void read(EMSESPSettings & settings, JsonObject & root); static void read(WebSettings & settings, JsonObject & root);
static StateUpdateResult update(JsonObject & root, EMSESPSettings & settings); static StateUpdateResult update(JsonObject & root, WebSettings & settings);
}; };
class EMSESPSettingsService : public StatefulService<EMSESPSettings> { class WebSettingsService : public StatefulService<WebSettings> {
public: public:
EMSESPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager); WebSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
void begin(); void begin();
void save(); void save();
private: private:
HttpEndpoint<EMSESPSettings> _httpEndpoint; HttpEndpoint<WebSettings> _httpEndpoint;
FSPersistence<EMSESPSettings> _fsPersistence; FSPersistence<WebSettings> _fsPersistence;
void onUpdate(); void onUpdate();
}; };

View File

@@ -16,16 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "EMSESPStatusService.h" #include "WebStatusService.h"
#include "emsesp.h" #include "emsesp.h"
namespace emsesp { namespace emsesp {
EMSESPStatusService::EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager) { WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
// rest endpoint for web page // rest endpoint for web page
server->on(EMSESP_STATUS_SERVICE_PATH, server->on(EMSESP_STATUS_SERVICE_PATH,
HTTP_GET, HTTP_GET,
securityManager->wrapRequest(std::bind(&EMSESPStatusService::emsespStatusService, this, std::placeholders::_1), securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED)); AuthenticationPredicates::IS_AUTHENTICATED));
// trigger on wifi connects/disconnects // trigger on wifi connects/disconnects
@@ -39,24 +39,24 @@ EMSESPStatusService::EMSESPStatusService(AsyncWebServer * server, SecurityManage
} }
#ifdef ESP32 #ifdef ESP32
void EMSESPStatusService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { void WebStatusService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
EMSESP::logger().debug(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason); EMSESP::logger().debug(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason);
} }
void EMSESPStatusService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { void WebStatusService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
EMSESP::logger().debug(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname()); EMSESP::logger().debug(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
EMSESP::system_.send_heartbeat(); // send out heartbeat MQTT as soon as we have a connection EMSESP::system_.send_heartbeat(); // send out heartbeat MQTT as soon as we have a connection
} }
#elif defined(ESP8266) #elif defined(ESP8266)
void EMSESPStatusService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) { void WebStatusService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
EMSESP::logger().debug(F("WiFi Disconnected. Reason code=%d"), event.reason); EMSESP::logger().debug(F("WiFi Disconnected. Reason code=%d"), event.reason);
} }
void EMSESPStatusService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) { void WebStatusService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
EMSESP::logger().debug(F("WiFi Connected with IP=%s, hostname=%s"), event.ip.toString().c_str(), WiFi.hostname().c_str()); EMSESP::logger().debug(F("WiFi Connected with IP=%s, hostname=%s"), event.ip.toString().c_str(), WiFi.hostname().c_str());
EMSESP::system_.send_heartbeat(); // send out heartbeat MQTT as soon as we have a connection EMSESP::system_.send_heartbeat(); // send out heartbeat MQTT as soon as we have a connection
} }
#endif #endif
void EMSESPStatusService::emsespStatusService(AsyncWebServerRequest * request) { void WebStatusService::webStatusService(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE); AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();

View File

@@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef EMSESPStatusService_h #ifndef WebStatusService_h
#define EMSESPStatusService_h #define WebStatusService_h
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
@@ -30,12 +30,12 @@
namespace emsesp { namespace emsesp {
class EMSESPStatusService { class WebStatusService {
public: public:
EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager); WebStatusService(AsyncWebServer * server, SecurityManager * securityManager);
private: private:
void emsespStatusService(AsyncWebServerRequest * request); void webStatusService(AsyncWebServerRequest * request);
#ifdef ESP32 #ifdef ESP32
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);

View File

@@ -177,8 +177,8 @@ void EMSESPShell::add_console_commands() {
[](Shell & shell, const std::vector<std::string> & arguments) { [](Shell & shell, const std::vector<std::string> & arguments) {
uint8_t device_id = Helpers::hextoint(arguments.front().c_str()); uint8_t device_id = Helpers::hextoint(arguments.front().c_str());
if ((device_id == 0x0B) || (device_id == 0x0D) || (device_id == 0x0A) || (device_id == 0x0F) || (device_id == 0x12)) { if ((device_id == 0x0B) || (device_id == 0x0D) || (device_id == 0x0A) || (device_id == 0x0F) || (device_id == 0x12)) {
EMSESP::emsespSettingsService.update( EMSESP::webSettingsService.update(
[&](EMSESPSettings & settings) { [&](WebSettings & settings) {
settings.ems_bus_id = device_id; settings.ems_bus_id = device_id;
shell.printfln(F_(bus_id_fmt), settings.ems_bus_id); shell.printfln(F_(bus_id_fmt), settings.ems_bus_id);
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
@@ -206,8 +206,8 @@ void EMSESPShell::add_console_commands() {
[](Shell & shell, const std::vector<std::string> & arguments) { [](Shell & shell, const std::vector<std::string> & arguments) {
uint8_t tx_mode = std::strtol(arguments[0].c_str(), nullptr, 10); uint8_t tx_mode = std::strtol(arguments[0].c_str(), nullptr, 10);
// save the tx_mode // save the tx_mode
EMSESP::emsespSettingsService.update( EMSESP::webSettingsService.update(
[&](EMSESPSettings & settings) { [&](WebSettings & settings) {
settings.tx_mode = tx_mode; settings.tx_mode = tx_mode;
shell.printfln(F_(tx_mode_fmt), settings.tx_mode); shell.printfln(F_(tx_mode_fmt), settings.tx_mode);
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
@@ -258,7 +258,7 @@ void EMSESPShell::add_console_commands() {
CommandFlags::USER, CommandFlags::USER,
flash_string_vector{F_(set)}, flash_string_vector{F_(set)},
[](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) { [](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) {
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
shell.printfln(F_(tx_mode_fmt), settings.tx_mode); shell.printfln(F_(tx_mode_fmt), settings.tx_mode);
shell.printfln(F_(bus_id_fmt), settings.ems_bus_id); shell.printfln(F_(bus_id_fmt), settings.ems_bus_id);
char buffer[4]; char buffer[4];
@@ -290,8 +290,8 @@ void EMSESPShell::add_console_commands() {
flash_string_vector{F_(deviceid_mandatory)}, flash_string_vector{F_(deviceid_mandatory)},
[](Shell & shell, const std::vector<std::string> & arguments) { [](Shell & shell, const std::vector<std::string> & arguments) {
uint8_t value = Helpers::hextoint(arguments.front().c_str()); uint8_t value = Helpers::hextoint(arguments.front().c_str());
EMSESP::emsespSettingsService.update( EMSESP::webSettingsService.update(
[&](EMSESPSettings & settings) { [&](WebSettings & settings) {
settings.master_thermostat = value; settings.master_thermostat = value;
EMSESP::actual_master_thermostat(value); // set the internal value too EMSESP::actual_master_thermostat(value); // set the internal value too
char buffer[5]; char buffer[5];

View File

@@ -18,7 +18,7 @@
// code originally written by nomis - https://github.com/nomis // code originally written by nomis - https://github.com/nomis
#include "sensor.h" #include "dallassensor.h"
#include "emsesp.h" #include "emsesp.h"
#ifdef ESP32 #ifdef ESP32
@@ -29,10 +29,10 @@
namespace emsesp { namespace emsesp {
uuid::log::Logger Sensor::logger_{F_(sensor), uuid::log::Facility::DAEMON}; uuid::log::Logger DallasSensor::logger_{F_(sensor), uuid::log::Facility::DAEMON};
// start the 1-wire // start the 1-wire
void Sensor::start() { void DallasSensor::start() {
reload(); reload();
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
@@ -48,8 +48,8 @@ void Sensor::start() {
} }
// load the MQTT settings // load the MQTT settings
void Sensor::reload() { void DallasSensor::reload() {
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
dallas_gpio_ = settings.dallas_gpio; dallas_gpio_ = settings.dallas_gpio;
parasite_ = settings.dallas_parasite; parasite_ = settings.dallas_parasite;
}); });
@@ -60,7 +60,7 @@ void Sensor::reload() {
} }
} }
void Sensor::loop() { void DallasSensor::loop() {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
uint32_t time_now = uuid::get_uptime(); uint32_t time_now = uuid::get_uptime();
@@ -109,31 +109,31 @@ void Sensor::loop() {
if ((t >= -550) && (t <= 1250)) { if ((t >= -550) && (t <= 1250)) {
// check if we have this sensor already // check if we have this sensor already
bool found = false; bool found = false;
for (auto & device : devices_) { for (auto & sensor : sensors_) {
if (device.id() == get_id(addr)) { if (sensor.id() == get_id(addr)) {
changed_ |= (t != device.temperature_c); changed_ |= (t != sensor.temperature_c);
device.temperature_c = t; sensor.temperature_c = t;
device.read = true; sensor.read = true;
found = true; found = true;
break; break;
} }
} }
// add new sensor // add new sensor
if (!found && (devices_.size() < (MAX_SENSORS - 1))) { if (!found && (sensors_.size() < (MAX_SENSORS - 1))) {
devices_.emplace_back(addr); sensors_.emplace_back(addr);
devices_.back().temperature_c = t; sensors_.back().temperature_c = t;
devices_.back().read = true; sensors_.back().read = true;
changed_ = true; changed_ = true;
} }
} }
break; break;
default: default:
LOG_ERROR(F("Unknown sensor %s"), Device(addr).to_string().c_str()); LOG_ERROR(F("Unknown sensor %s"), Sensor(addr).to_string().c_str());
break; break;
} }
} else { } else {
LOG_ERROR(F("Invalid sensor %s"), Device(addr).to_string().c_str()); LOG_ERROR(F("Invalid sensor %s"), Sensor(addr).to_string().c_str());
} }
} else { } else {
if (!parasite_) { if (!parasite_) {
@@ -141,21 +141,21 @@ void Sensor::loop() {
} }
// check for missing sensors after some samples // check for missing sensors after some samples
if (++scancnt_ > 5) { if (++scancnt_ > 5) {
for (auto & device : devices_) { for (auto & sensor : sensors_) {
if (!device.read) { if (!sensor.read) {
device.temperature_c = EMS_VALUE_SHORT_NOTSET; sensor.temperature_c = EMS_VALUE_SHORT_NOTSET;
changed_ = true; changed_ = true;
} }
device.read = false; sensor.read = false;
} }
scancnt_ = 0; scancnt_ = 0;
} else if (scancnt_ == -2) { // startup } else if (scancnt_ == -2) { // startup
firstscan_ = devices_.size(); firstscan_ = sensors_.size();
} else if ((scancnt_ <= 0) && (firstscan_ != devices_.size())) { // check 2 times for no change of sensorno. } else if ((scancnt_ <= 0) && (firstscan_ != sensors_.size())) { // check 2 times for no change of sensor #
scancnt_ = -3; scancnt_ = -3;
devices_.clear(); // restart scaning and clear to get correct numbering sensors_.clear(); // restart scaning and clear to get correct numbering
} }
// LOG_DEBUG(F("Found %zu sensor(s). Adding them."), devices_.size()); // uncomment for debug // LOG_DEBUG(F("Found %zu sensor(s). Adding them."), sensors_.size()); // uncomment for debug
state_ = State::IDLE; state_ = State::IDLE;
} }
} }
@@ -163,7 +163,7 @@ void Sensor::loop() {
#endif #endif
} }
bool Sensor::temperature_convert_complete() { bool DallasSensor::temperature_convert_complete() {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
if (parasite_) { if (parasite_) {
return true; // don't care, use the minimum time in loop return true; // don't care, use the minimum time in loop
@@ -177,10 +177,10 @@ bool Sensor::temperature_convert_complete() {
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-parameter"
int16_t Sensor::get_temperature_c(const uint8_t addr[]) { int16_t DallasSensor::get_temperature_c(const uint8_t addr[]) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
if (!bus_.reset()) { if (!bus_.reset()) {
LOG_ERROR(F("Bus reset failed before reading scratchpad from %s"), Device(addr).to_string().c_str()); LOG_ERROR(F("Bus reset failed before reading scratchpad from %s"), Sensor(addr).to_string().c_str());
return EMS_VALUE_SHORT_NOTSET; return EMS_VALUE_SHORT_NOTSET;
} }
YIELD; YIELD;
@@ -192,13 +192,13 @@ int16_t Sensor::get_temperature_c(const uint8_t addr[]) {
YIELD; YIELD;
if (!bus_.reset()) { if (!bus_.reset()) {
LOG_ERROR(F("Bus reset failed after reading scratchpad from %s"), Device(addr).to_string().c_str()); LOG_ERROR(F("Bus reset failed after reading scratchpad from %s"), Sensor(addr).to_string().c_str());
return EMS_VALUE_SHORT_NOTSET; return EMS_VALUE_SHORT_NOTSET;
} }
YIELD; YIELD;
if (bus_.crc8(scratchpad, SCRATCHPAD_LEN - 1) != scratchpad[SCRATCHPAD_LEN - 1]) { if (bus_.crc8(scratchpad, SCRATCHPAD_LEN - 1) != scratchpad[SCRATCHPAD_LEN - 1]) {
LOG_WARNING(F("Invalid scratchpad CRC: %02X%02X%02X%02X%02X%02X%02X%02X%02X from device %s"), LOG_WARNING(F("Invalid scratchpad CRC: %02X%02X%02X%02X%02X%02X%02X%02X%02X from sensor %s"),
scratchpad[0], scratchpad[0],
scratchpad[1], scratchpad[1],
scratchpad[2], scratchpad[2],
@@ -208,7 +208,7 @@ int16_t Sensor::get_temperature_c(const uint8_t addr[]) {
scratchpad[6], scratchpad[6],
scratchpad[7], scratchpad[7],
scratchpad[8], scratchpad[8],
Device(addr).to_string().c_str()); Sensor(addr).to_string().c_str());
return EMS_VALUE_SHORT_NOTSET; return EMS_VALUE_SHORT_NOTSET;
} }
@@ -217,7 +217,7 @@ int16_t Sensor::get_temperature_c(const uint8_t addr[]) {
if (addr[0] == TYPE_DS18S20) { if (addr[0] == TYPE_DS18S20) {
raw_value = (raw_value << 3) + 12 - scratchpad[SCRATCHPAD_CNT_REM]; raw_value = (raw_value << 3) + 12 - scratchpad[SCRATCHPAD_CNT_REM];
} else { } else {
// Adjust based on device resolution // Adjust based on sensor resolution
int resolution = 9 + ((scratchpad[SCRATCHPAD_CONFIG] >> 5) & 0x3); int resolution = 9 + ((scratchpad[SCRATCHPAD_CONFIG] >> 5) & 0x3);
switch (resolution) { switch (resolution) {
case 9: case 9:
@@ -242,26 +242,26 @@ int16_t Sensor::get_temperature_c(const uint8_t addr[]) {
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
const std::vector<Sensor::Device> Sensor::devices() const { const std::vector<DallasSensor::Sensor> DallasSensor::sensors() const {
return devices_; return sensors_;
} }
// skip crc from id. // skip crc from id.
Sensor::Device::Device(const uint8_t addr[]) DallasSensor::Sensor::Sensor(const uint8_t addr[])
: id_(((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16) : id_(((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16)
| ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6])) { | ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6])) {
} }
uint64_t Sensor::get_id(const uint8_t addr[]) { uint64_t DallasSensor::get_id(const uint8_t addr[]) {
return (((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16) return (((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16)
| ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6])); | ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6]));
} }
uint64_t Sensor::Device::id() const { uint64_t DallasSensor::Sensor::id() const {
return id_; return id_;
} }
std::string Sensor::Device::to_string() const { std::string DallasSensor::Sensor::to_string() const {
std::string str(20, '\0'); std::string str(20, '\0');
snprintf_P(&str[0], snprintf_P(&str[0],
str.capacity() + 1, str.capacity() + 1,
@@ -274,7 +274,7 @@ std::string Sensor::Device::to_string() const {
} }
// check to see if values have been updated // check to see if values have been updated
bool Sensor::updated_values() { bool DallasSensor::updated_values() {
if (changed_) { if (changed_) {
changed_ = false; changed_ = false;
return true; return true;
@@ -282,25 +282,26 @@ bool Sensor::updated_values() {
return false; return false;
} }
bool Sensor::command_info(const char * value, const int8_t id, JsonObject & output) { bool DallasSensor::command_info(const char * value, const int8_t id, JsonObject & output) {
return (export_values(output)); return (export_values(output));
} }
// creates JSON doc from values // creates JSON doc from values
// returns false if empty // returns false if empty
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}} // e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":23.30},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":24.0}}
bool Sensor::export_values(JsonObject & output) { bool DallasSensor::export_values(JsonObject & output) {
if (devices_.size() == 0) { if (sensors_.size() == 0) {
return false; return false;
} }
uint8_t i = 1; // sensor count uint8_t i = 1; // sensor count
for (const auto & device : devices_) { for (const auto & sensor : sensors_) {
char sensorID[10]; // sensor{1-n} char sensorID[10]; // sensor{1-n}
snprintf_P(sensorID, 10, PSTR("sensor%d"), i++); snprintf_P(sensorID, 10, PSTR("sensor%d"), i++);
JsonObject dataSensor = output.createNestedObject(sensorID); JsonObject dataSensor = output.createNestedObject(sensorID);
dataSensor["id"] = device.to_string(); dataSensor["id"] = sensor.to_string();
if (Helpers::hasValue(device.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
dataSensor["temp"] = (float)(device.temperature_c) / 10; dataSensor["temp"] = (float)(sensor.temperature_c) / 10;
} }
} }
@@ -308,41 +309,37 @@ bool Sensor::export_values(JsonObject & output) {
} }
// send all dallas sensor values as a JSON package to MQTT // send all dallas sensor values as a JSON package to MQTT
// assumes there are devices void DallasSensor::publish_values() {
void Sensor::publish_values() { uint8_t num_sensors = sensors_.size();
uint8_t num_devices = devices_.size();
if (num_devices == 0) { if (num_sensors == 0) {
return; return;
} }
DynamicJsonDocument doc(100 * num_devices); DynamicJsonDocument doc(100 * num_sensors);
uint8_t sensor_no = 1; // sensor count uint8_t sensor_no = 1;
uint8_t mqtt_format_ = Mqtt::mqtt_format(); uint8_t mqtt_format_ = Mqtt::mqtt_format();
for (const auto & device : devices_) {
for (const auto & sensor : sensors_) {
char sensorID[10]; // sensor{1-n} char sensorID[10]; // sensor{1-n}
snprintf_P(sensorID, 10, PSTR("sensor%d"), sensor_no); snprintf_P(sensorID, 10, PSTR("sensor%d"), sensor_no);
if (mqtt_format_ == Mqtt::Format::SINGLE) { if (mqtt_format_ == Mqtt::Format::SINGLE) {
// e.g. sensor_data = {"sensor1":23.3,"sensor2":24.0} // e.g. sensor_data = {"sensor1":23.3,"sensor2":24.0}
if (Helpers::hasValue(device.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
doc[sensorID] = (float)(device.temperature_c) / 10; doc[sensorID] = (float)(sensor.temperature_c) / 10;
} }
} else if (mqtt_format_ == Mqtt::Format::NESTED) { } else {
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03","temp":"23.3"},"sensor2":{"id":"28-233D-9497-0C03","temp":"24.0"}} // e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03","temp":23.3},"sensor2":{"id":"28-233D-9497-0C03","temp":24.0}}
JsonObject dataSensor = doc.createNestedObject(sensorID); JsonObject dataSensor = doc.createNestedObject(sensorID);
dataSensor["id"] = device.to_string(); dataSensor["id"] = sensor.to_string();
if (Helpers::hasValue(device.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
dataSensor["temp"] = (float)(device.temperature_c) / 10; dataSensor["temp"] = (float)(sensor.temperature_c) / 10;
} }
} else if (mqtt_format_ == Mqtt::Format::HA) { }
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03","temp":"23.3"},"sensor2":{"id":"28-233D-9497-0C03","temp":"24.0"}}
JsonObject dataSensor = doc.createNestedObject(sensorID); // create the HA MQTT config
dataSensor["id"] = device.to_string(); // to e.g. homeassistant/sensor/ems-esp/dallas_28-233D-9497-0C03/config
if (Helpers::hasValue(device.temperature_c)) { if (mqtt_format_ == Mqtt::Format::HA) {
dataSensor["temp"] = (float)(device.temperature_c) / 10;
}
// create the config if this hasn't already been done
// to e.g. homeassistant/sensor/ems-esp/dallas_28-233D-9497-0C03/config
if (!(registered_ha_[sensor_no - 1])) { if (!(registered_ha_[sensor_no - 1])) {
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> config; StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> config;
config["dev_cla"] = F("temperature"); config["dev_cla"] = F("temperature");
@@ -361,7 +358,7 @@ void Sensor::publish_values() {
snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %d"), sensor_no); snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %d"), sensor_no);
config["name"] = str; config["name"] = str;
snprintf_P(str, sizeof(str), PSTR("dallas_%s"), device.to_string().c_str()); snprintf_P(str, sizeof(str), PSTR("dallas_%s"), sensor.to_string().c_str());
config["uniq_id"] = str; config["uniq_id"] = str;
JsonObject dev = config.createNestedObject("dev"); JsonObject dev = config.createNestedObject("dev");
@@ -369,7 +366,7 @@ void Sensor::publish_values() {
ids.add("ems-esp"); ids.add("ems-esp");
std::string topic(100, '\0'); std::string topic(100, '\0');
snprintf_P(&topic[0], 100, PSTR("homeassistant/sensor/ems-esp/dallas_%s/config"), device.to_string().c_str()); snprintf_P(&topic[0], 100, PSTR("homeassistant/sensor/ems-esp/dallas_%s/config"), sensor.to_string().c_str());
Mqtt::publish_retain(topic, config.as<JsonObject>(), true); // publish the config payload with retain flag Mqtt::publish_retain(topic, config.as<JsonObject>(), true); // publish the config payload with retain flag
registered_ha_[sensor_no - 1] = true; registered_ha_[sensor_no - 1] = true;
@@ -378,6 +375,7 @@ void Sensor::publish_values() {
sensor_no++; // increment sensor count sensor_no++; // increment sensor count
} }
doc.shrinkToFit();
Mqtt::publish(F("sensor_data"), doc.as<JsonObject>()); Mqtt::publish(F("sensor_data"), doc.as<JsonObject>());
} }

View File

@@ -18,8 +18,8 @@
// code originally written by nomis - https://github.com/nomis // code originally written by nomis - https://github.com/nomis
#ifndef EMSESP_SENSOR_H #ifndef EMSESP_DALLASSENSOR_H
#define EMSESP_SENSOR_H #define EMSESP_DALLASSENSOR_H
#include <string> #include <string>
#include <vector> #include <vector>
@@ -36,12 +36,12 @@
namespace emsesp { namespace emsesp {
class Sensor { class DallasSensor {
public: public:
class Device { class Sensor {
public: public:
Device(const uint8_t addr[]); Sensor(const uint8_t addr[]);
~Device() = default; ~Sensor() = default;
uint64_t id() const; uint64_t id() const;
std::string to_string() const; std::string to_string() const;
@@ -53,8 +53,8 @@ class Sensor {
const uint64_t id_; const uint64_t id_;
}; };
Sensor() = default; DallasSensor() = default;
~Sensor() = default; ~DallasSensor() = default;
void start(); void start();
void loop(); void loop();
@@ -62,10 +62,7 @@ class Sensor {
void reload(); void reload();
bool updated_values(); bool updated_values();
bool command_info(const char * value, const int8_t id, JsonObject & output); const std::vector<Sensor> sensors() const;
bool export_values(JsonObject & doc);
const std::vector<Device> devices() const;
private: private:
static constexpr uint8_t MAX_SENSORS = 20; static constexpr uint8_t MAX_SENSORS = 20;
@@ -104,10 +101,13 @@ class Sensor {
int16_t get_temperature_c(const uint8_t addr[]); int16_t get_temperature_c(const uint8_t addr[]);
uint64_t get_id(const uint8_t addr[]); uint64_t get_id(const uint8_t addr[]);
bool command_info(const char * value, const int8_t id, JsonObject & output);
bool export_values(JsonObject & doc);
uint32_t last_activity_ = uuid::get_uptime(); uint32_t last_activity_ = uuid::get_uptime();
uint32_t last_publish_ = uuid::get_uptime(); uint32_t last_publish_ = uuid::get_uptime();
State state_ = State::IDLE; State state_ = State::IDLE;
std::vector<Device> devices_; std::vector<Sensor> sensors_;
bool registered_ha_[MAX_SENSORS]; bool registered_ha_[MAX_SENSORS];

View File

@@ -28,7 +28,7 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i
: EMSdevice(device_type, device_id, product_id, version, name, flags, brand) { : EMSdevice(device_type, device_id, product_id, version, name, flags, brand) {
uint8_t actual_master_thermostat = EMSESP::actual_master_thermostat(); // what we're actually using uint8_t actual_master_thermostat = EMSESP::actual_master_thermostat(); // what we're actually using
uint8_t master_thermostat = EMSESP_DEFAULT_MASTER_THERMOSTAT; uint8_t master_thermostat = EMSESP_DEFAULT_MASTER_THERMOSTAT;
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
master_thermostat = settings.master_thermostat; // what the user has defined master_thermostat = settings.master_thermostat; // what the user has defined
}); });

View File

@@ -23,20 +23,20 @@ namespace emsesp {
AsyncWebServer webServer(80); AsyncWebServer webServer(80);
#if defined(ESP32) #if defined(ESP32)
ESP8266React EMSESP::esp8266React(&webServer, &SPIFFS); ESP8266React EMSESP::esp8266React(&webServer, &SPIFFS);
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &SPIFFS, EMSESP::esp8266React.getSecurityManager()); WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &SPIFFS, EMSESP::esp8266React.getSecurityManager());
#elif defined(ESP8266) #elif defined(ESP8266)
ESP8266React EMSESP::esp8266React(&webServer, &LittleFS); ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager()); WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
#elif defined(EMSESP_STANDALONE) #elif defined(EMSESP_STANDALONE)
FS dummyFS; FS dummyFS;
ESP8266React EMSESP::esp8266React(&webServer, &dummyFS); ESP8266React EMSESP::esp8266React(&webServer, &dummyFS);
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager()); WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager());
#endif #endif
EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager()); WebStatusService EMSESP::webStatusService = WebStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager()); WebDevicesService EMSESP::webDevicesService = WebDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
EMSESPAPIService EMSESP::emsespAPIService = EMSESPAPIService(&webServer); WebAPIService EMSESP::webAPIService = WebAPIService(&webServer);
using DeviceFlags = emsesp::EMSdevice; using DeviceFlags = emsesp::EMSdevice;
using DeviceType = emsesp::EMSdevice::DeviceType; using DeviceType = emsesp::EMSdevice::DeviceType;
@@ -46,13 +46,13 @@ std::vector<emsesp::EMSESP::Device_record> EMSESP::device_library_; // libary of
uuid::log::Logger EMSESP::logger_{F_(emsesp), uuid::log::Facility::KERN}; uuid::log::Logger EMSESP::logger_{F_(emsesp), uuid::log::Facility::KERN};
// The services // The services
RxService EMSESP::rxservice_; // incoming Telegram Rx handler RxService EMSESP::rxservice_; // incoming Telegram Rx handler
TxService EMSESP::txservice_; // outgoing Telegram Tx handler TxService EMSESP::txservice_; // outgoing Telegram Tx handler
Mqtt EMSESP::mqtt_; // mqtt handler Mqtt EMSESP::mqtt_; // mqtt handler
System EMSESP::system_; // core system services System EMSESP::system_; // core system services
Console EMSESP::console_; // telnet and serial console Console EMSESP::console_; // telnet and serial console
Sensor EMSESP::sensor_; // Dallas sensors DallasSensor EMSESP::dallassensor_; // Dallas sensors
Shower EMSESP::shower_; // Shower logic Shower EMSESP::shower_; // Shower logic
// static/common variables // static/common variables
uint8_t EMSESP::actual_master_thermostat_ = EMSESP_DEFAULT_MASTER_THERMOSTAT; // which thermostat leads when multiple found uint8_t EMSESP::actual_master_thermostat_ = EMSESP_DEFAULT_MASTER_THERMOSTAT; // which thermostat leads when multiple found
@@ -140,7 +140,7 @@ void EMSESP::watch_id(uint16_t watch_id) {
// this is called when the tx_mode is persisted in the FS either via Web UI or the console // this is called when the tx_mode is persisted in the FS either via Web UI or the console
void EMSESP::init_tx() { void EMSESP::init_tx() {
uint8_t tx_mode; uint8_t tx_mode;
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
tx_mode = settings.tx_mode; tx_mode = settings.tx_mode;
#ifndef EMSESP_FORCE_SERIAL #ifndef EMSESP_FORCE_SERIAL
@@ -204,7 +204,7 @@ void EMSESP::show_ems(uuid::console::Shell & shell) {
if (bus_status() != BUS_STATUS_OFFLINE) { if (bus_status() != BUS_STATUS_OFFLINE) {
shell.printfln(F("EMS Bus info:")); shell.printfln(F("EMS Bus info:"));
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { shell.printfln(F(" Tx mode: %d"), settings.tx_mode); }); EMSESP::webSettingsService.read([&](WebSettings & settings) { shell.printfln(F(" Tx mode: %d"), settings.tx_mode); });
shell.printfln(F(" Bus protocol: %s"), EMSbus::is_ht3() ? F("HT3") : F("Buderus")); shell.printfln(F(" Bus protocol: %s"), EMSbus::is_ht3() ? F("HT3") : F("Buderus"));
shell.printfln(F(" #telegrams received: %d"), rxservice_.telegram_count()); shell.printfln(F(" #telegrams received: %d"), rxservice_.telegram_count());
shell.printfln(F(" #read requests sent: %d"), txservice_.telegram_read_count()); shell.printfln(F(" #read requests sent: %d"), txservice_.telegram_read_count());
@@ -335,8 +335,8 @@ void EMSESP::publish_other_values() {
} }
void EMSESP::publish_sensor_values(const bool force) { void EMSESP::publish_sensor_values(const bool force) {
if (sensor_.updated_values() || force) { if (dallassensor_.updated_values() || force) {
sensor_.publish_values(); dallassensor_.publish_values();
} }
} }
@@ -862,8 +862,8 @@ void EMSESP::start() {
LittleFS.begin(); LittleFS.begin();
#endif #endif
esp8266React.begin(); // loads system settings (wifi, mqtt, etc) esp8266React.begin(); // loads system settings (wifi, mqtt, etc)
emsespSettingsService.begin(); // load EMS-ESP specific settings webSettingsService.begin(); // load EMS-ESP specific settings
} }
// Load our library of known devices. Names are stored in Flash mem. // Load our library of known devices. Names are stored in Flash mem.
@@ -872,12 +872,12 @@ void EMSESP::start() {
#include "device_library.h" #include "device_library.h"
}; };
console_.start(); // telnet and serial console console_.start(); // telnet and serial console
mqtt_.start(); // mqtt init mqtt_.start(); // mqtt init
system_.start(); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings. system_.start(); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings.
shower_.start(); // initialize shower timer and shower alert shower_.start(); // initialize shower timer and shower alert
sensor_.start(); // dallas external sensors dallassensor_.start(); // dallas external sensors
webServer.begin(); // start web server webServer.begin(); // start web server
emsdevices.reserve(5); // reserve space for initially 5 devices to avoid mem emsdevices.reserve(5); // reserve space for initially 5 devices to avoid mem
@@ -897,12 +897,12 @@ void EMSESP::loop() {
return; return;
} }
system_.loop(); // does LED and checks system health, and syslog service system_.loop(); // does LED and checks system health, and syslog service
shower_.loop(); // check for shower on/off shower_.loop(); // check for shower on/off
sensor_.loop(); // this will also send out via MQTT dallassensor_.loop(); // this will also send out via MQTT
mqtt_.loop(); // sends out anything in the queue via MQTT mqtt_.loop(); // sends out anything in the queue via MQTT
console_.loop(); // telnet/serial console console_.loop(); // telnet/serial console
rxservice_.loop(); // process any incoming Rx telegrams rxservice_.loop(); // process any incoming Rx telegrams
// force a query on the EMS devices to fetch latest data at a set interval (1 min) // force a query on the EMS devices to fetch latest data at a set interval (1 min)
if ((uuid::get_uptime() - last_fetch_ > EMS_FETCH_FREQUENCY)) { if ((uuid::get_uptime() - last_fetch_ > EMS_FETCH_FREQUENCY)) {

View File

@@ -34,17 +34,17 @@
#endif #endif
#include <ESP8266React.h> #include <ESP8266React.h>
#include "EMSESPStatusService.h" #include "WebStatusService.h"
#include "EMSESPDevicesService.h" #include "WebDevicesService.h"
#include "EMSESPSettingsService.h" #include "WebSettingsService.h"
#include "EMSESPAPIService.h" #include "WebAPIService.h"
#include "emsdevice.h" #include "emsdevice.h"
#include "emsfactory.h" #include "emsfactory.h"
#include "telegram.h" #include "telegram.h"
#include "mqtt.h" #include "mqtt.h"
#include "system.h" #include "system.h"
#include "sensor.h" #include "dallassensor.h"
#include "console.h" #include "console.h"
#include "shower.h" #include "shower.h"
#include "roomcontrol.h" #include "roomcontrol.h"
@@ -108,12 +108,12 @@ class EMSESP {
static void incoming_telegram(uint8_t * data, const uint8_t length); static void incoming_telegram(uint8_t * data, const uint8_t length);
static const std::vector<Sensor::Device> sensor_devices() { static const std::vector<DallasSensor::Sensor> sensor_devices() {
return sensor_.devices(); return dallassensor_.sensors();
} }
static bool have_sensors() { static bool have_sensors() {
return (!(sensor_.devices().empty())); return (!(dallassensor_.sensors().empty()));
} }
enum Watch : uint8_t { WATCH_OFF, WATCH_ON, WATCH_RAW }; enum Watch : uint8_t { WATCH_OFF, WATCH_ON, WATCH_RAW };
@@ -155,20 +155,20 @@ class EMSESP {
static std::vector<std::unique_ptr<EMSdevice>> emsdevices; static std::vector<std::unique_ptr<EMSdevice>> emsdevices;
// services // services
static Mqtt mqtt_; static Mqtt mqtt_;
static System system_; static System system_;
static Sensor sensor_; static DallasSensor dallassensor_;
static Console console_; static Console console_;
static Shower shower_; static Shower shower_;
static RxService rxservice_; static RxService rxservice_;
static TxService txservice_; static TxService txservice_;
// web controllers // web controllers
static ESP8266React esp8266React; static ESP8266React esp8266React;
static EMSESPSettingsService emsespSettingsService; static WebSettingsService webSettingsService;
static EMSESPStatusService emsespStatusService; static WebStatusService webStatusService;
static EMSESPDevicesService emsespDevicesService; static WebDevicesService webDevicesService;
static EMSESPAPIService emsespAPIService; static WebAPIService webAPIService;
static uuid::log::Logger logger() { static uuid::log::Logger logger() {
return logger_; return logger_;
@@ -203,8 +203,7 @@ class EMSESP {
static uint16_t read_id_; static uint16_t read_id_;
static uint16_t publish_id_; static uint16_t publish_id_;
static bool tap_water_active_; static bool tap_water_active_;
static uint8_t unique_id_count_;
static uint8_t unique_id_count_;
}; };
} // namespace emsesp } // namespace emsesp

View File

@@ -706,7 +706,7 @@ void Mqtt::register_mqtt_ha_binary_sensor(const __FlashStringHelper * name, cons
snprintf_P(state_t, sizeof(state_t), PSTR("%s/%s"), hostname_.c_str(), entity); snprintf_P(state_t, sizeof(state_t), PSTR("%s/%s"), hostname_.c_str(), entity);
doc["stat_t"] = state_t; doc["stat_t"] = state_t;
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
if (settings.bool_format == BOOL_FORMAT_ONOFF) { if (settings.bool_format == BOOL_FORMAT_ONOFF) {
doc[F("payload_on")] = F("on"); doc[F("payload_on")] = F("on");
doc[F("payload_off")] = F("off"); doc[F("payload_off")] = F("off");

View File

@@ -23,7 +23,7 @@ namespace emsesp {
uuid::log::Logger Shower::logger_{F_(shower), uuid::log::Facility::CONSOLE}; uuid::log::Logger Shower::logger_{F_(shower), uuid::log::Facility::CONSOLE};
void Shower::start() { void Shower::start() {
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
shower_timer_ = settings.shower_timer; shower_timer_ = settings.shower_timer;
shower_alert_ = settings.shower_alert; shower_alert_ = settings.shower_alert;
}); });

View File

@@ -76,7 +76,7 @@ void System::wifi_reconnect() {
LOG_NOTICE("The wifi will reconnect..."); LOG_NOTICE("The wifi will reconnect...");
Shell::loop_all(); Shell::loop_all();
delay(1000); // wait a second delay(1000); // wait a second
EMSESP::emsespSettingsService.save(); // local settings EMSESP::webSettingsService.save(); // local settings
EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password
} }
@@ -111,7 +111,7 @@ uint8_t System::free_mem() {
void System::syslog_init() { void System::syslog_init() {
// fetch settings // fetch settings
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
syslog_level_ = settings.syslog_level; syslog_level_ = settings.syslog_level;
syslog_mark_interval_ = settings.syslog_mark_interval; syslog_mark_interval_ = settings.syslog_mark_interval;
syslog_host_ = settings.syslog_host; syslog_host_ = settings.syslog_host;
@@ -148,7 +148,7 @@ void System::start() {
[&](WiFiSettings & wifiSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), wifiSettings.hostname.c_str(), EMSESP_APP_VERSION); }); [&](WiFiSettings & wifiSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), wifiSettings.hostname.c_str(), EMSESP_APP_VERSION); });
// these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""} // these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""}
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
Command::add(EMSdevice::DeviceType::SYSTEM, settings.ems_bus_id, F("pin"), System::command_pin); Command::add(EMSdevice::DeviceType::SYSTEM, settings.ems_bus_id, F("pin"), System::command_pin);
Command::add(EMSdevice::DeviceType::SYSTEM, settings.ems_bus_id, F("send"), System::command_send); Command::add(EMSdevice::DeviceType::SYSTEM, settings.ems_bus_id, F("send"), System::command_send);
Command::add_with_json(EMSdevice::DeviceType::SYSTEM, F("info"), System::command_info); Command::add_with_json(EMSdevice::DeviceType::SYSTEM, F("info"), System::command_info);
@@ -165,7 +165,7 @@ void System::init() {
set_led(); // init LED set_led(); // init LED
// set the boolean format used for rendering booleans // set the boolean format used for rendering booleans
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
Helpers::bool_format(settings.bool_format); Helpers::bool_format(settings.bool_format);
analog_enabled_ = settings.analog_enabled; analog_enabled_ = settings.analog_enabled;
}); });
@@ -177,7 +177,7 @@ void System::init() {
// set the LED to on or off when in normal operating mode // set the LED to on or off when in normal operating mode
void System::set_led() { void System::set_led() {
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
hide_led_ = settings.hide_led; hide_led_ = settings.hide_led;
led_gpio_ = settings.led_gpio; led_gpio_ = settings.led_gpio;
if (led_gpio_) { if (led_gpio_) {
@@ -479,7 +479,7 @@ void System::show_system(uuid::console::Shell & shell) {
break; break;
} }
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
shell.println(); shell.println();
shell.printfln(F("Syslog:")); shell.printfln(F("Syslog:"));
shell.print(F_(1space)); shell.print(F_(1space));
@@ -717,7 +717,7 @@ bool System::check_upgrade() {
LittleFS.setConfig(l_cfg); LittleFS.setConfig(l_cfg);
LittleFS.begin(); LittleFS.begin();
EMSESP::esp8266React.begin(); EMSESP::esp8266React.begin();
EMSESP::emsespSettingsService.begin(); EMSESP::webSettingsService.begin();
EMSESP::esp8266React.getWiFiSettingsService()->update( EMSESP::esp8266React.getWiFiSettingsService()->update(
[&](WiFiSettings & wifiSettings) { [&](WiFiSettings & wifiSettings) {
@@ -796,8 +796,8 @@ bool System::check_upgrade() {
Serial.println(); Serial.println();
#endif #endif
custom_settings = doc["settings"]; custom_settings = doc["settings"];
EMSESP::emsespSettingsService.update( EMSESP::webSettingsService.update(
[&](EMSESPSettings & settings) { [&](WebSettings & settings) {
settings.tx_mode = custom_settings["tx_mode"] | EMSESP_DEFAULT_TX_MODE; settings.tx_mode = custom_settings["tx_mode"] | EMSESP_DEFAULT_TX_MODE;
settings.shower_alert = custom_settings["shower_alert"] | EMSESP_DEFAULT_SHOWER_ALERT; settings.shower_alert = custom_settings["shower_alert"] | EMSESP_DEFAULT_SHOWER_ALERT;
settings.shower_timer = custom_settings["shower_timer"] | EMSESP_DEFAULT_SHOWER_TIMER; settings.shower_timer = custom_settings["shower_timer"] | EMSESP_DEFAULT_SHOWER_TIMER;
@@ -913,7 +913,7 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & outp
// node["password"] = settings.password; // node["password"] = settings.password;
}); });
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
char s[7]; char s[7];
JsonObject node = output.createNestedObject("Settings"); JsonObject node = output.createNestedObject("Settings");
node["tx_mode"] = settings.tx_mode; node["tx_mode"] = settings.tx_mode;
@@ -968,7 +968,7 @@ bool System::command_report(const char * value, const int8_t id, JsonObject & ou
node["mqtt_retain"] = Helpers::render_boolean(s, settings.mqtt_retain); node["mqtt_retain"] = Helpers::render_boolean(s, settings.mqtt_retain);
}); });
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
char s[7]; char s[7];
node["tx_mode"] = settings.tx_mode; node["tx_mode"] = settings.tx_mode;
node["ems_bus_id"] = settings.ems_bus_id; node["ems_bus_id"] = settings.ems_bus_id;

View File

@@ -236,7 +236,7 @@ void TxService::flush_tx_queue() {
// send out request to EMS bus for all devices // send out request to EMS bus for all devices
void TxService::start() { void TxService::start() {
// grab the bus ID and tx_mode // grab the bus ID and tx_mode
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
ems_bus_id(settings.ems_bus_id); ems_bus_id(settings.ems_bus_id);
tx_mode(settings.tx_mode); tx_mode(settings.tx_mode);
}); });