From 2a646c5fdbe24e41be6ed22b2c1c47edff09eb39 Mon Sep 17 00:00:00 2001 From: Matthew Barth Date: Thu, 5 Oct 2017 17:04:11 -0500 Subject: Action that creates/starts a timer Another action is provided to the timer and is performed when the timer expires. Change-Id: Ie6a06b0d56272a158d74bf03287183f07f00aed8 Signed-off-by: Matthew Barth --- control/actions.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ control/actions.hpp | 15 ++++++++++++ 2 files changed, 85 insertions(+) diff --git a/control/actions.cpp b/control/actions.cpp index a608293..b1eb9be 100644 --- a/control/actions.cpp +++ b/control/actions.cpp @@ -11,6 +11,76 @@ namespace action using namespace phosphor::fan; +Action call_actions_based_on_timer(Timer&& tConf, std::vector&& actions) +{ + return [tConf = std::move(tConf), + actions = std::move(actions)](control::Zone& zone, + const Group& group) + { + try + { + // Find any services that do not have an owner + auto services = zone.getGroupServices(&group); + auto setTimer = std::any_of( + services.begin(), + services.end(), + [](const auto& s) + { + return !std::get(s); + }); + if (setTimer && + zone.findTimer(group, actions) == + std::end(zone.getTimerEvents())) + { + // Associate event data with timer + std::unique_ptr eventData = + std::make_unique( + EventData + { + group, + "", + nullptr, + actions + } + ); + // Create/start timer and associate event data with it + std::unique_ptr timer = + std::make_unique( + zone.getEventPtr(), + [&zone, + actions = &actions, + group = &group]() + { + zone.timerExpired(*group, *actions); + }); + if (!timer->running()) + { + timer->start(std::get(tConf), + std::get(tConf)); + } + zone.addTimer(std::move(eventData), std::move(timer)); + } + else + { + // Stop and remove any timers for this group + auto timer = zone.findTimer(group, actions); + if (timer != std::end(zone.getTimerEvents())) + { + if (std::get(*timer)->running()) + { + std::get(*timer)->stop(); + } + zone.removeTimer(timer); + } + } + } + catch (const std::out_of_range& oore) + { + // Group not found, no timers set + } + }; +} + void set_request_speed_base_with_max(control::Zone& zone, const Group& group) { diff --git a/control/actions.hpp b/control/actions.hpp index 3061814..fff8250 100644 --- a/control/actions.hpp +++ b/control/actions.hpp @@ -14,6 +14,21 @@ namespace control namespace action { +/** + * @brief An action that wraps a list of actions with a timer + * @details Sets up a list of actions to be invoked when the defined timer + * expires (or for each expiration of a repeating timer). + * + * @param[in] tConf - Timer configuration parameters + * @param[in] action - List of actions to be called when the timer expires + * + * @return Action lambda function + * An Action function that creates a timer + */ +Action call_actions_based_on_timer( + Timer&& tConf, + std::vector&& actions); + /** * @brief An action to set the request speed base * @details A new target speed is determined using a speed delta being added -- cgit v1.2.1