mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 01:09:51 +03:00
refactor device value rendering (to Web, Console or MQTT) to base class #632
This commit is contained in:
@@ -188,9 +188,11 @@ void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
|
||||
root["publish_time_mixer"] = settings.publish_time_mixer;
|
||||
root["publish_time_other"] = settings.publish_time_other;
|
||||
root["publish_time_sensor"] = settings.publish_time_sensor;
|
||||
root["mqtt_format"] = settings.mqtt_format;
|
||||
root["mqtt_qos"] = settings.mqtt_qos;
|
||||
root["mqtt_retain"] = settings.mqtt_retain;
|
||||
root["dallas_format"] = settings.dallas_format;
|
||||
root["ha_climate_format"] = settings.ha_climate_format;
|
||||
root["ha_enabled"] = settings.ha_enabled;
|
||||
}
|
||||
|
||||
StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & settings) {
|
||||
@@ -205,6 +207,8 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
|
||||
newSettings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE;
|
||||
newSettings.cleanSession = root["clean_session"] | FACTORY_MQTT_CLEAN_SESSION;
|
||||
newSettings.maxTopicLength = root["max_topic_length"] | FACTORY_MQTT_MAX_TOPIC_LENGTH;
|
||||
newSettings.mqtt_qos = root["mqtt_qos"] | EMSESP_DEFAULT_MQTT_QOS;
|
||||
newSettings.mqtt_retain = root["mqtt_retain"] | EMSESP_DEFAULT_MQTT_RETAIN;
|
||||
|
||||
newSettings.publish_time_boiler = root["publish_time_boiler"] | EMSESP_DEFAULT_PUBLISH_TIME;
|
||||
newSettings.publish_time_thermostat = root["publish_time_thermostat"] | EMSESP_DEFAULT_PUBLISH_TIME;
|
||||
@@ -212,16 +216,25 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
|
||||
newSettings.publish_time_mixer = root["publish_time_mixer"] | EMSESP_DEFAULT_PUBLISH_TIME;
|
||||
newSettings.publish_time_other = root["publish_time_other"] | EMSESP_DEFAULT_PUBLISH_TIME;
|
||||
newSettings.publish_time_sensor = root["publish_time_sensor"] | EMSESP_DEFAULT_PUBLISH_TIME;
|
||||
newSettings.mqtt_format = root["mqtt_format"] | EMSESP_DEFAULT_MQTT_FORMAT;
|
||||
newSettings.mqtt_qos = root["mqtt_qos"] | EMSESP_DEFAULT_MQTT_QOS;
|
||||
newSettings.mqtt_retain = root["mqtt_retain"] | EMSESP_DEFAULT_MQTT_RETAIN;
|
||||
|
||||
newSettings.dallas_format = root["dallas_format"] | EMSESP_DEFAULT_DALLAS_FORMAT;
|
||||
newSettings.ha_climate_format = root["ha_climate_format"] | EMSESP_DEFAULT_HA_CLIMATE_FORMAT;
|
||||
newSettings.ha_enabled = root["ha_enabled"] | EMSESP_DEFAULT_HA_ENABLED;
|
||||
|
||||
if (newSettings.mqtt_qos != settings.mqtt_qos) {
|
||||
emsesp::EMSESP::mqtt_.set_qos(newSettings.mqtt_qos);
|
||||
}
|
||||
|
||||
if (newSettings.mqtt_format != settings.mqtt_format) {
|
||||
emsesp::EMSESP::mqtt_.set_format(newSettings.mqtt_format);
|
||||
if (newSettings.dallas_format != settings.dallas_format) {
|
||||
emsesp::EMSESP::mqtt_.dallas_format(newSettings.dallas_format);
|
||||
}
|
||||
|
||||
if (newSettings.ha_climate_format != settings.ha_climate_format) {
|
||||
emsesp::EMSESP::mqtt_.ha_climate_format(newSettings.ha_climate_format);
|
||||
}
|
||||
|
||||
if (newSettings.ha_enabled != settings.ha_enabled) {
|
||||
emsesp::EMSESP::mqtt_.ha_enabled(newSettings.ha_enabled);
|
||||
}
|
||||
|
||||
if (newSettings.mqtt_retain != settings.mqtt_retain) {
|
||||
|
||||
@@ -60,9 +60,11 @@ static String generateClientId() {
|
||||
#define FACTORY_MQTT_MAX_TOPIC_LENGTH 128
|
||||
#endif
|
||||
|
||||
#define EMSESP_DEFAULT_MQTT_FORMAT 2 // nested
|
||||
#define EMSESP_DEFAULT_DALLAS_FORMAT 1 // sensorid
|
||||
#define EMSESP_DEFAULT_HA_CLIMATE_FORMAT 1 // current temp
|
||||
#define EMSESP_DEFAULT_MQTT_QOS 0
|
||||
#define EMSESP_DEFAULT_MQTT_RETAIN false
|
||||
#define EMSESP_DEFAULT_HA_ENABLED false
|
||||
#define EMSESP_DEFAULT_PUBLISH_TIME 10
|
||||
|
||||
class MqttSettings {
|
||||
@@ -91,9 +93,11 @@ class MqttSettings {
|
||||
uint16_t publish_time_mixer;
|
||||
uint16_t publish_time_other;
|
||||
uint16_t publish_time_sensor;
|
||||
uint8_t mqtt_format; // 1=single, 2=nested, 3=ha, 4=custom
|
||||
uint8_t mqtt_qos;
|
||||
bool mqtt_retain;
|
||||
uint8_t dallas_format;
|
||||
uint8_t ha_climate_format;
|
||||
bool ha_enabled;
|
||||
|
||||
static void read(MqttSettings & settings, JsonObject & root);
|
||||
static StateUpdateResult update(JsonObject & root, MqttSettings & settings);
|
||||
|
||||
71
lib/uuid-common/src/compare_flash_string.cpp
Normal file
71
lib/uuid-common/src/compare_flash_string.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/proddy/EMS-ESP
|
||||
* Copyright 2020 Paul Derbyshire
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <uuid/common.h>
|
||||
|
||||
// #ifdef ESP8266
|
||||
// #include <pgmspace.h>
|
||||
// #else
|
||||
// #include <avr/pgmspace.h>
|
||||
// #endif
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace uuid {
|
||||
|
||||
// On ESP8266, pgm_read_byte() already takes care of 4-byte alignment, and
|
||||
// memcpy_P(s, p, 4) makes 4 calls to pgm_read_byte() anyway, so don't bother
|
||||
// optimizing for 4-byte alignment here.
|
||||
|
||||
// class __FlashStringHelper;
|
||||
|
||||
int compare_flash_string(const __FlashStringHelper * a, const __FlashStringHelper * b) {
|
||||
const char * aa = reinterpret_cast<const char *>(a);
|
||||
const char * bb = reinterpret_cast<const char *>(b);
|
||||
|
||||
while (true) {
|
||||
uint8_t ca = pgm_read_byte(aa);
|
||||
uint8_t cb = pgm_read_byte(bb);
|
||||
if (ca != cb)
|
||||
return (int)ca - (int)cb;
|
||||
if (ca == 0)
|
||||
return 0;
|
||||
aa++;
|
||||
bb++;
|
||||
}
|
||||
}
|
||||
|
||||
int compare_flash_string(const __FlashStringHelper * a, const __FlashStringHelper * b, size_t n) {
|
||||
const char * aa = reinterpret_cast<const char *>(a);
|
||||
const char * bb = reinterpret_cast<const char *>(b);
|
||||
|
||||
while (n > 0) {
|
||||
uint8_t ca = pgm_read_byte(aa);
|
||||
uint8_t cb = pgm_read_byte(bb);
|
||||
if (ca != cb)
|
||||
return (int)ca - (int)cb;
|
||||
if (ca == 0)
|
||||
return 0;
|
||||
aa++;
|
||||
bb++;
|
||||
n--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace uuid
|
||||
@@ -16,6 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// compare_flash_string added by Proddy
|
||||
|
||||
#ifndef UUID_COMMON_H_
|
||||
#define UUID_COMMON_H_
|
||||
|
||||
@@ -32,6 +34,21 @@
|
||||
*/
|
||||
namespace uuid {
|
||||
|
||||
/**
|
||||
* String compare two flash strings
|
||||
*
|
||||
* The flash string must be stored with appropriate alignment for
|
||||
* reading it on the platform.
|
||||
*
|
||||
* @param[in] a Pointer to string stored in flash.
|
||||
* @param[in] b Pointer to string stored in flash.
|
||||
* @param[in] n optional max length
|
||||
* @return 0 for match, otherwise diff
|
||||
* @since 1.0.0
|
||||
*/
|
||||
int compare_flash_string(const __FlashStringHelper * a, const __FlashStringHelper * b);
|
||||
int compare_flash_string(const __FlashStringHelper * a, const __FlashStringHelper * b, size_t n);
|
||||
|
||||
/**
|
||||
* Read a string from flash and convert it to a std::string.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user