clean up boolean, move to MQTT screen

This commit is contained in:
proddy
2021-02-23 17:45:48 +01:00
parent 1d64285f09
commit b2e85ee026
16 changed files with 53 additions and 68 deletions

View File

@@ -47,7 +47,6 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) {
root["led_gpio"] = settings.led_gpio;
root["hide_led"] = settings.hide_led;
root["api_enabled"] = settings.api_enabled;
root["bool_format"] = settings.bool_format;
root["analog_enabled"] = settings.analog_enabled;
}
@@ -93,10 +92,9 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
}
// other
snprintf_P(&crc_before[0], crc_before.capacity() + 1, PSTR("%d%d"), settings.bool_format, settings.analog_enabled);
settings.bool_format = root["bool_format"] | EMSESP_DEFAULT_BOOL_FORMAT;
snprintf_P(&crc_before[0], crc_before.capacity() + 1, PSTR("%d"), settings.analog_enabled);
settings.analog_enabled = root["analog_enabled"] | EMSESP_DEFAULT_ANALOG_ENABLED;
snprintf_P(&crc_after[0], crc_after.capacity() + 1, PSTR("%d%d"), settings.bool_format, settings.analog_enabled);
snprintf_P(&crc_after[0], crc_after.capacity() + 1, PSTR("%d"), settings.analog_enabled);
if (crc_before != crc_after) {
add_flags(ChangeFlags::OTHER);
}

View File

