Add event init trigger
An event defined with an init trigger will run the event's actions
upon the event initializing. An event initializes when the fan control
application starts or at anytime an event's precondition is valid.
Tested:
Used missing fan action to set a speed at application start
Used missing fan action to set a speed upon valid precondition
Change-Id: I96a36425a2ca345371c27d7b580070bec28acaa1
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/triggers.cpp b/control/triggers.cpp
index 376ac9b..3cddce6 100644
--- a/control/triggers.cpp
+++ b/control/triggers.cpp
@@ -77,6 +77,31 @@
};
}
+Trigger init(Handler&& handler)
+{
+ return [handler = std::move(handler)](control::Zone& zone,
+ const Group& group,
+ const std::vector<Action>& actions)
+ {
+ // A handler function is optional
+ if (handler)
+ {
+ sdbusplus::message::message nullMsg{nullptr};
+ // Execute the given handler function prior to running the actions
+ handler(zone.getBus(), nullMsg, zone);
+ }
+ // Run action functions for initial event state
+ std::for_each(
+ actions.begin(),
+ actions.end(),
+ [&zone, &group](auto const& action)
+ {
+ action(zone, group);
+ }
+ );
+ };
+}
+
} // namespace trigger
} // namespace control
} // namespace fan