Skip to content

Commit

Permalink
fix: allow cooldown period change mid cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
SanaaHamel committed Jul 12, 2023
1 parent eb29538 commit 02364be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/utility/fan_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ constexpr PolicyState evaluate(FanPolicyEnvironmental::Instance const& instance,
EnvironmentService::ServiceData const& state, chrono::system_clock::time_point now) {
if (should_filter(instance.params, state.voc_index_intake, state.voc_index_exhaust)) return Filtering;

if (now < instance.cooldown_ends) return Cooldown;
auto cooldown_end =
instance.last_filter + chrono::seconds(uint32_t(instance.params.cooldown.value_or(0)));
if (now < cooldown_end) return Cooldown;

return Idle;
}
Expand All @@ -48,7 +50,7 @@ float FanPolicyEnvironmental::Instance::operator()(
case Idle: return 0;
case Cooldown: return 1;
case Filtering: {
cooldown_ends = now + chrono::seconds(uint32_t(params.cooldown.value_or(0)));
last_filter = now;
return 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utility/fan_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct FanPolicyEnvironmental {

struct Instance {
FanPolicyEnvironmental const& params;
std::chrono::system_clock::time_point cooldown_ends;
std::chrono::system_clock::time_point last_filter = std::chrono::system_clock::time_point::min();

// Stateful.
// Returns fan power [0, 1] based on env state and policy parameters.
Expand Down

0 comments on commit 02364be

Please sign in to comment.