Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "../zone.hpp" |
| 19 | #include "action.hpp" |
| 20 | #include "group.hpp" |
| 21 | |
| 22 | #include <nlohmann/json.hpp> |
| 23 | |
| 24 | namespace phosphor::fan::control::json |
| 25 | { |
| 26 | |
| 27 | using json = nlohmann::json; |
| 28 | |
| 29 | /** |
| 30 | * @class MappedFloor - Action to set a fan floor based on ranges of |
| 31 | * multiple sensor values. |
| 32 | * For example, consider the following config: |
| 33 | * |
| 34 | * { |
| 35 | * "name": "mapped_floor", |
| 36 | * "key_group": "ambient_temp", |
| 37 | * "fan_floors": [ |
| 38 | * { |
| 39 | * "key": 27, |
Matt Spinler | a17d5cc | 2022-02-02 13:13:30 -0600 | [diff] [blame^] | 40 | * "floor_offset_parameter": "floor_27_offset", |
Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 41 | * "floors": [ |
| 42 | * { |
| 43 | * "group": "altitude", |
| 44 | * "floors": [ |
| 45 | * { |
| 46 | * "value": 5000, |
| 47 | * "floor": 4500 |
| 48 | * } |
| 49 | * ] |
| 50 | * }, |
| 51 | * { |
| 52 | * "group": "power_mode", |
| 53 | * "floors": [ |
| 54 | * { |
| 55 | * "value": "MaximumPerformance", |
| 56 | * "floor": 5000 |
| 57 | * } |
| 58 | * ] |
| 59 | * } |
| 60 | * ] |
| 61 | * } |
| 62 | * ] |
| 63 | * } |
| 64 | * |
| 65 | * When it runs, it will: |
| 66 | * |
| 67 | * 1. Evaluate the key_group |
| 68 | * - Find the max D-Bus property value (if numeric) of the member properties |
| 69 | * in this group. |
| 70 | * - Check it against each 'key' value in the fan_floor entries until |
| 71 | * the key_group property value < key value, so: |
| 72 | * max ambient temp < 27. |
| 73 | * - If the above check passes, the rest of that entry will be evaluated |
| 74 | * and then the action will be done. |
| 75 | * |
| 76 | * 2. Evaluate the group values in each floors array entry for this key value. |
| 77 | * - Find the max D-Bus property value (if numeric) of the member properties |
| 78 | * of this group - in this case 'altitude'. |
| 79 | * - Depending on the data type of that group, compare to the 'value' entry: |
| 80 | * - If numeric, check if the group's value is <= the 'value' one |
| 81 | * - Otherwise, check if it is == |
| 82 | * - If that passes, save the value from the 'floor' entry and continue to |
| 83 | * the next entry in the floors array, which, if present, would specify |
| 84 | * another group to check. In this case, 'power_mode'. Repeat the above |
| 85 | * step. |
| 86 | * - After all the group compares are done, choose the largest floor value |
Matt Spinler | a17d5cc | 2022-02-02 13:13:30 -0600 | [diff] [blame^] | 87 | * to set the fan floor to, but first apply the floor offset provided |
| 88 | * by the parameter in the 'floor_offset_parameter' field, if it present. |
| 89 | * - If any group check results doesn't end in a match being found, then |
| 90 | * the default floor will be set. |
Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 91 | * |
| 92 | * Cases where the default floor will be set: |
| 93 | * - A table entry can't be found based on a key group's value. |
| 94 | * - A table entry can't be found based on a group's value. |
| 95 | * - A value can't be obtained for the 'key_group' D-Bus property group. |
| 96 | * - A value can't be obtained for any of the 'group' property groups. |
| 97 | * - A value is NaN, as no <, <=, or == checks would succeed. |
| 98 | * |
| 99 | * Other notes: |
| 100 | * - If a group has multiple members, they must be numeric or else |
| 101 | * the code will throw an exception. |
Matt Spinler | c981bb5 | 2021-09-21 08:31:14 -0500 | [diff] [blame] | 102 | * |
| 103 | * - The group inside the floors array can also be a Manager parameter, so that |
| 104 | * this action can operate on a parameter value set by another action. |
| 105 | * |
| 106 | * So instead of |
| 107 | * "group": "altitude", |
| 108 | * it can be: |
| 109 | * "parameter": "some_parameter" |
Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 110 | */ |
| 111 | |
| 112 | class MappedFloor : public ActionBase, public ActionRegister<MappedFloor> |
| 113 | { |
| 114 | public: |
| 115 | /* Name of this action */ |
| 116 | static constexpr auto name = "mapped_floor"; |
| 117 | |
| 118 | MappedFloor() = delete; |
| 119 | MappedFloor(const MappedFloor&) = delete; |
| 120 | MappedFloor(MappedFloor&&) = delete; |
| 121 | MappedFloor& operator=(const MappedFloor&) = delete; |
| 122 | MappedFloor& operator=(MappedFloor&&) = delete; |
| 123 | ~MappedFloor() = default; |
| 124 | |
| 125 | /** |
| 126 | * @brief Parse the JSON to set the members |
| 127 | * |
| 128 | * @param[in] jsonObj - JSON configuration of this action |
| 129 | * @param[in] groups - Groups of dbus objects the action uses |
| 130 | */ |
| 131 | MappedFloor(const json& jsonObj, const std::vector<Group>& groups); |
| 132 | |
| 133 | /** |
| 134 | * @brief Run the action. See description above. |
| 135 | * |
| 136 | * @param[in] zone - Zone to run the action on |
| 137 | */ |
| 138 | void run(Zone& zone) override; |
| 139 | |
| 140 | private: |
| 141 | /** |
| 142 | * @brief Parse and set the key group |
| 143 | * |
| 144 | * @param[in] jsonObj - JSON object for the action |
| 145 | */ |
| 146 | void setKeyGroup(const json& jsonObj); |
| 147 | |
| 148 | /** |
| 149 | * @brief Parses and sets the floor group data members |
| 150 | * |
| 151 | * @param[in] jsonObj - JSON object for the action |
| 152 | */ |
| 153 | void setFloorTable(const json& jsonObj); |
| 154 | |
| 155 | /** |
Matt Spinler | a17d5cc | 2022-02-02 13:13:30 -0600 | [diff] [blame^] | 156 | * @brief Applies the offset in offsetParameter to the |
| 157 | * value passed in. |
| 158 | * |
| 159 | * If offsetParameter is empty then no offset will be |
| 160 | * applied. |
| 161 | * |
| 162 | * Note: The offset may be negative. |
| 163 | * |
| 164 | * @param[in] floor - The floor to apply offset to |
| 165 | * @param[in] offsetParameter - The floor offset parameter |
| 166 | * |
| 167 | * @return uint64_t - The new floor value |
| 168 | */ |
| 169 | uint64_t applyFloorOffset(uint64_t floor, |
| 170 | const std::string& offsetParameter) const; |
| 171 | |
| 172 | /** |
Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 173 | * @brief Determines the maximum value of the property specified |
| 174 | * for the group of all members in the group. |
| 175 | * |
| 176 | * If not numeric, and more than one member, will throw an exception. |
| 177 | * Converts numeric values to doubles so they can be compared later. |
| 178 | * |
| 179 | * If cannot get at least one valid value, returns std::nullopt. |
| 180 | * |
| 181 | * @param[in] group - The group to get the max value of |
| 182 | * |
| 183 | * @param[in] manager - The Manager object |
| 184 | * |
| 185 | * @return optional<PropertyVariantType> - The value, or std::nullopt |
| 186 | */ |
| 187 | std::optional<PropertyVariantType> getMaxGroupValue(const Group& group, |
| 188 | const Manager& manager); |
| 189 | |
| 190 | /** |
| 191 | * @brief Returns a pointer to the group object specified |
| 192 | * |
| 193 | * Throws ActionParseError if no group found |
| 194 | * |
| 195 | * @param[in] name - The group name |
| 196 | * |
| 197 | * @return const Group* - Pointer to the group |
| 198 | */ |
| 199 | const Group* getGroup(const std::string& name); |
| 200 | |
| 201 | /* Key group pointer */ |
| 202 | const Group* _keyGroup; |
| 203 | |
| 204 | using FloorEntry = std::tuple<PropertyVariantType, uint64_t>; |
| 205 | |
| 206 | struct FloorGroup |
| 207 | { |
Matt Spinler | c981bb5 | 2021-09-21 08:31:14 -0500 | [diff] [blame] | 208 | std::variant<const Group*, std::string> groupOrParameter; |
Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 209 | std::vector<FloorEntry> floorEntries; |
| 210 | }; |
| 211 | |
| 212 | struct FanFloors |
| 213 | { |
| 214 | PropertyVariantType keyValue; |
Matt Spinler | a17d5cc | 2022-02-02 13:13:30 -0600 | [diff] [blame^] | 215 | std::string offsetParameter; |
Matt Spinler | 848799f | 2021-07-01 12:43:07 -0600 | [diff] [blame] | 216 | std::vector<FloorGroup> floorGroups; |
| 217 | }; |
| 218 | |
| 219 | /* The fan floors action data, loaded from JSON */ |
| 220 | std::vector<FanFloors> _fanFloors; |
| 221 | }; |
| 222 | |
| 223 | } // namespace phosphor::fan::control::json |