Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <algorithm> |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 4 | #include <numeric> |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace fan |
| 9 | { |
| 10 | namespace control |
| 11 | { |
| 12 | namespace action |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * @brief An action to set the speed on a zone |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 17 | * @details The zone is held at the given speed when a defined number of |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 18 | * 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 Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 28 | template <typename T> |
| 29 | auto count_state_before_speed(size_t count, T&& state, uint64_t speed) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 30 | { |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 31 | return [count, |
| 32 | speed, |
| 33 | state = std::forward<T>(state)](auto& zone, auto& group) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 34 | { |
| 35 | size_t numAtState = std::count_if( |
| 36 | group.begin(), |
| 37 | group.end(), |
| 38 | [&zone, &state](auto const& entry) |
| 39 | { |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 40 | return zone.template getPropertyValue<T>( |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 41 | entry.first, |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 42 | std::get<intfPos>(entry.second), |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 43 | std::get<propPos>(entry.second)) == state; |
| 44 | }); |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 45 | // Update group's fan control active allowed based on action results |
| 46 | zone.setActiveAllow(&group, !(numAtState >= count)); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 47 | if (numAtState >= count) |
| 48 | { |
| 49 | zone.setSpeed(speed); |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 54 | /** |
| 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 | */ |
| 66 | auto 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 Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 103 | } // namespace action |
| 104 | } // namespace control |
| 105 | } // namespace fan |
| 106 | } // namespace phosphor |