-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from echavet/cycle_mgmt_refactor
Cycle mgmt refactor
- Loading branch information
Showing
10 changed files
with
122 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,63 @@ | ||
#include "cycle_management.h" | ||
#include "cn105.h" | ||
#include "Globals.h" | ||
|
||
|
||
bool CN105Climate::isCycleRunning() { | ||
|
||
void cycleManagement::checkTimeout(unsigned int update_interval) { | ||
if (doesCycleTimeOut(update_interval)) { // does it last too long ? | ||
ESP_LOGW(TAG, "Cycle timeout, reseting cycle..."); | ||
cycleEnded(true); | ||
} | ||
} | ||
|
||
|
||
bool cycleManagement::isCycleRunning() { | ||
return cycleRunning; | ||
} | ||
|
||
void CN105Climate::deferCycle() { | ||
void cycleManagement::init() { | ||
cycleRunning = false; | ||
lastCompleteCycleMs = CUSTOM_MILLIS; | ||
} | ||
|
||
void cycleManagement::deferCycle() { | ||
|
||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG | ||
uint32_t delay = DEFER_SCHEDULE_UPDATE_LOOP_DELAY * 3; | ||
uint32_t delay = DEFER_SCHEDULE_UPDATE_LOOP_DELAY * 2; | ||
#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; | ||
lastCompleteCycleMs = CUSTOM_MILLIS + delay; | ||
|
||
} | ||
void CN105Climate::cycleStarted() { | ||
void cycleManagement::cycleStarted() { | ||
ESP_LOGI(LOG_CYCLE_TAG, "1: Cycle start"); | ||
this->lastRequestInfo = CUSTOM_MILLIS; | ||
lastCycleStartMs = CUSTOM_MILLIS; | ||
cycleRunning = true; | ||
} | ||
void CN105Climate::cycleEnded() { | ||
ESP_LOGI(LOG_CYCLE_TAG, "6: Cycle ends"); | ||
|
||
void cycleManagement::cycleEnded(bool timedOut) { | ||
cycleRunning = false; | ||
// a complete cycle is done | ||
this->lastCompleteCycle = CUSTOM_MILLIS; | ||
|
||
if (lastCompleteCycleMs < CUSTOM_MILLIS) { // we check this because of defering mecanism | ||
// a complete cycle is done | ||
lastCompleteCycleMs = CUSTOM_MILLIS; // to prevent next inteval from ticking too soon | ||
} | ||
|
||
ESP_LOGI(LOG_CYCLE_TAG, "6: Cycle ended in %.1f seconds (with timeout?: %s)", | ||
(lastCompleteCycleMs - lastCycleStartMs) / 1000.0, timedOut ? "YES" : " NO"); | ||
} | ||
|
||
bool CN105Climate::hasUpdateIntervalPassed() { | ||
return (CUSTOM_MILLIS - this->lastCompleteCycle) > this->update_interval_; | ||
bool cycleManagement::hasUpdateIntervalPassed(unsigned int update_interval) { | ||
if (CUSTOM_MILLIS < lastCompleteCycleMs) return false; // must be checked because operands are they are unsigned | ||
return (CUSTOM_MILLIS - lastCompleteCycleMs) > update_interval; | ||
} | ||
|
||
bool CN105Climate::didCycleTimeOut() { | ||
return (CUSTOM_MILLIS - this->lastRequestInfo) > (5 * this->update_interval_) + 4000; | ||
bool cycleManagement::doesCycleTimeOut(unsigned int update_interval) { | ||
if (CUSTOM_MILLIS < lastCycleStartMs) return false; // must be checked because operands are they are unsigned | ||
return (CUSTOM_MILLIS - lastCycleStartMs) > (2 * update_interval) + 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
struct cycleManagement { | ||
|
||
bool cycleRunning = false; | ||
unsigned long lastCycleStartMs = 0; | ||
unsigned long lastCompleteCycleMs = 0; | ||
|
||
void init(); | ||
void cycleStarted(); | ||
void cycleEnded(bool timedOut = false); | ||
bool hasUpdateIntervalPassed(unsigned int update_interval); | ||
bool doesCycleTimeOut(unsigned int update_interval); | ||
bool isCycleRunning(); | ||
void deferCycle(); | ||
void checkTimeout(unsigned int update_interval); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.