@@ -89,7 +89,6 @@ class WebSettings {
uint8_t led_gpio;
bool hide_led;
bool api_enabled;
uint8_t bool_format;
bool analog_enabled;
static void read(WebSettings & settings, JsonObject & root);

View File

@@ -499,10 +499,10 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
data.add(*(uint8_t *)(dv.value_p) ? dv.options[0] : dv.options[1]);
} else {
// see how to render the value depending on the setting
if (Helpers::bool_format() == BOOL_FORMAT_ONOFF) {
if (Mqtt::bool_format() == BOOL_FORMAT_ONOFF) {
// on or off as strings
data.add(*(uint8_t *)(dv.value_p) ? F_(on) : F_(off));
} else if (Helpers::bool_format() == BOOL_FORMAT_TRUEFALSE) {
} else if (Mqtt::bool_format() == BOOL_FORMAT_TRUEFALSE) {
// true or false values (not strings)
data.add((bool)(*(uint8_t *)(dv.value_p)) ? true : false);
} else {
@@ -645,11 +645,11 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
has_value = true;
} else {
// see how to render the value depending on the setting
if (Helpers::bool_format() == BOOL_FORMAT_ONOFF) {
if (Mqtt::bool_format() == BOOL_FORMAT_ONOFF) {
// on or off as strings
json[name] = *(uint8_t *)(dv.value_p) ? F_(on) : F_(off);
has_value = true;
} else if (Helpers::bool_format() == BOOL_FORMAT_TRUEFALSE) {
} else if (Mqtt::bool_format() == BOOL_FORMAT_TRUEFALSE) {
// true or false values (not strings)
json[name] = (bool)(*(uint8_t *)(dv.value_p)) ? true : false;
has_value = true;

View File

@@ -17,11 +17,10 @@
*/
#include "helpers.h"
#include "mqtt.h"
namespace emsesp {
uint8_t Helpers::bool_format_ = BOOL_FORMAT_ONOFF; // on/off
// like itoa but for hex, and quicker
// note: only for single byte hex values
char * Helpers::hextoa(char * result, const uint8_t value) {
@@ -125,9 +124,10 @@ char * Helpers::smallitoa(char * result, const uint16_t value) {
// work out how to display booleans
char * Helpers::render_boolean(char * result, bool value) {
if (bool_format() == BOOL_FORMAT_ONOFF) {
uint8_t bool_format_ = Mqtt::bool_format();
if (bool_format_ == BOOL_FORMAT_ONOFF) {
strlcpy(result, value ? "on" : "off", 5);
} else if (bool_format() == BOOL_FORMAT_TRUEFALSE) {
} else if (bool_format_ == BOOL_FORMAT_TRUEFALSE) {
strlcpy(result, value ? "true" : "false", 7);
} else {
strlcpy(result, value ? "1" : "0", 2);

View File

@@ -23,8 +23,6 @@
#include "telegram.h" // for EMS_VALUE_* settings
enum { BOOL_FORMAT_ONOFF = 1, BOOL_FORMAT_TRUEFALSE, BOOL_FORMAT_NUMBERS }; // matches Web UI settings
// #define FJSON(x) x
#define FJSON(x) F(x)
@@ -68,20 +66,9 @@ class Helpers {
static bool value2string(const char * v, std::string & value);
static bool value2enum(const char * v, uint8_t & value, const flash_string_vector & strs);
static void bool_format(uint8_t bool_format) {
bool_format_ = bool_format;
}
static uint8_t bool_format() {
return bool_format_;
}
#ifdef EMSESP_STANDALONE
static char * ultostr(char * ptr, uint32_t value, const uint8_t base);
#endif
private:
static uint8_t bool_format_;
};
} // namespace emsesp

View File

@@ -36,6 +36,7 @@ uint32_t Mqtt::publish_time_sensor_;
uint32_t Mqtt::publish_time_other_;
bool Mqtt::mqtt_enabled_;
uint8_t Mqtt::dallas_format_;
uint8_t Mqtt::bool_format_;
uint8_t Mqtt::ha_climate_format_;
bool Mqtt::ha_enabled_;
@@ -467,6 +468,7 @@ void Mqtt::on_connect() {
ha_enabled_ = mqttSettings.ha_enabled;
ha_climate_format_ = mqttSettings.ha_climate_format;
dallas_format_ = mqttSettings.dallas_format;
bool_format_ = mqttSettings.bool_format;
});
// first time to connect

View File

@@ -50,6 +50,8 @@ using uuid::console::Shell;
#define MAX_MQTT_MESSAGES 20
#endif
enum { BOOL_FORMAT_ONOFF = 1, BOOL_FORMAT_TRUEFALSE, BOOL_FORMAT_10 }; // matches Web UI settings
namespace emsesp {
using mqtt_subfunction_p = std::function<bool(const char * message)>;
@@ -156,6 +158,10 @@ class Mqtt {
return dallas_format_;
}
static uint8_t bool_format() {
return bool_format_;
}
static bool ha_enabled() {
return ha_enabled_;
}
@@ -265,6 +271,7 @@ class Mqtt {
static uint32_t publish_time_sensor_;
static bool mqtt_enabled_;
static uint8_t dallas_format_;
static uint8_t bool_format_;
static uint8_t ha_climate_format_;
static bool ha_enabled_;
};

View File

@@ -258,7 +258,6 @@ void System::start(uint32_t heap_start) {
void System::other_init() {
// set the boolean format used for rendering booleans
EMSESP::webSettingsService.read([&](WebSettings & settings) {
Helpers::bool_format(settings.bool_format);
analog_enabled_ = settings.analog_enabled;
});
}
@@ -767,7 +766,6 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) {
JsonObject node = json.createNestedObject("WIFI");
node["ssid"] = settings.ssid;
// node["password"] = settings.password;
node["hostname"] = settings.hostname;
node["static_ip_config"] = settings.staticIPConfig;
JsonUtils::writeIP(node, "local_ip", settings.localIP);
@@ -782,7 +780,6 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
JsonObject node = json.createNestedObject("AP");
node["provision_mode"] = settings.provisionMode;
node["ssid"] = settings.ssid;
// node["password"] = settings.password;
node["local_ip"] = settings.localIP.toString();
node["gateway_ip"] = settings.gatewayIP.toString();
node["subnet_mask"] = settings.subnetMask.toString();
@@ -790,17 +787,15 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
#endif
EMSESP::esp8266React.getMqttSettingsService()->read([&](MqttSettings & settings) {
char s[7];
JsonObject node = json.createNestedObject("MQTT");
node["enabled"] = Helpers::render_boolean(s, settings.enabled);
// node["password"] = settings.password;
node["enabled"] = settings.enabled;
#ifndef EMSESP_STANDALONE
node["host"] = settings.host;
node["port"] = settings.port;
node["username"] = settings.username;
node["client_id"] = settings.clientId;
node["keep_alive"] = settings.keepAlive;
node["clean_session"] = Helpers::render_boolean(s, settings.cleanSession);
node["clean_session"] = settings.cleanSession;
#endif
node["publish_time_boiler"] = settings.publish_time_boiler;
node["publish_time_thermostat"] = settings.publish_time_thermostat;
@@ -809,52 +804,48 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
node["publish_time_other"] = settings.publish_time_other;
node["publish_time_sensor"] = settings.publish_time_sensor;
node["dallas_format"] = settings.dallas_format;
node["bool_format"] = settings.bool_format;
node["ha_climate_format"] = settings.ha_climate_format;
node["ha_enabled"] = settings.ha_enabled;
node["mqtt_qos"] = settings.mqtt_qos;
node["mqtt_retain"] = Helpers::render_boolean(s, settings.mqtt_retain);
node["mqtt_retain"] = settings.mqtt_retain;
});
#ifndef EMSESP_STANDALONE
EMSESP::esp8266React.getNTPSettingsService()->read([&](NTPSettings & settings) {
char s[7];
JsonObject node = json.createNestedObject("NTP");
node["enabled"] = Helpers::render_boolean(s, settings.enabled);
node["enabled"] = settings.enabled;
node["server"] = settings.server;
node["tz_label"] = settings.tzLabel;
node["tz_format"] = settings.tzFormat;
});
EMSESP::esp8266React.getOTASettingsService()->read([&](OTASettings & settings) {
char s[7];
JsonObject node = json.createNestedObject("OTA");
node["enabled"] = Helpers::render_boolean(s, settings.enabled);
node["enabled"] = settings.enabled;
node["port"] = settings.port;
// node["password"] = settings.password;
});
#endif
EMSESP::webSettingsService.read([&](WebSettings & settings) {
char s[7];
JsonObject node = json.createNestedObject("Settings");
node["tx_mode"] = settings.tx_mode;
node["ems_bus_id"] = settings.ems_bus_id;
node["syslog_enabled"] = Helpers::render_boolean(s, settings.syslog_enabled);
node["syslog_enabled"] = settings.syslog_enabled;
node["syslog_level"] = settings.syslog_level;
node["syslog_mark_interval"] = settings.syslog_mark_interval;
node["syslog_host"] = settings.syslog_host;
node["master_thermostat"] = settings.master_thermostat;
node["shower_timer"] = Helpers::render_boolean(s, settings.shower_timer);
node["shower_alert"] = Helpers::render_boolean(s, settings.shower_alert);
node["shower_timer"] = settings.shower_timer;
node["shower_alert"] = settings.shower_alert;
node["rx_gpio"] = settings.rx_gpio;
node["tx_gpio"] = settings.tx_gpio;
node["dallas_gpio"] = settings.dallas_gpio;
node["dallas_parasite"] = Helpers::render_boolean(s, settings.dallas_parasite);
node["dallas_parasite"] = settings.dallas_parasite;
node["led_gpio"] = settings.led_gpio;
node["hide_led"] = Helpers::render_boolean(s, settings.hide_led);
node["api_enabled"] = Helpers::render_boolean(s, settings.api_enabled);
node["bool_format"] = settings.bool_format;
node["analog_enabled"] = Helpers::render_boolean(s, settings.analog_enabled);
node["hide_led"] = settings.hide_led;
node["api_enabled"] = settings.api_enabled;
node["analog_enabled"] = settings.analog_enabled;
});
return true;