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 #include "count_state_target.hpp"
17 
18 #include "../manager.hpp"
19 #include "../zone.hpp"
20 #include "action.hpp"
21 #include "group.hpp"
22 
23 #include <fmt/format.h>
24 
25 #include <nlohmann/json.hpp>
26 
27 namespace phosphor::fan::control::json
28 {
29 
30 using json = nlohmann::json;
31 
32 CountStateTarget::CountStateTarget(const json& jsonObj,
33                                    const std::vector<Group>& groups) :
34     ActionBase(jsonObj, groups)
35 {
36     setCount(jsonObj);
37     setState(jsonObj);
38     setTarget(jsonObj);
39 }
40 
41 void CountStateTarget::run(Zone& zone)
42 {
43     size_t numAtState = 0;
44     for (const auto& group : _groups)
45     {
46         for (auto& member : group.getMembers())
47         {
48             try
49             {
50                 if (Manager::getObjValueVariant(member, group.getInterface(),
51                                                 group.getProperty()) == _state)
52                 {
53                     numAtState++;
54                 }
55             }
56             catch (const std::out_of_range& oore)
57             {
58                 // Default to property not equal when not found
59             }
60             if (numAtState >= _count)
61             {
62                 zone.setTarget(_target);
63                 break;
64             }
65         }
66         // Update group's fan control active allowed based on action results
67         zone.setActiveAllow(group.getName(), !(numAtState >= _count));
68     }
69 }
70 
71 void CountStateTarget::setCount(const json& jsonObj)
72 {
73     if (!jsonObj.contains("count"))
74     {
75         throw ActionParseError{ActionBase::getName(),
76                                "Missing required count value"};
77     }
78     _count = jsonObj["count"].get<size_t>();
79 }
80 
81 void CountStateTarget::setState(const json& jsonObj)
82 {
83     if (!jsonObj.contains("state"))
84     {
85         throw ActionParseError{ActionBase::getName(),
86                                "Missing required state value"};
87     }
88     _state = getJsonValue(jsonObj["state"]);
89 }
90 
91 void CountStateTarget::setTarget(const json& jsonObj)
92 {
93     if (!jsonObj.contains("speed"))
94     {
95         throw ActionParseError{ActionBase::getName(),
96                                "Missing required speed value"};
97     }
98     _target = jsonObj["speed"].get<uint64_t>();
99 }
100 
101 } // namespace phosphor::fan::control::json
102