minor cleanup of debug statements #632

This commit is contained in:
proddy
2020-12-14 21:27:25 +01:00
parent 6a67808ddb
commit 4087a4f12e
2 changed files with 13 additions and 21 deletions

View File

@@ -23,7 +23,6 @@ namespace emsesp {
// mapping of UOM, to match order in DeviceValueUOM enum emsdevice.h // mapping of UOM, to match order in DeviceValueUOM enum emsdevice.h
// must be an int of 4 bytes, 32bit aligned // must be an int of 4 bytes, 32bit aligned
// TODO see if its easier to use const strings here
static const __FlashStringHelper * DeviceValueUOM_s[] __attribute__((__aligned__(sizeof(int)))) PROGMEM = { static const __FlashStringHelper * DeviceValueUOM_s[] __attribute__((__aligned__(sizeof(int)))) PROGMEM = {
F_(degrees), F_(degrees),
@@ -67,7 +66,7 @@ const std::string EMSdevice::tag_to_string(uint8_t tag) {
if (tag == DeviceValueTAG::TAG_NONE) { if (tag == DeviceValueTAG::TAG_NONE) {
return std::string{}; return std::string{};
} }
return uuid::read_flash_string(DeviceValueTAG_s[tag]); return uuid::read_flash_string(DeviceValueTAG_s[tag - 1]); // offset by 1 to account for NONE
} }
const std::vector<EMSdevice::DeviceValue> EMSdevice::devicevalues() const { const std::vector<EMSdevice::DeviceValue> EMSdevice::devicevalues() const {
@@ -532,8 +531,6 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
// For each value in the device create the json object pair and add it to given json // For each value in the device create the json object pair and add it to given json
// return false if empty // return false if empty
// TODO fix tag_filter!
bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter, const bool verbose) { bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter, const bool verbose) {
bool has_value = false; // to see if we've added a value. it's faster than doing a json.size() at the end bool has_value = false; // to see if we've added a value. it's faster than doing a json.size() at the end
uint8_t old_tag = 255; uint8_t old_tag = 255;

View File

@@ -745,17 +745,14 @@ void Mqtt::register_mqtt_ha_sensor(uint8_t type, // device v
// DynamicJsonDocument doc(EMSESP_JSON_SIZE_HA_CONFIG); // DynamicJsonDocument doc(EMSESP_JSON_SIZE_HA_CONFIG);
StaticJsonDocument<EMSESP_JSON_SIZE_HA_CONFIG> doc; // TODO see if this crashes ESP8266? StaticJsonDocument<EMSESP_JSON_SIZE_HA_CONFIG> doc; // TODO see if this crashes ESP8266?
// TODO fix prefix bool have_prefix = ((tag != DeviceValueTAG::TAG_NONE) && (device_type != EMSdevice::DeviceType::BOILER));
char prefix[10] = "test"; // TODO remove!
bool have_prefix = ((prefix[0] != '\0') && (device_type != EMSdevice::DeviceType::BOILER));
// create entity by inserting any given prefix // create entity by inserting any given prefix
// we ignore the tag if BOILER // we ignore the tag if BOILER
char new_entity[50]; char new_entity[50];
// special case for boiler - don't use the prefix // special case for boiler - don't use the prefix
if (have_prefix) { if (have_prefix) {
snprintf_P(new_entity, sizeof(new_entity), PSTR("%s.%s"), prefix, uuid::read_flash_string(entity).c_str()); snprintf_P(new_entity, sizeof(new_entity), PSTR("%s.%s"), EMSdevice::tag_to_string(tag).c_str(), uuid::read_flash_string(entity).c_str());
} else { } else {
snprintf_P(new_entity, sizeof(new_entity), PSTR("%s"), uuid::read_flash_string(entity).c_str()); snprintf_P(new_entity, sizeof(new_entity), PSTR("%s"), uuid::read_flash_string(entity).c_str());
} }
@@ -769,35 +766,29 @@ void Mqtt::register_mqtt_ha_sensor(uint8_t type, // device v
std::string uniq(50, '\0'); std::string uniq(50, '\0');
snprintf_P(&uniq[0], uniq.capacity() + 1, PSTR("%s_%s"), device_name, new_entity); snprintf_P(&uniq[0], uniq.capacity() + 1, PSTR("%s_%s"), device_name, new_entity);
std::replace(uniq.begin(), uniq.end(), '.', '_'); std::replace(uniq.begin(), uniq.end(), '.', '_');
doc["uniq_id"] = uniq;
// topic
char topic[MQTT_TOPIC_MAX_SIZE];
// state topic // state topic
// if its a boiler we use the tag // if its a boiler we use the tag
char stat_t[MQTT_TOPIC_MAX_SIZE]; char stat_t[MQTT_TOPIC_MAX_SIZE];
if (device_type == EMSdevice::DeviceType::BOILER) { if (device_type == EMSdevice::DeviceType::BOILER) {
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/%s"), hostname_.c_str(), prefix); snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/%s"), hostname_.c_str(), EMSdevice::tag_to_string(tag).c_str());
} else { } else {
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/%s_data"), hostname_.c_str(), device_name); snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/%s_data"), hostname_.c_str(), device_name);
} }
doc["stat_t"] = stat_t;
// ha device
char ha_device[40];
snprintf_P(ha_device, sizeof(ha_device), PSTR("ems-esp-%s"), device_name);
// name // name
char new_name[50]; char new_name[50];
if (have_prefix) { if (have_prefix) {
snprintf_P(new_name, sizeof(new_name), PSTR("%s %s %s"), device_name, prefix, uuid::read_flash_string(name).c_str()); snprintf_P(new_name, sizeof(new_name), PSTR("%s %s %s"), device_name, EMSdevice::tag_to_string(tag).c_str(), uuid::read_flash_string(name).c_str());
} else { } else {
snprintf_P(new_name, sizeof(new_name), PSTR("%s %s"), device_name, uuid::read_flash_string(name).c_str()); snprintf_P(new_name, sizeof(new_name), PSTR("%s %s"), device_name, uuid::read_flash_string(name).c_str());
} }
new_name[0] = toupper(new_name[0]); // capitalize first letter new_name[0] = toupper(new_name[0]); // capitalize first letter
doc["name"] = new_name;
doc["name"] = new_name; char topic[MQTT_TOPIC_MAX_SIZE]; // reserved for topic
doc["uniq_id"] = uniq;
doc["stat_t"] = stat_t;
// look at the device value type // look at the device value type
if (type != DeviceValueType::BOOL) { if (type != DeviceValueType::BOOL) {
@@ -850,6 +841,10 @@ void Mqtt::register_mqtt_ha_sensor(uint8_t type, // device v
JsonObject dev = doc.createNestedObject("dev"); JsonObject dev = doc.createNestedObject("dev");
JsonArray ids = dev.createNestedArray("ids"); JsonArray ids = dev.createNestedArray("ids");
// ha device
char ha_device[40];
snprintf_P(ha_device, sizeof(ha_device), PSTR("ems-esp-%s"), device_name);
ids.add(ha_device); ids.add(ha_device);
publish_ha(topic, doc.as<JsonObject>()); publish_ha(topic, doc.as<JsonObject>());