blob: 9f2ba5ecc62fe8560e1d2ba3ae995cdfefbc299e [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 {
67 zone.setTarget(_target);
68 break;
Matthew Barth89c2fa12021-02-04 14:50:40 -060069 }
70 }
Matthew Barth82a7d0b2021-08-26 13:35:16 -050071 if (numAtState >= _count)
72 {
73 break;
74 }
Matthew Barth89c2fa12021-02-04 14:50:40 -060075 }
Matthew Barth82a7d0b2021-08-26 13:35:16 -050076
77 // Update zone's active fan control allowed based on action results
78 zone.setActiveAllow(ActionBase::getName() + std::to_string(_id),
79 !(numAtState >= _count));
Matthew Barth89c2fa12021-02-04 14:50:40 -060080}
81
82void CountStateTarget::setCount(const json& jsonObj)
83{
84 if (!jsonObj.contains("count"))
85 {
86 throw ActionParseError{ActionBase::getName(),
87 "Missing required count value"};
88 }
89 _count = jsonObj["count"].get<size_t>();
90}
91
92void CountStateTarget::setState(const json& jsonObj)
93{
94 if (!jsonObj.contains("state"))
95 {
96 throw ActionParseError{ActionBase::getName(),
97 "Missing required state value"};
98 }
99 _state = getJsonValue(jsonObj["state"]);
100}
101
102void CountStateTarget::setTarget(const json& jsonObj)
103{
Matthew Barthbab94f22021-08-24 11:21:42 -0500104 if (!jsonObj.contains("target"))
Matthew Barth89c2fa12021-02-04 14:50:40 -0600105 {
106 throw ActionParseError{ActionBase::getName(),
Matthew Barthbab94f22021-08-24 11:21:42 -0500107 "Missing required target value"};
Matthew Barth89c2fa12021-02-04 14:50:40 -0600108 }
Matthew Barthbab94f22021-08-24 11:21:42 -0500109 _target = jsonObj["target"].get<uint64_t>();
Matthew Barth89c2fa12021-02-04 14:50:40 -0600110}
111
112} // namespace phosphor::fan::control::json