fix retry bug

This commit is contained in:
proddy
2020-06-20 11:27:31 +02:00
parent ea9b913285
commit 04503298ab
3 changed files with 28 additions and 27 deletions

View File

@@ -60,7 +60,7 @@ Shower EMSESP::shower_; // Shower logic
// static/common variables // static/common variables
uint8_t EMSESP::actual_master_thermostat_ = EMSESP_DEFAULT_MASTER_THERMOSTAT; // which thermostat leads when multiple found uint8_t EMSESP::actual_master_thermostat_ = EMSESP_DEFAULT_MASTER_THERMOSTAT; // which thermostat leads when multiple found
uint16_t EMSESP::watch_id_ = WATCH_NONE; // for when log is TRACE. 0 means no trace set uint16_t EMSESP::watch_id_ = WATCH_ID_NONE; // for when log is TRACE. 0 means no trace set
uint8_t EMSESP::watch_ = 0; // trace off uint8_t EMSESP::watch_ = 0; // trace off
bool EMSESP::tap_water_active_ = false; // for when Boiler states we having running warm water. used in Shower() bool EMSESP::tap_water_active_ = false; // for when Boiler states we having running warm water. used in Shower()
bool EMSESP::ems_read_only_; bool EMSESP::ems_read_only_;
@@ -398,8 +398,8 @@ void EMSESP::process_version(std::shared_ptr<const Telegram> telegram) {
// returns false if there are none found // returns false if there are none found
bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) { bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) {
// if watching... // if watching...
if (watch() == 1) { if (watch() == WATCH_ON) {
if ((watch_id_ == WATCH_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) { if ((watch_id_ == WATCH_ID_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) {
LOG_NOTICE(pretty_telegram(telegram).c_str()); LOG_NOTICE(pretty_telegram(telegram).c_str());
} }
} }
@@ -570,6 +570,7 @@ void EMSESP::send_write_request(const uint16_t type_id,
// this is main entry point when data is received on the Rx line, via emsuart library // this is main entry point when data is received on the Rx line, via emsuart library
// we check if its a complete telegram or just a single byte (which could be a poll or a return status) // we check if its a complete telegram or just a single byte (which could be a poll or a return status)
// the CRC check is not done here, only when it's added to the Rx queue with add()
void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
static uint32_t tx_time_ = 0; static uint32_t tx_time_ = 0;
// check first for echo // check first for echo
@@ -641,6 +642,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
// check if there is a message for the roomcontroller // check if there is a message for the roomcontroller
Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data); Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data);
// add to RxQueue, what ever it is. // add to RxQueue, what ever it is.
// in add() the CRC will be checked
rxservice_.add(data, length); rxservice_.add(data, length);
} }
} }
@@ -828,11 +830,11 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) {
[](Shell & shell, const std::vector<std::string> & arguments) { [](Shell & shell, const std::vector<std::string> & arguments) {
// get raw/pretty // get raw/pretty
if (arguments[0] == read_flash_string(F_(raw))) { if (arguments[0] == read_flash_string(F_(raw))) {
emsesp::EMSESP::watch(2); // raw emsesp::EMSESP::watch(WATCH_RAW); // raw
} else if (arguments[0] == read_flash_string(F_(on))) { } else if (arguments[0] == read_flash_string(F_(on))) {
emsesp::EMSESP::watch(1); // on emsesp::EMSESP::watch(WATCH_ON); // on
} else if (arguments[0] == read_flash_string(F_(off))) { } else if (arguments[0] == read_flash_string(F_(off))) {
emsesp::EMSESP::watch(0); // off emsesp::EMSESP::watch(WATCH_OFF); // off
} else { } else {
shell.printfln(F_(invalid_watch)); shell.printfln(F_(invalid_watch));
return; return;
@@ -843,13 +845,13 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) {
// get the watch_id if its set // get the watch_id if its set
watch_id = Helpers::hextoint(arguments[1].c_str()); watch_id = Helpers::hextoint(arguments[1].c_str());
} else { } else {
watch_id = WATCH_NONE; watch_id = WATCH_ID_NONE;
} }
emsesp::EMSESP::watch_id(watch_id); emsesp::EMSESP::watch_id(watch_id);
uint8_t watch = emsesp::EMSESP::watch(); uint8_t watch = emsesp::EMSESP::watch();
if (watch == 0) { if (watch == WATCH_OFF) {
shell.printfln(F("Watch is off")); shell.printfln(F("Watch is off"));
return; return;
} }
@@ -859,14 +861,14 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) {
shell.log_level(Level::NOTICE); shell.log_level(Level::NOTICE);
} }
if (watch == 1) { if (watch == WATCH_ON) {
shell.printfln(F("Watching incoming telegrams, displayed in decoded format")); shell.printfln(F("Watching incoming telegrams, displayed in decoded format"));
} else { } else {
shell.printfln(F("Watching incoming telegrams, displayed as raw bytes")); shell.printfln(F("Watching incoming telegrams, displayed as raw bytes")); // WATCH_RAW
} }
watch_id = emsesp::EMSESP::watch_id(); watch_id = emsesp::EMSESP::watch_id();
if (watch_id != WATCH_NONE) { if (watch_id != WATCH_ID_NONE) {
shell.printfln(F("Filtering only telegrams that match a device ID or telegram type of 0x%02X"), watch_id); shell.printfln(F("Filtering only telegrams that match a device ID or telegram type of 0x%02X"), watch_id);
} }
}); });

