mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 08:49:52 +03:00
optimize device_info
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user