control: Add count state set target action
Add the YAML based count_state_before_speed action function as an action
class for JSON configs to use.
Change-Id: Ibfb54190728ebcd22229fe665dae864666054d9d
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/Makefile.am b/control/Makefile.am
index 7d7824a..d90adc1 100644
--- a/control/Makefile.am
+++ b/control/Makefile.am
@@ -33,7 +33,8 @@
json/event.cpp \
json/actions/default_floor.cpp \
json/actions/request_target_base.cpp \
- json/actions/missing_owner_target.cpp
+ json/actions/missing_owner_target.cpp \
+ json/actions/count_state_target.cpp
else
phosphor_fan_control_SOURCES += \
argument.cpp \
diff --git a/control/json/actions/count_state_target.cpp b/control/json/actions/count_state_target.cpp
new file mode 100644
index 0000000..432137b
--- /dev/null
+++ b/control/json/actions/count_state_target.cpp
@@ -0,0 +1,96 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "count_state_target.hpp"
+
+#include "../manager.hpp"
+#include "../zone.hpp"
+#include "action.hpp"
+#include "group.hpp"
+
+#include <fmt/format.h>
+
+#include <nlohmann/json.hpp>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+
+CountStateTarget::CountStateTarget(const json& jsonObj) : ActionBase(jsonObj)
+{
+ setCount(jsonObj);
+ setState(jsonObj);
+ setTarget(jsonObj);
+}
+
+void CountStateTarget::run(Zone& zone, const Group& group)
+{
+ size_t numAtState = 0;
+ for (auto& member : group.getMembers())
+ {
+ try
+ {
+ if (Manager::getObjValueVariant(member, group.getInterface(),
+ group.getProperty()) == _state)
+ {
+ numAtState++;
+ }
+ }
+ catch (const std::out_of_range& oore)
+ {
+ // Default to property not equal when not found
+ }
+ if (numAtState >= _count)
+ {
+ zone.setTarget(_target);
+ break;
+ }
+ }
+ // Update group's fan control active allowed based on action results
+ zone.setActiveAllow(group.getName(), !(numAtState >= _count));
+}
+
+void CountStateTarget::setCount(const json& jsonObj)
+{
+ if (!jsonObj.contains("count"))
+ {
+ throw ActionParseError{ActionBase::getName(),
+ "Missing required count value"};
+ }
+ _count = jsonObj["count"].get<size_t>();
+}
+
+void CountStateTarget::setState(const json& jsonObj)
+{
+ if (!jsonObj.contains("state"))
+ {
+ throw ActionParseError{ActionBase::getName(),
+ "Missing required state value"};
+ }
+ _state = getJsonValue(jsonObj["state"]);
+}
+
+void CountStateTarget::setTarget(const json& jsonObj)
+{
+ if (!jsonObj.contains("speed"))
+ {
+ throw ActionParseError{ActionBase::getName(),
+ "Missing required speed value"};
+ }
+ _target = jsonObj["speed"].get<uint64_t>();
+}
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json/actions/count_state_target.hpp b/control/json/actions/count_state_target.hpp
new file mode 100644
index 0000000..a17f44a
--- /dev/null
+++ b/control/json/actions/count_state_target.hpp
@@ -0,0 +1,111 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "../zone.hpp"
+#include "action.hpp"
+#include "group.hpp"
+
+#include <nlohmann/json.hpp>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+
+/**
+ * @class CountStateTarget - Action to set a target when members are at a state
+ *
+ * Sets the fans to a configured target when a number of members within the
+ * group are at a configured state. Once the number of members at the given
+ * state falls below the configured count, active fan target changes are
+ * allowed.
+ */
+class CountStateTarget :
+ public ActionBase,
+ public ActionRegister<CountStateTarget>
+{
+ public:
+ /* Name of this action */
+ static constexpr auto name = "count_state_before_speed";
+
+ CountStateTarget() = delete;
+ CountStateTarget(const CountStateTarget&) = delete;
+ CountStateTarget(CountStateTarget&&) = delete;
+ CountStateTarget& operator=(const CountStateTarget&) = delete;
+ CountStateTarget& operator=(CountStateTarget&&) = delete;
+ ~CountStateTarget() = default;
+
+ /**
+ * @brief Set target when a number of members are at a given state
+ *
+ * @param[in] jsonObj - JSON configuration of this action
+ */
+ explicit CountStateTarget(const json& jsonObj);
+
+ /**
+ * @brief Run the action
+ *
+ * Counts the number of members within a group are equal to a given state
+ * and when the number of members are at or above the given state, the fans
+ * within the zone are set to the configured target. The fans are held at
+ * the configured target until the number of members equal to the given
+ * state fall below the provided count.
+ *
+ * @param[in] zone - Zone to run the action on
+ * @param[in] group - Group of dbus objects the action runs against
+ */
+ void run(Zone& zone, const Group& group) override;
+
+ private:
+ /* Number of group members */
+ size_t _count;
+
+ /* State the members must be at to set the target */
+ PropertyVariantType _state;
+
+ /* Target for this action */
+ uint64_t _target;
+
+ /**
+ * @brief Parse and set the count
+ *
+ * @param[in] jsonObj - JSON object for the action
+ *
+ * Sets the number of members that must equal the state
+ */
+ void setCount(const json& jsonObj);
+
+ /**
+ * @brief Parse and set the state
+ *
+ * @param[in] jsonObj - JSON object for the action
+ *
+ * Sets the state for each member to equal to set the target
+ */
+ void setState(const json& jsonObj);
+
+ /**
+ * @brief Parse and set the target
+ *
+ * @param[in] jsonObj - JSON object for the action
+ *
+ * Sets the target to use when running the action
+ */
+ void setTarget(const json& jsonObj);
+};
+
+} // namespace phosphor::fan::control::json