process telegram selection of device

This commit is contained in:
MichaelDvP
2024-01-28 11:31:17 +01:00
parent 0760e6e021
commit 6155645436

View File

@@ -685,7 +685,7 @@ bool EMSESP::get_device_value_info(JsonObject root, const char * cmd, const int8
return EMSESP::webSchedulerService.get_value_info(root, cmd); return EMSESP::webSchedulerService.get_value_info(root, cmd);
} }
// own entities // custom entities
if (devicetype == DeviceType::CUSTOM) { if (devicetype == DeviceType::CUSTOM) {
return EMSESP::webCustomEntityService.get_value_info(root, cmd); return EMSESP::webCustomEntityService.get_value_info(root, cmd);
} }
@@ -721,20 +721,26 @@ std::string EMSESP::pretty_telegram(std::shared_ptr<const Telegram> telegram) {
std::string dest_name(""); std::string dest_name("");
std::string type_name(""); std::string type_name("");
for (const auto & emsdevice : emsdevices) { for (const auto & emsdevice : emsdevices) {
if (emsdevice) { // get src & dest
// get src & dest if (emsdevice->is_device_id(src)) {
if (emsdevice->is_device_id(src)) { src_name = emsdevice->device_type_name();
src_name = emsdevice->device_type_name(); } else if (emsdevice->is_device_id(dest)) {
} else if (emsdevice->is_device_id(dest)) { dest_name = emsdevice->device_type_name();
dest_name = emsdevice->device_type_name(); }
// get the type name
if (type_name.empty()) {
if ((telegram->operation == Telegram::Operation::RX_READ && emsdevice->is_device_id(dest))
|| (telegram->operation != Telegram::Operation::RX_READ && dest == 0 && emsdevice->is_device_id(src))
|| (telegram->operation != Telegram::Operation::RX_READ && src == EMSbus::ems_bus_id() && emsdevice->is_device_id(dest))) {
type_name = emsdevice->telegram_type_name(telegram);
} }
// get the type name }
if (type_name.empty()) { }
if ((telegram->operation == Telegram::Operation::RX_READ && emsdevice->is_device_id(dest)) if (type_name.empty()) {
|| (telegram->operation != Telegram::Operation::RX_READ && dest == 0 && emsdevice->is_device_id(src)) // fallback, get the type name from src
|| (telegram->operation != Telegram::Operation::RX_READ && src == EMSbus::ems_bus_id() && emsdevice->is_device_id(dest))) { for (const auto & emsdevice : emsdevices) {
type_name = emsdevice->telegram_type_name(telegram); if (telegram->operation != Telegram::Operation::RX_READ && emsdevice->is_device_id(src)) {
} type_name = emsdevice->telegram_type_name(telegram);
} }
} }
} }
@@ -919,49 +925,70 @@ bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) {
// calls the associated process function for that EMS device // calls the associated process function for that EMS device
// returns false if the device_id doesn't recognize it // returns false if the device_id doesn't recognize it
// after the telegram has been processed, see if there have been values changed and we need to do a MQTT publish // after the telegram has been processed, see if there have been values changed and we need to do a MQTT publish
bool found = false; bool telegram_found = false;
bool knowndevice = false; uint8_t device_found = 0;
// broadcast or send to us
for (const auto & emsdevice : emsdevices) { for (const auto & emsdevice : emsdevices) {
if (emsdevice->is_device_id(telegram->src) && (telegram->dest == 0 || telegram->dest == EMSbus::ems_bus_id()|| telegram->dest == 0x10)) { if (emsdevice->is_device_id(telegram->src) && (telegram->dest == 0 || telegram->dest == EMSbus::ems_bus_id())) {
knowndevice = true; telegram_found = emsdevice->handle_telegram(telegram);
found = emsdevice->handle_telegram(telegram); device_found = emsdevice->unique_id();
// if we correctly processed the telegram then follow up with sending it via MQTT (if enabled)
if (found && Mqtt::connected()) {
if ((mqtt_.get_publish_onchange(emsdevice->device_type()) && emsdevice->has_update())
|| (telegram->type_id == publish_id_ && telegram->dest == EMSbus::ems_bus_id())) {
if (telegram->type_id == publish_id_) {
publish_id_ = 0;
}
emsdevice->has_update(false); // reset flag
if (!Mqtt::publish_single()) {
publish_device_values(emsdevice->device_type()); // publish to MQTT if we explicitly have too
}
}
}
if (wait_validate_ == telegram->type_id) {
wait_validate_ = 0;
}
if (!found && telegram->message_length > 0) {
emsdevice->add_handlers_ignored(telegram->type_id);
}
break; break;
} else if (emsdevice->is_device_id(telegram->dest) && telegram->src != EMSbus::ems_bus_id()) {
emsdevice->handle_telegram(telegram);
} }
} }
if (!telegram_found) {
// handle unknown broadcasted telegrams // check for command to the device
if (!found && telegram->dest == 0) { for (const auto & emsdevice : emsdevices) {
if (emsdevice->is_device_id(telegram->dest) && telegram->src != EMSbus::ems_bus_id()) {
telegram_found = emsdevice->handle_telegram(telegram);
device_found = emsdevice->unique_id();
break;
}
}
}
if (!telegram_found) {
// check for sends to master thermostat
for (const auto & emsdevice : emsdevices) {
if (emsdevice->is_device_id(telegram->src) && telegram->dest == 0x10) {
telegram_found = emsdevice->handle_telegram(telegram);
device_found = emsdevice->unique_id();
break;
}
}
}
for (const auto & emsdevice : emsdevices) {
if (emsdevice->unique_id() == device_found) {
if (telegram->message_length > 0) {
emsdevice->add_handlers_ignored(telegram->type_id);
}
if (telegram->dest == 0 && telegram->offset == 0 && telegram->message_length > 0) {
emsdevice->add_handlers_broadcasted(telegram->type_id);
}
if (Mqtt::connected() && telegram_found
&& ((mqtt_.get_publish_onchange(emsdevice->device_type()) && emsdevice->has_update())
|| (telegram->type_id == publish_id_ && telegram->dest == EMSbus::ems_bus_id()))) {
if (telegram->type_id == publish_id_) {
publish_id_ = 0;
}
emsdevice->has_update(false); // reset flag
if (!Mqtt::publish_single()) {
publish_device_values(emsdevice->device_type()); // publish to MQTT if we explicitly have too
}
}
break;
}
}
// handle unknown broadcasted telegrams (or send to us)
if (!telegram_found && (telegram->dest == 0 || telegram->dest == EMSbus::ems_bus_id())) {
LOG_DEBUG("No telegram type handler found for ID 0x%02X (src 0x%02X)", telegram->type_id, telegram->src); LOG_DEBUG("No telegram type handler found for ID 0x%02X (src 0x%02X)", telegram->type_id, telegram->src);
if (watch() == WATCH_UNKNOWN) { if (watch() == WATCH_UNKNOWN) {
LOG_NOTICE("%s", pretty_telegram(telegram).c_str()); LOG_NOTICE("%s", pretty_telegram(telegram).c_str());
} }
if (!wait_km_ && !knowndevice && (telegram->src != EMSbus::ems_bus_id()) && (telegram->message_length > 0)) { if (!wait_km_ && !device_found && (telegram->src != EMSbus::ems_bus_id()) && (telegram->message_length > 0)) {
send_read_request(EMSdevice::EMS_TYPE_VERSION, telegram->src); send_read_request(EMSdevice::EMS_TYPE_VERSION, telegram->src);
} }
} }
return found; return telegram_found;
} }
// return true if we have this device already registered // return true if we have this device already registered