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