From c3ce53a433c1dc78ce177d366ba472ff4f82463e Mon Sep 17 00:00:00 2001 From: Eric Chavet Date: Wed, 13 Mar 2024 14:36:46 +0100 Subject: [PATCH] correction to issu introduced while trying to correct log flood #56 --- components/cn105/Globals.h | 2 -- components/cn105/cn105.cpp | 33 +++++++++++++++++++++++++------- components/cn105/cn105.h | 5 ++++- components/cn105/hp_writings.cpp | 22 ++++++++++++--------- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/components/cn105/Globals.h b/components/cn105/Globals.h index 919650e..0183c63 100644 --- a/components/cn105/Globals.h +++ b/components/cn105/Globals.h @@ -22,8 +22,6 @@ static const char* LOG_CYCLE_TAG = "CYCLE"; // loop cycles logs static const char* LOG_UPD_INT_TAG = "UPDT_ITVL"; // update interval logging -static const char* SHEDULER_INTERVAL_SYNC_NAME = "hp->sync"; // name of the scheduler to prpgram hp updates -static const char* DEFER_SHEDULER_INTERVAL_SYNC_NAME = "hp->sync_defer"; // name of the scheduler to prpgram hp updates static const char* SHEDULER_REMOTE_TEMP_TIMEOUT = "->remote_temp_timeout"; // defering delay for update_interval when we've just sent a wentedSettings diff --git a/components/cn105/cn105.cpp b/components/cn105/cn105.cpp index 4b6527d..33fd5d2 100644 --- a/components/cn105/cn105.cpp +++ b/components/cn105/cn105.cpp @@ -14,7 +14,8 @@ CN105Climate::CN105Climate(uart::UARTComponent* uart) : this->traits_.set_visual_max_temperature(ESPMHP_MAX_TEMPERATURE); this->traits_.set_visual_temperature_step(ESPMHP_TEMPERATURE_STEP); - this->isConnected_ = false; + + this->isUARTConnected_ = false; this->tempMode = false; this->wideVaneAdj = false; this->functions = heatpumpFunctions(); @@ -23,6 +24,7 @@ CN105Climate::CN105Climate(uart::UARTComponent* uart) : this->externalUpdate = false; this->lastSend = 0; this->infoMode = 0; + this->lastConnectRqTimeMs = 0; this->currentStatus.operating = false; this->currentStatus.compressorFrequency = -1; this->tx_pin_ = -1; @@ -91,7 +93,7 @@ void CN105Climate::setupUART() { ESP_LOGI(TAG, "setupUART() with baudrate %d", this->parent_->get_baud_rate()); this->isHeatpumpConnected_ = false; - this->isConnected_ = false; + this->isUARTConnected_ = false; // just for debugging purpose, a way to use a button i, yaml to trigger a reconnect this->uart_setup_switch = true; @@ -100,7 +102,7 @@ void CN105Climate::setupUART() { this->parent_->get_parity() == uart::UART_CONFIG_PARITY_EVEN && this->parent_->get_stop_bits() == 1) { ESP_LOGD("CustomComponent", "UART est configuré en SERIAL_8E1"); - this->isConnected_ = true; + this->isUARTConnected_ = true; this->initBytePointer(); } else { ESP_LOGW("CustomComponent", "UART n'est pas configuré en SERIAL_8E1"); @@ -114,9 +116,8 @@ void CN105Climate::disconnectUART() { this->uart_setup_switch = false; this->isHeatpumpConnected_ = false; - this->isConnected_ = false; + this->isUARTConnected_ = false; this->firstRun = true; - this->cancel_timeout(SHEDULER_INTERVAL_SYNC_NAME); this->publish_state(); } @@ -128,12 +129,30 @@ void CN105Climate::reconnectUART() { this->sendFirstConnectionPacket(); } + +void CN105Climate::reconnectIfConnectionLost() { + if (!this->isHeatpumpConnectionActive()) { + long connectTimeMs = CUSTOM_MILLIS - this->lastConnectRqTimeMs; + if (connectTimeMs > this->update_interval_) { + long lrTimeMs = CUSTOM_MILLIS - this->lastResponseMs; + ESP_LOGW(TAG, "Heatpump has not replied for %ld s", lrTimeMs / 1000); + ESP_LOGI(TAG, "We think Heatpump is not connected anymore.."); + this->disconnectUART(); + this->setupUART(); + this->sendFirstConnectionPacket(); + } + } +} + bool CN105Climate::isHeatpumpConnectionActive() { long lrTimeMs = CUSTOM_MILLIS - this->lastResponseMs; if (lrTimeMs > MAX_DELAY_RESPONSE_FACTOR * this->update_interval_) { - ESP_LOGW(TAG, "Heatpump has not replied for %ld s", lrTimeMs / 1000); - ESP_LOGI(TAG, "We think Heatpump is not connected anymore.."); + ESP_LOGV(TAG, "Heatpump has not replied for %ld s", lrTimeMs / 1000); + ESP_LOGV(TAG, "We think Heatpump is not connected anymore.."); + this->disconnectUART(); + // this->setupUART(); + // this->sendFirstConnectionPacket(); } return (lrTimeMs < MAX_DELAY_RESPONSE_FACTOR * this->update_interval_); diff --git a/components/cn105/cn105.h b/components/cn105/cn105.h index bc534b9..7590c2a 100644 --- a/components/cn105/cn105.h +++ b/components/cn105/cn105.h @@ -77,6 +77,7 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR void buildAndSendRequestsInfoPackets(); void buildAndSendRequestPacket(int packetType); bool isHeatpumpConnectionActive(); + void reconnectIfConnectionLost(); void sendWantedSettings(); void sendWantedSettingsDelegate(); @@ -245,6 +246,7 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR unsigned long lastResponseMs; + uint32_t remote_temp_timeout_; uint32_t debounce_delay_; @@ -252,11 +254,12 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR int tx_pin_ = -1; int rx_pin_ = -1; - bool isConnected_ = false; + bool isUARTConnected_ = false; bool isHeatpumpConnected_ = false; //HardwareSerial* _HardSerial{ nullptr }; unsigned long lastSend; + unsigned long lastConnectRqTimeMs; uint8_t storedInputData[MAX_DATA_BYTES]; // multi-byte data uint8_t* data; diff --git a/components/cn105/hp_writings.cpp b/components/cn105/hp_writings.cpp index 1b6b69e..8f54b36 100644 --- a/components/cn105/hp_writings.cpp +++ b/components/cn105/hp_writings.cpp @@ -12,7 +12,8 @@ uint8_t CN105Climate::checkSum(uint8_t bytes[], int len) { void CN105Climate::sendFirstConnectionPacket() { - if (this->isConnected_) { + if (this->isUARTConnected_) { + this->isHeatpumpConnected_ = false; ESP_LOGD(TAG, "Envoi du packet de connexion..."); @@ -23,6 +24,7 @@ void CN105Climate::sendFirstConnectionPacket() { this->writePacket(packet, CONNECT_LEN, false); // checkIsActive=false because it's the first packet and we don't have any reply yet this->lastSend = CUSTOM_MILLIS; + this->lastConnectRqTimeMs = CUSTOM_MILLIS; // we wait for a 10s timeout to check if the hp has replied to connection packet this->set_timeout("checkFirstConnection", 10000, [this]() { @@ -30,11 +32,13 @@ void CN105Climate::sendFirstConnectionPacket() { ESP_LOGE(TAG, "--> Heatpump did not reply: NOT CONNECTED <--"); ESP_LOGI(TAG, "Trying to connect again..."); this->sendFirstConnectionPacket(); - } - }); + }}); } else { - ESP_LOGE(TAG, "You should connect the heatpump through UART"); + ESP_LOGE(TAG, "UART doesn't seem to be connected..."); + this->setupUART(); + // this delay to prevent a logging flood should never happen + CUSTOM_DELAY(750); } } @@ -74,7 +78,7 @@ void CN105Climate::prepareSetPacket(uint8_t* packet, int length) { void CN105Climate::writePacket(uint8_t* packet, int length, bool checkIsActive) { - if ((this->isConnected_) && + if ((this->isUARTConnected_) && (this->isHeatpumpConnectionActive() || (!checkIsActive))) { ESP_LOGD(TAG, "writing packet..."); @@ -91,7 +95,7 @@ void CN105Climate::writePacket(uint8_t* packet, int length, bool checkIsActive) // this->sendFirstConnectionPacket(); ESP_LOGW(TAG, "delaying packet writing because we need to reconnect first..."); - this->set_timeout("write", 500, [this, packet, length]() { this->writePacket(packet, length); }); + this->set_timeout("write", 1000, [this, packet, length]() { this->writePacket(packet, length); }); } } @@ -267,12 +271,11 @@ void CN105Climate::sendWantedSettingsDelegate() { /** * builds and send all an update packet to the heatpump - * SHEDULER_INTERVAL_SYNC_NAME scheduler is canceled * * */ void CN105Climate::sendWantedSettings() { - if (this->isHeatpumpConnectionActive() && this->isConnected_) { + if (this->isHeatpumpConnectionActive() && this->isUARTConnected_) { if (CUSTOM_MILLIS - this->lastSend > 300) { // we don't want to send too many packets //this->cycleEnded(); // only if we let the cycle be interrupted to send wented settings @@ -305,11 +308,12 @@ void CN105Climate::buildAndSendRequestsInfoPackets() { if (this->isHeatpumpConnected_) { ESP_LOGV(LOG_UPD_INT_TAG, "triggering infopacket because of update interval tick"); ESP_LOGV("CONTROL_WANTED_SETTINGS", "hasChanged is %s", wantedSettings.hasChanged ? "true" : "false"); - ESP_LOGD(TAG, "sending a request for settings packet (0x02)"); this->loopCycle.cycleStarted(); ESP_LOGD(LOG_CYCLE_TAG, "2a: Sending settings request (0x02)"); this->buildAndSendRequestPacket(RQST_PKT_SETTINGS); + } else { + this->reconnectIfConnectionLost(); } }