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

@@ -2,8 +2,8 @@
# Remember to also enable CORS in platformio.ini before uploading the code to the device.
# ESP32 dev
REACT_APP_HTTP_ROOT=http://192.168.1.32
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.1.32
REACT_APP_HTTP_ROOT=http://10.10.10.101
REACT_APP_WEB_SOCKET_ROOT=ws://10.10.10.101
# ESP8266 dev
#REACT_APP_HTTP_ROOT=http://10.10.10.140

View File

@@ -151,6 +151,17 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {
<MenuItem value={1}>by Sensor ID</MenuItem>
<MenuItem value={2}>by Number</MenuItem>
</SelectValidator>
<SelectValidator name="bool_format"
label="Boolean Format"
value={data.bool_format}
fullWidth
variant="outlined"
onChange={handleValueChange('bool_format')}
margin="normal">
<MenuItem value={1}>on/off</MenuItem>
<MenuItem value={2}>true/false</MenuItem>
<MenuItem value={3}>1/0</MenuItem>
</SelectValidator>
<BlockFormControlLabel
control={
<Checkbox

View File

@@ -35,6 +35,7 @@ export interface MqttSettings {
publish_time_other: number;
publish_time_sensor: number;
dallas_format: number;
bool_format: number;
mqtt_qos: number;
mqtt_retain: boolean;
ha_enabled: boolean;

View File

@@ -282,21 +282,6 @@ function EMSESPSettingsControllerForm(props: EMSESPSettingsControllerFormProps)
label="Enable ADC"
/>
<br></br>
<Typography variant="h6" color="primary" >
Other
</Typography>
<SelectValidator name="bool_format"
label="Boolean Format"
value={data.bool_format}
fullWidth
variant="outlined"
onChange={handleValueChange('bool_format')}
margin="normal">
<MenuItem value={1}>on/off</MenuItem>
<MenuItem value={2}>true/false</MenuItem>
<MenuItem value={3}>1/0</MenuItem>
</SelectValidator>
<br></br>
<FormActions>
<FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit">
Save

View File

@@ -16,7 +16,6 @@ export interface EMSESPSettings {
led_gpio: number;
hide_led: boolean;
api_enabled: boolean;
bool_format: number;
analog_enabled: boolean;
trace_raw: boolean;
}

View File

@@ -46,7 +46,7 @@ void APSettingsService::manageAP() {
}
void APSettingsService::startAP() {
Serial.println(F("Starting software access point"));
// Serial.println(F("Starting software access point"));
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str());
if (!_dnsServer) {
@@ -65,7 +65,7 @@ void APSettingsService::stopAP() {
delete _dnsServer;
_dnsServer = nullptr;
}
Serial.println(F("Stopping software access point"));
// Serial.println(F("Stopping software access point"));
WiFi.softAPdisconnect(true);
}

View File

@@ -185,6 +185,7 @@ void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
root["mqtt_qos"] = settings.mqtt_qos;
root["mqtt_retain"] = settings.mqtt_retain;
root["dallas_format"] = settings.dallas_format;
root["bool_format"] = settings.bool_format;
root["ha_climate_format"] = settings.ha_climate_format;
root["ha_enabled"] = settings.ha_enabled;
}
@@ -214,6 +215,7 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
newSettings.publish_time_sensor = root["publish_time_sensor"] | EMSESP_DEFAULT_PUBLISH_TIME;
newSettings.dallas_format = root["dallas_format"] | EMSESP_DEFAULT_DALLAS_FORMAT;
newSettings.bool_format = root["bool_format"] | EMSESP_DEFAULT_BOOL_FORMAT;
newSettings.ha_climate_format = root["ha_climate_format"] | EMSESP_DEFAULT_HA_CLIMATE_FORMAT;
newSettings.ha_enabled = root["ha_enabled"] | EMSESP_DEFAULT_HA_ENABLED;

View File

@@ -60,6 +60,8 @@ static String generateClientId() {
#define FACTORY_MQTT_MAX_TOPIC_LENGTH 128
#endif
#define EMSESP_DEFAULT_BOOL_FORMAT 1 // on/off
#define EMSESP_DEFAULT_DALLAS_FORMAT 1 // sensorid
#define EMSESP_DEFAULT_HA_CLIMATE_FORMAT 1 // current temp
#define EMSESP_DEFAULT_MQTT_QOS 0
@@ -101,6 +103,7 @@ class MqttSettings {
uint8_t mqtt_qos;
bool mqtt_retain;
uint8_t dallas_format;
uint8_t bool_format;
uint8_t ha_climate_format;
bool ha_enabled;

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;