optimize device_info

This commit is contained in:
proddy
2024-07-12 15:20:48 +02:00
parent f839c07d23
commit fbe20fec41
10 changed files with 146 additions and 105 deletions

View File

@@ -1425,7 +1425,7 @@ void EMSdevice::dump_telegram_info(std::vector<TelegramFunctionDump> & telegram_
}
#endif
// builds json for a specific device value / entity
// builds json for a specific EMS device value / entity
// cmd is the endpoint or name of the device entity
// returns false if failed, otherwise true
bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t tag) {
@@ -1582,6 +1582,9 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t
json["writeable"] = dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY);
json["visible"] = !dv.has_state(DeviceValueState::DV_WEB_EXCLUDE);
// TODO refactor to remove containsKey as it's costly and not advisable to use it
// https://arduinojson.org/v7/api/jsonobject/containskey/#avoid
// if there is no value, mention it
if (!json.containsKey(value)) {
json[value] = "not set";
@@ -1590,23 +1593,22 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t
// if we're filtering on an attribute, go find it
if (attribute_s) {
#if defined(EMSESP_DEBUG)
EMSESP::logger().debug("[DEBUG] single attribute '%s'", attribute_s);
EMSESP::logger().debug("[DEBUG] fetching single attribute '%s'", attribute_s);
#endif
if (json.containsKey(attribute_s)) {
String data = json[attribute_s].as<String>();
std::string data = json[attribute_s].as<std::string>();
output.clear();
output["api_data"] = data;
output["api_data"] = data; // always as string
return true;
} else {
return EMSESP::return_not_found(output, "attribute", command_s); // not found
}
return EMSESP::return_not_found(output, "attribute", command_s); // not found
}
return true;
}
}
return EMSESP::return_not_found(output, "entity data", cmd); // not found
return false; // not found, but don't return a message error yet
}
// mqtt publish all single values from one device (used for time schedule)
@@ -1934,7 +1936,7 @@ std::string EMSdevice::name() {
// returns true on success.
int EMSdevice::get_modbus_value(uint8_t tag, const std::string & shortname, std::vector<uint16_t> & result) {
// find device value by shortname
// TODO linear search is inefficient
// TODO replace linear search which is inefficient
const auto & it = std::find_if(devicevalues_.begin(), devicevalues_.end(), [&](const DeviceValue & x) { return x.tag == tag && x.short_name == shortname; });
if (it == devicevalues_.end())
return -1;