mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
tidy up how EMS bus line state is handled
This commit is contained in:
@@ -123,7 +123,7 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
|
||||
(
|
||||
<Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}>
|
||||
<Typography variant="body1">
|
||||
No EMS devices found. Check the connection and for possible Tx errors and try scanning for new devices.
|
||||
No EMS devices found. Check the connections and for possible Tx errors.
|
||||
</Typography>
|
||||
</Box>
|
||||
)
|
||||
@@ -205,24 +205,15 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
|
||||
const { width } = this.props;
|
||||
|
||||
if (this.noDevices()) {
|
||||
return (
|
||||
<p />
|
||||
)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!deviceData) {
|
||||
return (
|
||||
<Typography variant="h6">
|
||||
<p></p>
|
||||
Click on a device to show it's values
|
||||
</Typography>
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((deviceData.deviceData || []).length === 0) {
|
||||
return (
|
||||
<p />
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -151,7 +151,22 @@ uint8_t EMSESP::bus_status() {
|
||||
// show the EMS bus status plus both Rx and Tx queues
|
||||
void EMSESP::show_ems(uuid::console::Shell & shell) {
|
||||
// EMS bus information
|
||||
if (rxservice_.bus_connected()) {
|
||||
switch (bus_status()) {
|
||||
case BUS_STATUS_OFFLINE:
|
||||
shell.printfln(F("EMS Bus is disconnected."));
|
||||
break;
|
||||
case BUS_STATUS_TX_ERRORS:
|
||||
shell.printfln(F("EMS Bus is connected, but Tx is not stable."));
|
||||
break;
|
||||
case BUS_STATUS_CONNECTED:
|
||||
default:
|
||||
shell.printfln(F("EMS Bus is connected."));
|
||||
break;
|
||||
}
|
||||
|
||||
shell.println();
|
||||
|
||||
if (bus_status() != BUS_STATUS_OFFLINE) {
|
||||
uint8_t success_rate = 0;
|
||||
if (rxservice_.telegram_error_count()) {
|
||||
success_rate = ((float)rxservice_.telegram_error_count() / (float)rxservice_.telegram_count()) * 100;
|
||||
@@ -165,8 +180,6 @@ void EMSESP::show_ems(uuid::console::Shell & shell) {
|
||||
shell.printfln(F(" #write requests sent: %d"), txservice_.telegram_write_count());
|
||||
shell.printfln(F(" #corrupted telegrams: %d (%d%%)"), rxservice_.telegram_error_count(), success_rate);
|
||||
shell.printfln(F(" #tx fails (after %d retries): %d"), TxService::MAXIMUM_TX_RETRIES, txservice_.telegram_fail_count());
|
||||
} else {
|
||||
shell.printfln(F("EMS Bus is disconnected."));
|
||||
}
|
||||
|
||||
shell.println();
|
||||
@@ -647,14 +660,14 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
|
||||
}
|
||||
|
||||
// are we waiting for a response from a recent Tx Read or Write?
|
||||
uint8_t op = EMSbus::tx_waiting();
|
||||
if (op != Telegram::Operation::NONE) {
|
||||
uint8_t tx_state = EMSbus::tx_state();
|
||||
if (tx_state != Telegram::Operation::NONE) {
|
||||
bool tx_successful = false;
|
||||
EMSbus::tx_waiting(Telegram::Operation::NONE); // reset Tx wait state
|
||||
EMSbus::tx_state(Telegram::Operation::NONE); // reset Tx wait state
|
||||
// txservice_.print_last_tx();
|
||||
|
||||
// if we're waiting on a Write operation, we want a single byte 1 or 4
|
||||
if ((op == Telegram::Operation::TX_WRITE) && (length == 1)) {
|
||||
if ((tx_state == Telegram::Operation::TX_WRITE) && (length == 1)) {
|
||||
if (first_value == TxService::TX_WRITE_SUCCESS) {
|
||||
LOG_DEBUG(F("Last Tx write successful"));
|
||||
txservice_.increment_telegram_write_count(); // last tx/write was confirmed ok
|
||||
@@ -667,7 +680,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
|
||||
txservice_.send_poll(); // close the bus
|
||||
txservice_.reset_retry_count();
|
||||
}
|
||||
} else if (op == Telegram::Operation::TX_READ) {
|
||||
} else if (tx_state == Telegram::Operation::TX_READ) {
|
||||
// got a telegram with data in it. See if the src/dest matches that from the last one we sent and continue to process it
|
||||
uint8_t src = data[0];
|
||||
uint8_t dest = data[1];
|
||||
@@ -682,13 +695,15 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
|
||||
|
||||
// if Tx wasn't successful, retry or just give up
|
||||
if (!tx_successful) {
|
||||
txservice_.retry_tx(op, data, length);
|
||||
txservice_.retry_tx(tx_state, data, length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// check for poll
|
||||
if (length == 1) {
|
||||
EMSbus::last_bus_activity(uuid::get_uptime()); // set the flag indication the EMS bus is active
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
char s[4];
|
||||
if (first_value & 0x80) {
|
||||
@@ -702,7 +717,6 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
|
||||
// check for poll to us, if so send top message from Tx queue immediately and quit
|
||||
// if ht3 poll must be ems_bus_id else if Buderus poll must be (ems_bus_id | 0x80)
|
||||
if ((first_value ^ 0x80 ^ rxservice_.ems_mask()) == txservice_.ems_bus_id()) {
|
||||
EMSbus::last_bus_activity(uuid::get_uptime()); // set the flag indication the EMS bus is active
|
||||
txservice_.send();
|
||||
}
|
||||
// send remote room temperature if active
|
||||
@@ -764,7 +778,6 @@ void EMSESP::loop() {
|
||||
|
||||
system_.loop(); // does LED and checks system health, and syslog service
|
||||
rxservice_.loop(); // process what ever is in the rx queue
|
||||
txservice_.loop(); // check that the Tx is all ok
|
||||
shower_.loop(); // check for shower on/off
|
||||
sensors_.loop(); // this will also send out via MQTT
|
||||
mqtt_.loop(); // sends out anything in the queue via MQTT
|
||||
|
||||
@@ -42,8 +42,7 @@ uint32_t EMSbus::last_bus_activity_ = 0; // timestamp of last time
|
||||
bool EMSbus::bus_connected_ = false; // start assuming the bus hasn't been connected
|
||||
uint8_t EMSbus::ems_mask_ = EMS_MASK_UNSET; // unset so its triggered when booting, the its 0x00=buderus, 0x80=junker/ht3
|
||||
uint8_t EMSbus::ems_bus_id_ = EMSESP_DEFAULT_EMS_BUS_ID;
|
||||
uint8_t EMSbus::tx_waiting_ = Telegram::Operation::NONE;
|
||||
bool EMSbus::tx_active_ = false;
|
||||
uint8_t EMSbus::tx_state_ = Telegram::Operation::NONE;
|
||||
|
||||
uuid::log::Logger EMSbus::logger_{F_(logger_name), uuid::log::Facility::CONSOLE};
|
||||
|
||||
@@ -259,19 +258,6 @@ void TxService::start() {
|
||||
read_request(EMSdevice::EMS_TYPE_UBADevices, EMSdevice::EMS_DEVICE_ID_BOILER);
|
||||
}
|
||||
|
||||
// Tx loop
|
||||
// here we check if the Tx is not full and report an error
|
||||
void TxService::loop() {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
if ((uuid::get_uptime() - last_tx_check_) > TX_LOOP_WAIT) {
|
||||
last_tx_check_ = uuid::get_uptime();
|
||||
if (!tx_active() && (EMSbus::bus_connected())) {
|
||||
LOG_ERROR(F("Tx is not active. Please check settings and the circuit connection."));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// sends a 1 byte poll which is our own device ID
|
||||
void TxService::send_poll() {
|
||||
//LOG_DEBUG(F("Ack %02X"),ems_bus_id() ^ ems_mask());
|
||||
@@ -381,11 +367,11 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
|
||||
if (status == EMS_TX_STATUS_ERR) {
|
||||
LOG_ERROR(F("Failed to transmit Tx via UART."));
|
||||
increment_telegram_fail_count(); // another Tx fail
|
||||
tx_waiting(Telegram::Operation::NONE); // nothing send, tx not in wait state
|
||||
tx_state(Telegram::Operation::NONE); // nothing send, tx not in wait state
|
||||
return;
|
||||
}
|
||||
|
||||
tx_waiting(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
|
||||
@@ -399,7 +385,7 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) {
|
||||
}
|
||||
telegram_raw[length] = calculate_crc(telegram_raw, length); // apppend CRC
|
||||
|
||||
tx_waiting(Telegram::Operation::NONE); // no post validation needed
|
||||
tx_state(Telegram::Operation::NONE); // no post validation needed
|
||||
|
||||
// send the telegram to the UART Tx
|
||||
uint16_t status = EMSuart::transmit(telegram_raw, length);
|
||||
|
||||
@@ -173,23 +173,11 @@ class EMSbus {
|
||||
bus_connected_ = true;
|
||||
}
|
||||
|
||||
static bool tx_active() {
|
||||
return tx_active_;
|
||||
}
|
||||
static void tx_active(bool tx_active) {
|
||||
tx_active_ = tx_active;
|
||||
}
|
||||
|
||||
static uint8_t tx_waiting() {
|
||||
return tx_waiting_;
|
||||
}
|
||||
static void tx_waiting(uint8_t tx_waiting) {
|
||||
tx_waiting_ = tx_waiting;
|
||||
|
||||
// if NONE, then it's been reset which means we have an active Tx
|
||||
if ((tx_waiting == Telegram::Operation::NONE) && !(tx_active_)) {
|
||||
tx_active_ = true;
|
||||
static uint8_t tx_state() {
|
||||
return tx_state_;
|
||||
}
|
||||
static void tx_state(uint8_t tx_state) {
|
||||
tx_state_ = tx_state;
|
||||
}
|
||||
|
||||
static uint8_t calculate_crc(const uint8_t * data, const uint8_t length);
|
||||
@@ -201,8 +189,7 @@ class EMSbus {
|
||||
static bool bus_connected_; // start assuming the bus hasn't been connected
|
||||
static uint8_t ems_mask_; // unset=0xFF, buderus=0x00, junkers/ht3=0x80
|
||||
static uint8_t ems_bus_id_; // the bus id, which configurable and stored in settings
|
||||
static uint8_t tx_waiting_; // state of the Tx line (NONE or waiting on a TX_READ or TX_WRITE)
|
||||
static bool tx_active_; // is true is we have a working Tx connection
|
||||
static uint8_t tx_state_; // state of the Tx line (NONE or waiting on a TX_READ or TX_WRITE)
|
||||
};
|
||||
|
||||
class RxService : public EMSbus {
|
||||
@@ -269,7 +256,6 @@ class TxService : public EMSbus {
|
||||
~TxService() = default;
|
||||
|
||||
void start();
|
||||
void loop();
|
||||
void send();
|
||||
|
||||
void add(const uint8_t operation,
|
||||
@@ -364,7 +350,6 @@ class TxService : public EMSbus {
|
||||
private:
|
||||
uint8_t tx_telegram_id_ = 0; // queue counter
|
||||
|
||||
static constexpr uint32_t TX_LOOP_WAIT = 10000; // when to check if Tx is up and running (10 sec)
|
||||
uint32_t last_tx_check_ = 0;
|
||||
|
||||
std::deque<QueuedTxTelegram> tx_telegrams_;
|
||||
|
||||
Reference in New Issue
Block a user