Skip to content

Commit

Permalink
Merge pull request #61 from echavet/echavet/issue56
Browse files Browse the repository at this point in the history
correction to issue introduced while trying to correct log flood #56
  • Loading branch information
echavet authored Mar 13, 2024
2 parents c5888de + c3ce53a commit 61eee4e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
2 changes: 0 additions & 2 deletions components/cn105/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 26 additions & 7 deletions components/cn105/cn105.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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();

}
Expand All @@ -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_);
Expand Down
5 changes: 4 additions & 1 deletion components/cn105/cn105.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -245,18 +246,20 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR

unsigned long lastResponseMs;


uint32_t remote_temp_timeout_;
uint32_t debounce_delay_;

int baud_ = 0;
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;
Expand Down
22 changes: 13 additions & 9 deletions components/cn105/hp_writings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand All @@ -23,18 +24,21 @@ 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]() {
if (!this->isHeatpumpConnected_) {
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);
}
}

Expand Down Expand Up @@ -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...");
Expand All @@ -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); });
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 61eee4e

Please sign in to comment.