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