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   protected:
74     /* Instance id for each instance of this action */
75     static size_t instanceId;
76 
77   private:
78     /* Number of group members */
79     size_t _count;
80 
81     /* State the members must be at to set the target */
82     PropertyVariantType _state;
83 
84     /* Target for this action */
85     uint64_t _target;
86 
87     /* Unique id of this action */
88     size_t _id;
89 
90     /**
91      * @brief Parse and set the count
92      *
93      * @param[in] jsonObj - JSON object for the action
94      *
95      * Sets the number of members that must equal the state
96      */
97     void setCount(const json& jsonObj);
98 
99     /**
100      * @brief Parse and set the state
101      *
102      * @param[in] jsonObj - JSON object for the action
103      *
104      * Sets the state for each member to equal to set the target
105      */
106     void setState(const json& jsonObj);
107 
108     /**
109      * @brief Parse and set the target
110      *
111      * @param[in] jsonObj - JSON object for the action
112      *
113      * Sets the target to use when running the action
114      */
115     void setTarget(const json& jsonObj);
116 };
117 
118 } // namespace phosphor::fan::control::json
119