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