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 CountStateFloor - Action to set a floor when members are at a state
31  *
32  * Sets the fans to a configured floor when a number of members within the
33  * group are at a configured state. Once the number of members at the given
34  * state falls below the configured count, the floor hold is released.
35  *
36  * Example JSON:
37  *    {
38  *      "name": "count_state_floor",
39  *      "count": 1,
40  *      "state": false,
41  *      "floor": 5000
42  *    }
43  */
44 class CountStateFloor :
45     public ActionBase,
46     public ActionRegister<CountStateFloor>
47 {
48   public:
49     /* Name of this action */
50     static constexpr auto name = "count_state_floor";
51 
52     CountStateFloor() = delete;
53     CountStateFloor(const CountStateFloor&) = delete;
54     CountStateFloor(CountStateFloor&&) = delete;
55     CountStateFloor& operator=(const CountStateFloor&) = delete;
56     CountStateFloor& operator=(CountStateFloor&&) = delete;
57     ~CountStateFloor() = default;
58 
59     /**
60      * @brief Set floor when a number of members are at a given state
61      *
62      * @param[in] jsonObj - JSON configuration of this action
63      * @param[in] groups - Groups of dbus objects the action uses
64      */
65     CountStateFloor(const json& jsonObj, const std::vector<Group>& groups);
66 
67     /**
68      * @brief Run the action
69      *
70      * Counts the number of members within a group are equal to a given state
71      * and when the number of members are at or above the given state, the
72      * zone is set to the configured floor, with a hold.  The hold is released
73      * when the number of members goes below the configured count.
74      *
75      * @param[in] zone - Zone to run the action on
76      */
77     void run(Zone& zone) override;
78 
79   private:
80     /**
81      * @brief Parse and set the count
82      *
83      * @param[in] jsonObj - JSON object for the action
84      *
85      * Sets the number of members that must equal the state
86      */
87     void setCount(const json& jsonObj);
88 
89     /**
90      * @brief Parse and set the state
91      *
92      * @param[in] jsonObj - JSON object for the action
93      *
94      * Sets the state for each member to equal to set the floor
95      */
96     void setState(const json& jsonObj);
97 
98     /**
99      * @brief Parse and set the floor
100      *
101      * @param[in] jsonObj - JSON object for the action
102      *
103      * Sets the floor to use when running the action
104      */
105     void setFloor(const json& jsonObj);
106 
107     /* Number of group members */
108     size_t _count;
109 
110     /* State the members must be at to set the floor */
111     PropertyVariantType _state;
112 
113     /* Floor for this action */
114     uint64_t _floor;
115 };
116 
117 } // namespace phosphor::fan::control::json
118