Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
store validateId in tx_telegram for multiple writes (wwtemp)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Mar 4, 2021
1 parent 7fe31bb commit 25a6641
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- fix mixerTemp and tankMiddleTemp (PR #714 @joanwa)
- icon update for pumps
- add mixer wiring naming from documentation
- wwtemp feedback and ems+ command

### Changed
- split `show values` in smaller packages and separate heating circuits
Expand Down
7 changes: 3 additions & 4 deletions src/emsesp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@ bool EMSESP::process_telegram(std::shared_ptr<const Telegram> 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->updated_values()) || telegram->type_id == publish_id_) {
if ((mqtt_.get_publish_onchange(emsdevice->device_type()) && emsdevice->updated_values())
|| (telegram->type_id == publish_id_ && telegram->dest == txservice_.ems_bus_id())) {
if (telegram->type_id == publish_id_) {
publish_id_ = 0;
}
Expand Down Expand Up @@ -878,9 +879,7 @@ void EMSESP::send_write_request(const uint16_t type_id,
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) {
Expand Down
25 changes: 14 additions & 11 deletions src/telegram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,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);

Expand Down Expand Up @@ -411,6 +412,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<Telegram>(operation, ems_bus_id(), dest, type_id, offset, message_data, message_length);

Expand All @@ -424,17 +426,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
// 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;
Expand All @@ -445,6 +447,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
Expand Down Expand Up @@ -480,7 +483,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);
}
Expand All @@ -497,9 +500,9 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt
#endif

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, validate_id); // 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, validate_id); // add to back of queue
}
}

Expand All @@ -508,7 +511,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
Expand Down Expand Up @@ -549,7 +552,7 @@ void TxService::send_raw(const char * telegram_data) {
return; // nothing to send
}

add(Telegram::Operation::TX_RAW, data, count + 1, true); // add to front of Tx queue
add(Telegram::Operation::TX_RAW, data, count + 1, 0, true); // add to front of Tx queue
}

// add last Tx to tx queue and increment count
Expand Down Expand Up @@ -579,13 +582,13 @@ 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());
}

uint16_t TxService::read_next_tx() {
// add to the top 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;
}

Expand All @@ -608,7 +611,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);
this->add(Telegram::Operation::TX_READ, dest, post_typeid, offset, message_data, 1, 0, true);
LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), post_typeid, dest);
set_post_send_query(0); // reset
// delay the request if we have a different type_id for post_send_query
Expand Down
9 changes: 6 additions & 3 deletions src/telegram.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ class TxService : public EMSbus {
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 send_raw(const char * telegram_data);
void send_poll();
Expand Down Expand Up @@ -349,12 +350,14 @@ class TxService : public EMSbus {
const uint16_t id_;
const std::shared_ptr<const Telegram> telegram_;
const bool retry_; // is a retry
const uint16_t validateid_;

~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)
, telegram_(std::move(telegram))
, retry_(retry) {
, retry_(retry)
, validateid_(validateid) {
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,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");
Expand All @@ -631,7 +631,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);
Expand All @@ -654,7 +654,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();

Expand Down

0 comments on commit 25a6641

Please sign in to comment.