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, 40 * "floors": [ 41 * { 42 * "group": "altitude", 43 * "floors": [ 44 * { 45 * "value": 5000, 46 * "floor": 4500 47 * } 48 * ] 49 * }, 50 * { 51 * "group": "power_mode", 52 * "floors": [ 53 * { 54 * "value": "MaximumPerformance", 55 * "floor": 5000 56 * } 57 * ] 58 * } 59 * ] 60 * } 61 * ] 62 * } 63 * 64 * When it runs, it will: 65 * 66 * 1. Evaluate the key_group 67 * - Find the max D-Bus property value (if numeric) of the member properties 68 * in this group. 69 * - Check it against each 'key' value in the fan_floor entries until 70 * the key_group property value < key value, so: 71 * max ambient temp < 27. 72 * - If the above check passes, the rest of that entry will be evaluated 73 * and then the action will be done. 74 * 75 * 2. Evaluate the group values in each floors array entry for this key value. 76 * - Find the max D-Bus property value (if numeric) of the member properties 77 * of this group - in this case 'altitude'. 78 * - Depending on the data type of that group, compare to the 'value' entry: 79 * - If numeric, check if the group's value is <= the 'value' one 80 * - Otherwise, check if it is == 81 * - If that passes, save the value from the 'floor' entry and continue to 82 * the next entry in the floors array, which, if present, would specify 83 * another group to check. In this case, 'power_mode'. Repeat the above 84 * step. 85 * - After all the group compares are done, choose the largest floor value 86 * to set the fan floor to. If any group check results doesn't end in 87 * a match being found, then the default floor will be set. 88 * 89 * Cases where the default floor will be set: 90 * - A table entry can't be found based on a key group's value. 91 * - A table entry can't be found based on a group's value. 92 * - A value can't be obtained for the 'key_group' D-Bus property group. 93 * - A value can't be obtained for any of the 'group' property groups. 94 * - A value is NaN, as no <, <=, or == checks would succeed. 95 * 96 * Other notes: 97 * - If a group has multiple members, they must be numeric or else 98 * the code will throw an exception. 99 */ 100 101 class MappedFloor : public ActionBase, public ActionRegister<MappedFloor> 102 { 103 public: 104 /* Name of this action */ 105 static constexpr auto name = "mapped_floor"; 106 107 MappedFloor() = delete; 108 MappedFloor(const MappedFloor&) = delete; 109 MappedFloor(MappedFloor&&) = delete; 110 MappedFloor& operator=(const MappedFloor&) = delete; 111 MappedFloor& operator=(MappedFloor&&) = delete; 112 ~MappedFloor() = default; 113 114 /** 115 * @brief Parse the JSON to set the members 116 * 117 * @param[in] jsonObj - JSON configuration of this action 118 * @param[in] groups - Groups of dbus objects the action uses 119 */ 120 MappedFloor(const json& jsonObj, const std::vector<Group>& groups); 121 122 /** 123 * @brief Run the action. See description above. 124 * 125 * @param[in] zone - Zone to run the action on 126 */ 127 void run(Zone& zone) override; 128 129 private: 130 /** 131 * @brief Parse and set the key group 132 * 133 * @param[in] jsonObj - JSON object for the action 134 */ 135 void setKeyGroup(const json& jsonObj); 136 137 /** 138 * @brief Parses and sets the floor group data members 139 * 140 * @param[in] jsonObj - JSON object for the action 141 */ 142 void setFloorTable(const json& jsonObj); 143 144 /** 145 * @brief Determines the maximum value of the property specified 146 * for the group of all members in the group. 147 * 148 * If not numeric, and more than one member, will throw an exception. 149 * Converts numeric values to doubles so they can be compared later. 150 * 151 * If cannot get at least one valid value, returns std::nullopt. 152 * 153 * @param[in] group - The group to get the max value of 154 * 155 * @param[in] manager - The Manager object 156 * 157 * @return optional<PropertyVariantType> - The value, or std::nullopt 158 */ 159 std::optional<PropertyVariantType> getMaxGroupValue(const Group& group, 160 const Manager& manager); 161 162 /** 163 * @brief Returns a pointer to the group object specified 164 * 165 * Throws ActionParseError if no group found 166 * 167 * @param[in] name - The group name 168 * 169 * @return const Group* - Pointer to the group 170 */ 171 const Group* getGroup(const std::string& name); 172 173 /* Key group pointer */ 174 const Group* _keyGroup; 175 176 using FloorEntry = std::tuple<PropertyVariantType, uint64_t>; 177 178 struct FloorGroup 179 { 180 const Group* group; 181 std::vector<FloorEntry> floorEntries; 182 }; 183 184 struct FanFloors 185 { 186 PropertyVariantType keyValue; 187 std::vector<FloorGroup> floorGroups; 188 }; 189 190 /* The fan floors action data, loaded from JSON */ 191 std::vector<FanFloors> _fanFloors; 192 }; 193 194 } // namespace phosphor::fan::control::json 195