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