fix devicevalue sorting the Michael way (thanks)

This commit is contained in:
proddy
2022-03-28 17:50:33 +02:00
parent bad6346e7a
commit 592c5ca778

View File

@@ -637,17 +637,11 @@ void EMSdevice::generate_values_web(JsonObject & output) {
output["label"] = to_string_short();
JsonArray data = output.createNestedArray("data");
// sort the device values
std::sort(devicevalues_.begin(), devicevalues_.end(), [](const emsesp::DeviceValue & a, const emsesp::DeviceValue & b __attribute__((unused))) {
return a.has_state(DeviceValueState::DV_FAVORITE);
});
// do two passes. First for all entities marked as favourites, then for all others. This sorts the list.
for (uint8_t i = 0; i < 2; i++) {
for (auto & dv : devicevalues_) {
// check conditions:
// 1. full_name cannot be empty
// 2. it must have a valid value, if it is not a command like 'reset'
if (!dv.has_state(DeviceValueState::DV_WEB_EXCLUDE) && dv.full_name && (dv.hasValue() || (dv.type == DeviceValueType::CMD))) {
bool state = (!i && dv.has_state(DeviceValueState::DV_FAVORITE)) || (i && !dv.has_state(DeviceValueState::DV_FAVORITE));
if (state && (!dv.has_state(DeviceValueState::DV_WEB_EXCLUDE) && dv.full_name && (dv.hasValue() || (dv.type == DeviceValueType::CMD)))) {
JsonObject obj = data.createNestedObject(); // create the object, we know there is a value
uint8_t fahrenheit = 0;
@@ -701,13 +695,15 @@ void EMSdevice::generate_values_web(JsonObject & output) {
// add the unit of measure (uom)
obj["u"] = fahrenheit ? (uint8_t)DeviceValueUOM::FAHRENHEIT : dv.uom;
auto mask = Helpers::hextoa((uint8_t)(dv.state >> 4), false); // create mask to a 2-char string
// add name, prefixing the tag if it exists
if ((dv.tag == DeviceValueTAG::TAG_NONE) || tag_to_string(dv.tag).empty()) {
obj["n"] = dv.full_name;
obj["n"] = mask + read_flash_string(dv.full_name);
} else if (dv.tag < DeviceValueTAG::TAG_HC1) {
obj["n"] = tag_to_string(dv.tag) + " " + read_flash_string(dv.full_name);
obj["n"] = mask + tag_to_string(dv.tag) + " " + read_flash_string(dv.full_name);
} else {
obj["n"] = tag_to_string(dv.tag) + " " + read_flash_string(dv.full_name);
obj["n"] = mask + tag_to_string(dv.tag) + " " + read_flash_string(dv.full_name);
}
// add commands and options
@@ -756,6 +752,7 @@ void EMSdevice::generate_values_web(JsonObject & output) {
}
}
}
}
// as generate_values_web() but stripped down to only show all entities and their state
// this is used only for WebCustomizationService::device_entities()
@@ -853,7 +850,7 @@ void EMSdevice::generate_values_web_all(JsonArray & output) {
// this is called before loading in the exclude entities list from the customization service
void EMSdevice::reset_entity_masks() {
for (auto & dv : devicevalues_) {
dv.state &= 0x0F;
dv.state &= 0x0F; // clear high nibble
}
}