blob: a99fa971fe7a0f791ac1ee83fe967f93dc27e268 [file] [log] [blame]
Matthew Barth17d1fe22017-05-11 15:00:36 -05001#pragma once
2
3#include <algorithm>
Matthew Barth4af419c2017-06-12 13:39:31 -05004#include <numeric>
Matthew Barth17d1fe22017-05-11 15:00:36 -05005
6namespace phosphor
7{
8namespace fan
9{
10namespace control
11{
12namespace action
13{
14
15/**
16 * @brief An action to set the speed on a zone
Matthew Barth861d77c2017-05-22 14:18:25 -050017 * @details The zone is held at the given speed when a defined number of
Matthew Barth17d1fe22017-05-11 15:00:36 -050018 * properties in the group are set to the given state
19 *
20 * @param[in] count - Number of properties
21 * @param[in] state - Value the property(s) needed to be set at
22 * @param[in] speed - Speed to set the zone to
23 *
24 * @return Lambda function
25 * A lambda function to set the zone speed when the number of properties
26 * within the group are at a certain value
27 */
Matthew Barth9e741ed2017-06-02 16:29:09 -050028template <typename T>
29auto count_state_before_speed(size_t count, T&& state, uint64_t speed)
Matthew Barth17d1fe22017-05-11 15:00:36 -050030{
Matthew Barth9e741ed2017-06-02 16:29:09 -050031 return [count,
32 speed,
33 state = std::forward<T>(state)](auto& zone, auto& group)
Matthew Barth17d1fe22017-05-11 15:00:36 -050034 {
35 size_t numAtState = std::count_if(
36 group.begin(),
37 group.end(),
38 [&zone, &state](auto const& entry)
39 {
Matthew Barth9e741ed2017-06-02 16:29:09 -050040 return zone.template getPropertyValue<T>(
Matthew Barth17d1fe22017-05-11 15:00:36 -050041 entry.first,
Matthew Barthcec5ab72017-06-02 15:20:56 -050042 std::get<intfPos>(entry.second),
Matthew Barth17d1fe22017-05-11 15:00:36 -050043 std::get<propPos>(entry.second)) == state;
44 });
Matthew Barth861d77c2017-05-22 14:18:25 -050045 // Update group's fan control active allowed based on action results
46 zone.setActiveAllow(&group, !(numAtState >= count));
Matthew Barth17d1fe22017-05-11 15:00:36 -050047 if (numAtState >= count)
48 {
49 zone.setSpeed(speed);
50 }
51 };
52}
53
Matthew Barth4af419c2017-06-12 13:39:31 -050054/**
55 * @brief An action to set the floor speed on a zone
56 * @details Based on the average of the defined sensor group values, the floor
57 * speed is selected from the first map key entry that the average sensor value
58 * is less than.
59 *
60 * @param[in] val_to_speed - Ordered map of sensor value-to-speed
61 *
62 * @return Lambda function
63 * A lambda function to set the zone's floor speed when the average of
64 * property values within the group is below the lowest sensor value given
65 */
66auto set_floor_from_average_sensor_value(
67 std::map<int64_t, uint64_t>&& val_to_speed)
68{
69 return [val_to_speed = std::move(val_to_speed)](auto& zone, auto& group)
70 {
71 auto speed = zone.getDefFloor();
72 if (group.size() != 0)
73 {
74 auto sumValue = std::accumulate(
75 group.begin(),
76 group.end(),
77 0,
78 [&zone](int64_t sum, auto const& entry)
79 {
80 return sum + zone.template getPropertyValue<int64_t>(
81 entry.first,
82 std::get<intfPos>(entry.second),
83 std::get<propPos>(entry.second));
84 });
85 auto avgValue= sumValue / group.size();
86 auto it = std::find_if(
87 val_to_speed.begin(),
88 val_to_speed.end(),
89 [&avgValue](auto const& entry)
90 {
91 return avgValue < entry.first;
92 }
93 );
94 if (it != std::end(val_to_speed))
95 {
96 speed = (*it).second;
97 }
98 }
99 zone.setFloor(speed);
100 };
101}
102
Matthew Barth17d1fe22017-05-11 15:00:36 -0500103} // namespace action
104} // namespace control
105} // namespace fan
106} // namespace phosphor