blob: 1017ae66ce6fafd3b4591c5f4418d1069f75b437 [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
Matthew Barth82a7d0b2021-08-26 13:35:16 -050032// Instance id for setting the unique id of each instance of this action
33size_t CountStateTarget::instanceId = 0;
34
Matthew Barth19c77492021-04-08 10:06:06 -050035CountStateTarget::CountStateTarget(const json& jsonObj,
36 const std::vector<Group>& groups) :
37 ActionBase(jsonObj, groups)
Matthew Barth89c2fa12021-02-04 14:50:40 -060038{
39 setCount(jsonObj);
40 setState(jsonObj);
41 setTarget(jsonObj);
Matthew Barth82a7d0b2021-08-26 13:35:16 -050042
43 _id = instanceId++;
Matthew Barth89c2fa12021-02-04 14:50:40 -060044}
45
Matthew Barth6d2476c2021-04-08 10:48:57 -050046void CountStateTarget::run(Zone& zone)
Matthew Barth89c2fa12021-02-04 14:50:40 -060047{
48 size_t numAtState = 0;
Matthew Barth6d2476c2021-04-08 10:48:57 -050049 for (const auto& group : _groups)
Matthew Barth89c2fa12021-02-04 14:50:40 -060050 {
Matthew Barth82a7d0b2021-08-26 13:35:16 -050051 for (const auto& member : group.getMembers())
Matthew Barth89c2fa12021-02-04 14:50:40 -060052 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050053 try
Matthew Barth89c2fa12021-02-04 14:50:40 -060054 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050055 if (Manager::getObjValueVariant(member, group.getInterface(),
56 group.getProperty()) == _state)
57 {
58 numAtState++;
59 }
60 }
61 catch (const std::out_of_range& oore)
62 {
63 // Default to property not equal when not found
64 }
65 if (numAtState >= _count)
66 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050067 break;
Matthew Barth89c2fa12021-02-04 14:50:40 -060068 }
69 }
Matthew Barth82a7d0b2021-08-26 13:35:16 -050070 if (numAtState >= _count)
71 {
72 break;
73 }
Matthew Barth89c2fa12021-02-04 14:50:40 -060074 }
Matthew Barth82a7d0b2021-08-26 13:35:16 -050075
Matthew Barth53680112021-09-23 11:20:41 -050076 // Update zone's target hold based on action results
77 zone.setTargetHold(ActionBase::getName() + std::to_string(_id), _target,
78 (numAtState >= _count));
Matthew Barth89c2fa12021-02-04 14:50:40 -060079}
80
81void CountStateTarget::setCount(const json& jsonObj)
82{
83 if (!jsonObj.contains("count"))
84 {
85 throw ActionParseError{ActionBase::getName(),
86 "Missing required count value"};
87 }
88 _count = jsonObj["count"].get<size_t>();
89}
90
91void CountStateTarget::setState(const json& jsonObj)
92{
93 if (!jsonObj.contains("state"))
94 {
95 throw ActionParseError{ActionBase::getName(),
96 "Missing required state value"};
97 }
98 _state = getJsonValue(jsonObj["state"]);
99}
100
101void CountStateTarget::setTarget(const json& jsonObj)
102{
Matthew Barthbab94f22021-08-24 11:21:42 -0500103 if (!jsonObj.contains("target"))
Matthew Barth89c2fa12021-02-04 14:50:40 -0600104 {
105 throw ActionParseError{ActionBase::getName(),
Matthew Barthbab94f22021-08-24 11:21:42 -0500106 "Missing required target value"};
Matthew Barth89c2fa12021-02-04 14:50:40 -0600107 }
Matthew Barthbab94f22021-08-24 11:21:42 -0500108 _target = jsonObj["target"].get<uint64_t>();
Matthew Barth89c2fa12021-02-04 14:50:40 -0600109}
110
111} // namespace phosphor::fan::control::json