Skip to content

Commit

Permalink
Merge pull request #37 from echavet/cycle_mgmt_refactor
Browse files Browse the repository at this point in the history
Cycle management refactor
  • Loading branch information
echavet authored Feb 21, 2024
2 parents ca73a83 + b39a506 commit e660d70
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
38 changes: 6 additions & 32 deletions components/cn105/cn105.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,38 +186,12 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR
void setWideVaneSetting(const char* setting);
void setFanSpeed(const char* setting);

bool isCycleRunning() {
return cycleRunning;
}

void deferCycle() {

#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
uint32_t delay = DEFER_SCHEDULE_UPDATE_LOOP_DELAY * 3;
#else
uint32_t delay = DEFER_SCHEDULE_UPDATE_LOOP_DELAY;
#endif

ESP_LOGI(LOG_CYCLE_TAG, "Defering cycle trigger of %d ms", delay);
// forces the lastCompleteCycle offset of delay ms to allow a longer rest time
this->lastCompleteCycle += delay;

}
void cycleStarted() {
ESP_LOGI(LOG_CYCLE_TAG, "1: Cycle start");
this->lastRequestInfo = CUSTOM_MILLIS;
cycleRunning = true;
}
void cycleEnded() {
ESP_LOGI(LOG_CYCLE_TAG, "6: Cycle ends");
cycleRunning = false;
// a complete cycle is done
this->lastCompleteCycle = CUSTOM_MILLIS;
}

bool hasUpdateIntervalPassed() {
return (CUSTOM_MILLIS - this->lastCompleteCycle) > this->update_interval_;
}
void cycleStarted();
void cycleEnded();
bool hasUpdateIntervalPassed();
bool didCycleTimeOut();
bool isCycleRunning();
void deferCycle();

private:
const char* lookupByteMapValue(const char* valuesMap[], const uint8_t byteMap[], int len, uint8_t byteValue, const char* debugInfo = "", const char* defaultValue = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion components/cn105/componentEntries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void CN105Climate::loop() {
this->buildAndSendRequestsInfoPackets();
}
} else {
if ((CUSTOM_MILLIS - this->lastRequestInfo) > (5 * this->update_interval_) + 4000) {
if (this->didCycleTimeOut()) {
ESP_LOGW(TAG, "Cycle timeout, reseting cycle...");
cycleRunning = false;
}
Expand Down
39 changes: 39 additions & 0 deletions components/cn105/cycle_management.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "cn105.h"


bool CN105Climate::isCycleRunning() {
return cycleRunning;
}

void CN105Climate::deferCycle() {

#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
uint32_t delay = DEFER_SCHEDULE_UPDATE_LOOP_DELAY * 3;
#else
uint32_t delay = DEFER_SCHEDULE_UPDATE_LOOP_DELAY;
#endif

ESP_LOGI(LOG_CYCLE_TAG, "Defering cycle trigger of %d ms", delay);
// forces the lastCompleteCycle offset of delay ms to allow a longer rest time
this->lastCompleteCycle += delay;

}
void CN105Climate::cycleStarted() {
ESP_LOGI(LOG_CYCLE_TAG, "1: Cycle start");
this->lastRequestInfo = CUSTOM_MILLIS;
cycleRunning = true;
}
void CN105Climate::cycleEnded() {
ESP_LOGI(LOG_CYCLE_TAG, "6: Cycle ends");
cycleRunning = false;
// a complete cycle is done
this->lastCompleteCycle = CUSTOM_MILLIS;
}

bool CN105Climate::hasUpdateIntervalPassed() {
return (CUSTOM_MILLIS - this->lastCompleteCycle) > this->update_interval_;
}

bool CN105Climate::didCycleTimeOut() {
return (CUSTOM_MILLIS - this->lastRequestInfo) > (5 * this->update_interval_) + 4000;
}

0 comments on commit e660d70

Please sign in to comment.