View File

@@ -46,7 +46,7 @@
#include "devices/boiler.h" #include "devices/boiler.h"
#define WATCH_NONE 0 // no watch id set #define WATCH_ID_NONE 0 // no watch id set
namespace emsesp { namespace emsesp {
@@ -109,8 +109,9 @@ class EMSESP {
watch_ = watch; // 0=off, 1=on, 2=raw watch_ = watch; // 0=off, 1=on, 2=raw
} }
enum Watch : uint8_t { WATCH_OFF, WATCH_ON, WATCH_RAW };
static uint8_t watch() { static uint8_t watch() {
return watch_; // 0=off, 1=on, 2=raw return watch_;
} }
static bool tap_water_active() { static bool tap_water_active() {

View File

@@ -246,8 +246,8 @@ void RxService::add(uint8_t * data, uint8_t length) {
// validate the CRC // validate the CRC
uint8_t crc = calculate_crc(data, length - 1); uint8_t crc = calculate_crc(data, length - 1);
if ((data[length - 1] != crc) && (EMSESP::watch() != 0)) { if ((data[length - 1] != crc) && (EMSESP::watch() != EMSESP::Watch::WATCH_OFF)) {
LOG_ERROR(F("Rx: %s %s(BAD, CRC %02X != %02X)%s"), Helpers::data_to_hex(data, length).c_str(), COLOR_RED, data[length - 1], crc, COLOR_RESET); LOG_ERROR(F("Rx: %s %s(CRC %02X != %02X)%s"), Helpers::data_to_hex(data, length).c_str(), COLOR_RED, data[length - 1], crc, COLOR_RESET);
increment_telegram_error_count(); increment_telegram_error_count();
return; return;
} }
@@ -296,9 +296,9 @@ void RxService::add(uint8_t * data, uint8_t length) {
} }
// if we're watching and "raw" print out actual telegram as bytes to the console // if we're watching and "raw" print out actual telegram as bytes to the console
if (EMSESP::watch() == 2) { if (EMSESP::watch() == EMSESP::Watch::WATCH_RAW) {
uint16_t trace_watch_id = EMSESP::watch_id(); uint16_t trace_watch_id = EMSESP::watch_id();
if ((trace_watch_id == WATCH_NONE) || (src == trace_watch_id) || (dest == trace_watch_id) || (type_id == trace_watch_id)) { if ((trace_watch_id == WATCH_ID_NONE) || (src == trace_watch_id) || (dest == trace_watch_id) || (type_id == trace_watch_id)) {
LOG_NOTICE(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); LOG_NOTICE(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str());
} }
} }
@@ -465,7 +465,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
#ifdef EMSESP_DEBUG #ifdef EMSESP_DEBUG
// if watching in 'raw' mode // if watching in 'raw' mode
if (EMSESP::watch() == 2) { if (EMSESP::watch() == EMSESP::Watch::WATCH_RAW) {
LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str());
} }
#endif #endif
@@ -624,26 +624,24 @@ void TxService::send_raw(const char * telegram_data) {
// add last Tx to tx queue and increment count // add last Tx to tx queue and increment count
// returns retry count, or 0 if all done // returns retry count, or 0 if all done
void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const uint8_t length) { void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const uint8_t length) {
retry_count_++;
// have we reached the limit? if so, reset count and give up // have we reached the limit? if so, reset count and give up
if (retry_count_ > MAXIMUM_TX_RETRIES) { if (++retry_count_ > MAXIMUM_TX_RETRIES) {
reset_retry_count(); // give up reset_retry_count(); // give up
increment_telegram_fail_count(); // another Tx fail increment_telegram_fail_count(); // another Tx fail
LOG_ERROR(F("Last Tx %s operation failed after %d retries. Ignoring request."), LOG_ERROR(F("Last Tx %s operation failed after %d retries. Ignoring request."),
(operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"), (operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"),
MAXIMUM_TX_RETRIES); MAXIMUM_TX_RETRIES);
} else {
LOG_DEBUG(F("[DEBUG] Last Tx %s operation failed. Retry #%d. sent message data: %s, received: %s"),
(operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"),
retry_count_,
telegram_last_->to_string().c_str(),
Helpers::data_to_hex(data, length).c_str());
return; return;
} }
LOG_DEBUG(F("[DEBUG] Last Tx %s operation failed. Retry #%d. sent message data: %s, received: %s"),
(operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"),
retry_count_,
telegram_last_->to_string().c_str(),
Helpers::data_to_hex(data, length).c_str());
// add to the top of the queue // add to the top of the queue
if (tx_telegrams_.size() >= MAX_TX_TELEGRAMS) { if (tx_telegrams_.size() >= MAX_TX_TELEGRAMS) {
tx_telegrams_.pop_back(); tx_telegrams_.pop_back();