blob: b2a900b2df30376288f8e065d1d28241de650bcf [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
Matthew Barth89c2fa12021-02-04 14:50:40 -060023#include <nlohmann/json.hpp>
24
Patrick Williamsfbf47032023-07-17 12:27:34 -050025#include <format>
26
Matthew Barth89c2fa12021-02-04 14:50:40 -060027namespace phosphor::fan::control::json
28{
29
30using json = nlohmann::json;
31
Matthew Barth19c77492021-04-08 10:06:06 -050032CountStateTarget::CountStateTarget(const json& jsonObj,
33 const std::vector<Group>& groups) :
34 ActionBase(jsonObj, groups)
Matthew Barth89c2fa12021-02-04 14:50:40 -060035{
36 setCount(jsonObj);
37 setState(jsonObj);
38 setTarget(jsonObj);
39}
40
Matthew Barth6d2476c2021-04-08 10:48:57 -050041void CountStateTarget::run(Zone& zone)
Matthew Barth89c2fa12021-02-04 14:50:40 -060042{
43 size_t numAtState = 0;
Matthew Barth6d2476c2021-04-08 10:48:57 -050044 for (const auto& group : _groups)
Matthew Barth89c2fa12021-02-04 14:50:40 -060045 {
Matthew Barth82a7d0b2021-08-26 13:35:16 -050046 for (const auto& member : group.getMembers())
Matthew Barth89c2fa12021-02-04 14:50:40 -060047 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050048 try
Matthew Barth89c2fa12021-02-04 14:50:40 -060049 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050050 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 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050062 break;
Matthew Barth89c2fa12021-02-04 14:50:40 -060063 }
64 }
Matthew Barth82a7d0b2021-08-26 13:35:16 -050065 if (numAtState >= _count)
66 {
67 break;
68 }
Matthew Barth89c2fa12021-02-04 14:50:40 -060069 }
Matthew Barth82a7d0b2021-08-26 13:35:16 -050070
Matthew Barth53680112021-09-23 11:20:41 -050071 // Update zone's target hold based on action results
Matt Spinler274f2812022-04-13 14:42:55 -050072 zone.setTargetHold(getUniqueName(), _target, (numAtState >= _count));
Matthew Barth89c2fa12021-02-04 14:50:40 -060073}
74
75void CountStateTarget::setCount(const json& jsonObj)
76{
77 if (!jsonObj.contains("count"))
78 {
79 throw ActionParseError{ActionBase::getName(),
80 "Missing required count value"};
81 }
82 _count = jsonObj["count"].get<size_t>();
83}
84
85void CountStateTarget::setState(const json& jsonObj)
86{
87 if (!jsonObj.contains("state"))
88 {
89 throw ActionParseError{ActionBase::getName(),
90 "Missing required state value"};
91 }
92 _state = getJsonValue(jsonObj["state"]);
93}
94
95void CountStateTarget::setTarget(const json& jsonObj)
96{
Matthew Barthbab94f22021-08-24 11:21:42 -050097 if (!jsonObj.contains("target"))
Matthew Barth89c2fa12021-02-04 14:50:40 -060098 {
99 throw ActionParseError{ActionBase::getName(),
Matthew Barthbab94f22021-08-24 11:21:42 -0500100 "Missing required target value"};
Matthew Barth89c2fa12021-02-04 14:50:40 -0600101 }
Matthew Barthbab94f22021-08-24 11:21:42 -0500102 _target = jsonObj["target"].get<uint64_t>();
Matthew Barth89c2fa12021-02-04 14:50:40 -0600103}
104
105} // namespace phosphor::fan::control::json