From 7299be15df84af842ab75ad01b369afaf3f77498 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Thu, 4 Mar 2021 16:12:53 +0100 Subject: [PATCH] store validate_Id with tx-Telegram --- src/emsesp.cpp | 16 +++++--------- src/telegram.cpp | 54 ++++++++++++++--------------------------------- src/telegram.h | 15 +++++++------ src/test/test.cpp | 8 +++---- 4 files changed, 34 insertions(+), 59 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 778d41df9..99435ee5a 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -364,17 +364,13 @@ void EMSESP::publish_all(bool force) { // on command "publish HA" loop and wait between devices for publishing all sensors void EMSESP::publish_all_loop() { - static uint32_t last = 0; if (!Mqtt::connected() || !publish_all_idx_) { return; } - - // every HA-sensor takes 20 ms, wait ~2 sec to finish (boiler has ~70 sensors) - if ((uuid::get_uptime() - last < 2000)) { + // wait for free queue before sending next message, v3 queues HA-messages + if (!Mqtt::is_empty()) { return; } - - last = uuid::get_uptime(); switch (publish_all_idx_++) { case 1: publish_device_values(EMSdevice::DeviceType::BOILER); @@ -400,7 +396,6 @@ void EMSESP::publish_all_loop() { default: // all finished publish_all_idx_ = 0; - last = 0; } } @@ -727,7 +722,8 @@ bool EMSESP::process_telegram(std::shared_ptr telegram) { found = emsdevice->handle_telegram(telegram); // if we correctly processes the telegram follow up with sending it via MQTT if needed if (found && Mqtt::connected()) { - if ((mqtt_.get_publish_onchange(emsdevice->device_type()) && emsdevice->has_update()) || telegram->type_id == publish_id_) { + if ((mqtt_.get_publish_onchange(emsdevice->device_type()) && emsdevice->has_update()) + || (telegram->type_id == publish_id_ && telegram->dest == txservice_.ems_bus_id())) { if (telegram->type_id == publish_id_) { publish_id_ = 0; } @@ -911,9 +907,7 @@ void EMSESP::send_read_request(const uint16_t type_id, const uint8_t dest) { // sends write request void EMSESP::send_write_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, uint8_t * message_data, const uint8_t message_length, const uint16_t validate_typeid) { - txservice_.add(Telegram::Operation::TX_WRITE, dest, type_id, offset, message_data, message_length, true); - - txservice_.set_post_send_query(validate_typeid); // store which type_id to send Tx read after a write + txservice_.add(Telegram::Operation::TX_WRITE, dest, type_id, offset, message_data, message_length, validate_typeid, true); } void EMSESP::send_write_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, const uint8_t value) { diff --git a/src/telegram.cpp b/src/telegram.cpp index df9b848e7..6a6fa1645 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -376,6 +376,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { tx_telegram.id_, Helpers::data_to_hex(telegram_raw, length).c_str()); + set_post_send_query(tx_telegram.validateid_); // send the telegram to the UART Tx uint16_t status = EMSuart::transmit(telegram_raw, length); @@ -389,6 +390,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { tx_state(telegram->operation); // tx now in a wait state } +/* // send an array of bytes as a telegram // we need to calculate the CRC and append it before sending // this function is fire-and-forget. there are no checks or post-send validations @@ -410,6 +412,7 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) { increment_telegram_fail_count(); // another Tx fail } } +*/ void TxService::add(const uint8_t operation, const uint8_t dest, @@ -417,6 +420,7 @@ void TxService::add(const uint8_t operation, const uint8_t offset, uint8_t * message_data, const uint8_t message_length, + const uint16_t validateid, const bool front) { auto telegram = std::make_shared(operation, ems_bus_id(), dest, type_id, offset, message_data, message_length); @@ -430,44 +434,17 @@ void TxService::add(const uint8_t operation, } if (front) { - tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram), false); // add to front of queue + tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram), false, validateid); // add to front of queue } else { - tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false); // add to back of queue + tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false, validateid); // add to back of queue } } -/* -// builds a Tx telegram and adds to queue -// given some details like the destination, type, offset and message block -void TxService::add(const uint8_t operation, - const uint8_t dest, - const uint16_t type_id, - const uint8_t offset, - uint8_t * message_data, - const uint8_t message_length, - const bool front) { -#ifdef EMSESP_DEBUG - LOG_DEBUG(F("[DEBUG] New Tx [#%d] telegram, length %d"), tx_telegram_id_, message_length); -#endif - - QueuedTxTelegram qtxt; - qtxt.id_ = tx_telegram_id_++; - qtxt.retry_ = false; - qtxt.telegram_ = std::make_shared(operation, ems_bus_id(), dest, type_id, offset, message_data, message_length); - - if (front) { - tx_telegrams_.push_front(qtxt); // add to front of queue - } else { - tx_telegrams_.push_back(qtxt); // add to back of queue - } -} -*/ - // builds a Tx telegram and adds to queue // this is used by the retry() function to put the last failed Tx back into the queue // format is EMS 1.0 (src, dest, type_id, offset, data) // length is the length of the whole telegram data, excluding the CRC -void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t length, const bool front) { +void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t length, const uint16_t validateid, const bool front) { // check length if (length < 5) { return; @@ -478,6 +455,7 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt uint8_t dest = data[1]; uint8_t offset = data[3]; + uint16_t validate_id = validateid; uint16_t type_id; const uint8_t * message_data; // where the message block starts uint8_t message_length; // length of the message block, excluding CRC @@ -513,7 +491,7 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt operation = Telegram::Operation::TX_READ; } else { operation = Telegram::Operation::TX_WRITE; - set_post_send_query(type_id); + validate_id = type_id; } EMSESP::set_read_id(type_id); } @@ -538,10 +516,10 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt if (front) { // tx_telegrams_.push_front(qtxt); // add to front of queue - tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram), false); // add to front of queue + tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram), false, validate_id); // add to front of queue } else { // tx_telegrams_.push_back(qtxt); // add to back of queue - tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false); // add to back of queue + tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false, validate_id); // add to back of queue } } @@ -550,7 +528,7 @@ void TxService::read_request(const uint16_t type_id, const uint8_t dest, const u LOG_DEBUG(F("Tx read request to device 0x%02X for type ID 0x%02X"), dest, type_id); uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes - add(Telegram::Operation::TX_READ, dest, type_id, offset, message_data, 1); + add(Telegram::Operation::TX_READ, dest, type_id, offset, message_data, 1, 0); } // Send a raw telegram to the bus, telegram is a text string of hex values @@ -591,7 +569,7 @@ void TxService::send_raw(const char * telegram_data) { return; // nothing to send } - add(Telegram::Operation::TX_RAW, data, count + 1, true); // add to top/front of Tx queue + add(Telegram::Operation::TX_RAW, data, count + 1, 0, true); // add to top/front of Tx queue } // add last Tx to tx queue and increment count @@ -621,7 +599,7 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui tx_telegrams_.pop_back(); } - tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram_last_), true); + tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram_last_), true, get_post_send_query()); /* QueuedTxTelegram qtxt; @@ -635,7 +613,7 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui uint16_t TxService::read_next_tx() { // add to the top/front of the queue uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes - add(Telegram::Operation::TX_READ, telegram_last_->dest, telegram_last_->type_id, telegram_last_->offset + 25, message_data, 1, true); + add(Telegram::Operation::TX_READ, telegram_last_->dest, telegram_last_->type_id, telegram_last_->offset + 25, message_data, 1, 0, true); return telegram_last_->type_id; } @@ -658,7 +636,7 @@ uint16_t TxService::post_send_query() { // when set a value with large offset before and validate on same type, we have to add offset 0, 26, 52, ... uint8_t offset = (this->telegram_last_->type_id == post_typeid) ? ((this->telegram_last_->offset / 26) * 26) : 0; uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes - this->add(Telegram::Operation::TX_READ, dest, post_typeid, offset, message_data, 1, true); // add to top/front of queue + this->add(Telegram::Operation::TX_READ, dest, post_typeid, offset, message_data, 1, 0, true); // add to top/front of queue // read_request(telegram_last_post_send_query_, dest, 0); // no offset LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), post_typeid, dest); set_post_send_query(0); // reset diff --git a/src/telegram.h b/src/telegram.h index 716dd50a8..4ad3d13ab 100644 --- a/src/telegram.h +++ b/src/telegram.h @@ -271,8 +271,8 @@ class TxService : public EMSbus { void start(); void send(); - void add(const uint8_t operation, const uint8_t dest, const uint16_t type_id, const uint8_t offset, uint8_t * message_data, const uint8_t message_length, const bool front = false); - void add(const uint8_t operation, const uint8_t * data, const uint8_t length, const bool front = false); + void add(const uint8_t operation, const uint8_t dest, const uint16_t type_id, const uint8_t offset, uint8_t * message_data, const uint8_t message_length, const uint16_t validateid, const bool front = false); + void add(const uint8_t operation, const uint8_t * data, const uint8_t length, const uint16_t validateid, const bool front = false); void read_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset = 0); void send_raw(const char * telegram_data); void send_poll(); @@ -321,7 +321,7 @@ class TxService : public EMSbus { if (telegram_fail_count_ == 0) { return 100; // all good, 100% } - return (100 - (((float)telegram_fail_count_ / telegram_read_count_ * 100))); + return (100 - (uint8_t)(((float)telegram_fail_count_ / telegram_read_count_ * 100))); } void increment_telegram_fail_count() { @@ -344,12 +344,14 @@ class TxService : public EMSbus { const uint16_t id_; const std::shared_ptr telegram_; const bool retry_; // true if its a retry + const uint16_t validateid_; ~QueuedTxTelegram() = default; - QueuedTxTelegram(uint16_t id, std::shared_ptr && telegram, bool retry) + QueuedTxTelegram(uint16_t id, std::shared_ptr && telegram, bool retry, uint16_t validateid) : id_(id) , telegram_(std::move(telegram)) - , retry_(retry) { + , retry_(retry) + , validateid_(validateid) { } }; @@ -391,8 +393,9 @@ class TxService : public EMSbus { uint8_t tx_telegram_id_ = 0; // queue counter + void send_telegram(const QueuedTxTelegram & tx_telegram); - void send_telegram(const uint8_t * data, const uint8_t length); + // void send_telegram(const uint8_t * data, const uint8_t length); }; } // namespace emsesp diff --git a/src/test/test.cpp b/src/test/test.cpp index e91d17916..083d8001d 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -622,11 +622,11 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) { // TX queue example - Me -> Thermostat, (0x91), telegram: 0B 17 91 05 44 45 46 47 (#data=4) uint8_t t11[] = {0x44, 0x45, 0x46, 0x47}; - EMSESP::txservice_.add(Telegram::Operation::TX_RAW, 0x17, 0x91, 0x05, t11, sizeof(t11)); + EMSESP::txservice_.add(Telegram::Operation::TX_RAW, 0x17, 0x91, 0x05, t11, sizeof(t11), 0); // TX - raw example test uint8_t t12[] = {0x10, 0x08, 0x63, 0x04, 0x64}; - EMSESP::txservice_.add(Telegram::Operation::TX_RAW, t12, sizeof(t12)); + EMSESP::txservice_.add(Telegram::Operation::TX_RAW, t12, sizeof(t12), 0); // TX - sending raw string EMSESP::txservice_.send_raw("10 08 63 03 64 65 66"); @@ -640,7 +640,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) { // TX - send EMS+ const uint8_t t13[] = {0x90, 0x0B, 0xFF, 00, 01, 0xBA, 00, 0x2E, 0x2A, 0x26, 0x1E, 0x03, 00, 0xFF, 0xFF, 05, 0x2A, 01, 0xE1, 0x20, 0x01, 0x0F, 05, 0x2A}; - EMSESP::txservice_.add(Telegram::Operation::TX_RAW, t13, sizeof(t13)); + EMSESP::txservice_.add(Telegram::Operation::TX_RAW, t13, sizeof(t13), 0); // EMS+ Junkers read request EMSESP::send_read_request(0x16F, 0x10); @@ -658,7 +658,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) { // simulate sending a read request // uint8_t t16[] = {0x44, 0x45, 0x46, 0x47}; // Me -> Thermostat, (0x91), telegram: 0B 17 91 05 44 45 46 47 (#data=4) - // EMSESP::txservice_.add(Telegram::Operation::TX_RAW, 0x17, 0x91, 0x05, t16, sizeof(t16)); + // EMSESP::txservice_.add(Telegram::Operation::TX_RAW, 0x17, 0x91, 0x05, t16, sizeof(t16), 0); EMSESP::send_read_request(0x91, 0x17); // EMSESP::txservice_.show_tx_queue();