store validate_Id with tx-Telegram

This commit is contained in:
MichaelDvP
2021-03-04 16:12:53 +01:00
parent 300fff1909
commit 7299be15df
4 changed files with 34 additions and 59 deletions

View File

@@ -364,17 +364,13 @@ void EMSESP::publish_all(bool force) {
// on command "publish HA" loop and wait between devices for publishing all sensors // on command "publish HA" loop and wait between devices for publishing all sensors
void EMSESP::publish_all_loop() { void EMSESP::publish_all_loop() {
static uint32_t last = 0;
if (!Mqtt::connected() || !publish_all_idx_) { if (!Mqtt::connected() || !publish_all_idx_) {
return; return;
} }
// wait for free queue before sending next message, v3 queues HA-messages
// every HA-sensor takes 20 ms, wait ~2 sec to finish (boiler has ~70 sensors) if (!Mqtt::is_empty()) {
if ((uuid::get_uptime() - last < 2000)) {
return; return;
} }
last = uuid::get_uptime();
switch (publish_all_idx_++) { switch (publish_all_idx_++) {
case 1: case 1:
publish_device_values(EMSdevice::DeviceType::BOILER); publish_device_values(EMSdevice::DeviceType::BOILER);
@@ -400,7 +396,6 @@ void EMSESP::publish_all_loop() {
default: default:
// all finished // all finished
publish_all_idx_ = 0; publish_all_idx_ = 0;
last = 0;
} }
} }
@@ -727,7 +722,8 @@ bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) {
found = emsdevice->handle_telegram(telegram); found = emsdevice->handle_telegram(telegram);
// if we correctly processes the telegram follow up with sending it via MQTT if needed // if we correctly processes the telegram follow up with sending it via MQTT if needed
if (found && Mqtt::connected()) { 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_) { if (telegram->type_id == publish_id_) {
publish_id_ = 0; publish_id_ = 0;
} }
@@ -911,9 +907,7 @@ void EMSESP::send_read_request(const uint16_t type_id, const uint8_t dest) {
// sends write request // 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) { 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_.add(Telegram::Operation::TX_WRITE, dest, type_id, offset, message_data, message_length, validate_typeid, true);
txservice_.set_post_send_query(validate_typeid); // store which type_id to send Tx read after a write
} }
void EMSESP::send_write_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, const uint8_t value) { void EMSESP::send_write_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, const uint8_t value) {

View File

@@ -376,6 +376,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
tx_telegram.id_, tx_telegram.id_,
Helpers::data_to_hex(telegram_raw, length).c_str()); Helpers::data_to_hex(telegram_raw, length).c_str());
set_post_send_query(tx_telegram.validateid_);
// send the telegram to the UART Tx // send the telegram to the UART Tx
uint16_t status = EMSuart::transmit(telegram_raw, length); 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 tx_state(telegram->operation); // tx now in a wait state
} }
/*
// send an array of bytes as a telegram // send an array of bytes as a telegram
// we need to calculate the CRC and append it before sending // 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 // 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 increment_telegram_fail_count(); // another Tx fail
} }
} }
*/
void TxService::add(const uint8_t operation, void TxService::add(const uint8_t operation,
const uint8_t dest, const uint8_t dest,
@@ -417,6 +420,7 @@ void TxService::add(const uint8_t operation,
const uint8_t offset, const uint8_t offset,
uint8_t * message_data, uint8_t * message_data,
const uint8_t message_length, const uint8_t message_length,
const uint16_t validateid,
const bool front) { const bool front) {
auto telegram = std::make_shared<Telegram>(operation, ems_bus_id(), dest, type_id, offset, message_data, message_length); auto telegram = std::make_shared<Telegram>(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) { 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 { } 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<Telegram>(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 // 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 // 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) // format is EMS 1.0 (src, dest, type_id, offset, data)
// length is the length of the whole telegram data, excluding the CRC // 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 // check length
if (length < 5) { if (length < 5) {
return; 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 dest = data[1];
uint8_t offset = data[3]; uint8_t offset = data[3];
uint16_t validate_id = validateid;
uint16_t type_id; uint16_t type_id;
const uint8_t * message_data; // where the message block starts const uint8_t * message_data; // where the message block starts
uint8_t message_length; // length of the message block, excluding CRC 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; operation = Telegram::Operation::TX_READ;
} else { } else {
operation = Telegram::Operation::TX_WRITE; operation = Telegram::Operation::TX_WRITE;
set_post_send_query(type_id); validate_id = type_id;
} }
EMSESP::set_read_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) { if (front) {
// tx_telegrams_.push_front(qtxt); // add to front of queue // 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 { } else {
// tx_telegrams_.push_back(qtxt); // add to back of queue // 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); 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 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 // 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 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 // 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_.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; 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() { uint16_t TxService::read_next_tx() {
// add to the top/front of the queue // add to the top/front of the queue
uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes 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; 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, ... // 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 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 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 // 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); LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), post_typeid, dest);
set_post_send_query(0); // reset set_post_send_query(0); // reset

View File

@@ -271,8 +271,8 @@ class TxService : public EMSbus {
void start(); void start();
void send(); 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 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 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 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_raw(const char * telegram_data);
void send_poll(); void send_poll();
@@ -321,7 +321,7 @@ class TxService : public EMSbus {
if (telegram_fail_count_ == 0) { if (telegram_fail_count_ == 0) {
return 100; // all good, 100% 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() { void increment_telegram_fail_count() {
@@ -344,12 +344,14 @@ class TxService : public EMSbus {
const uint16_t id_; const uint16_t id_;
const std::shared_ptr<const Telegram> telegram_; const std::shared_ptr<const Telegram> telegram_;
const bool retry_; // true if its a retry const bool retry_; // true if its a retry
const uint16_t validateid_;
~QueuedTxTelegram() = default; ~QueuedTxTelegram() = default;
QueuedTxTelegram(uint16_t id, std::shared_ptr<Telegram> && telegram, bool retry) QueuedTxTelegram(uint16_t id, std::shared_ptr<Telegram> && telegram, bool retry, uint16_t validateid)
: id_(id) : id_(id)
, telegram_(std::move(telegram)) , 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 uint8_t tx_telegram_id_ = 0; // queue counter
void send_telegram(const QueuedTxTelegram & tx_telegram); 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 } // namespace emsesp

View File

@@ -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) // TX queue example - Me -> Thermostat, (0x91), telegram: 0B 17 91 05 44 45 46 47 (#data=4)
uint8_t t11[] = {0x44, 0x45, 0x46, 0x47}; 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 // TX - raw example test
uint8_t t12[] = {0x10, 0x08, 0x63, 0x04, 0x64}; 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 // TX - sending raw string
EMSESP::txservice_.send_raw("10 08 63 03 64 65 66"); 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+ // 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}; 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 // EMS+ Junkers read request
EMSESP::send_read_request(0x16F, 0x10); 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 // 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) // 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::send_read_request(0x91, 0x17);
// EMSESP::txservice_.show_tx_queue(); // EMSESP::txservice_.show_tx_queue();