165dfe1eaSMatt Spinler /** 265dfe1eaSMatt Spinler * Copyright © 2021 IBM Corporation 365dfe1eaSMatt Spinler * 465dfe1eaSMatt Spinler * Licensed under the Apache License, Version 2.0 (the "License"); 565dfe1eaSMatt Spinler * you may not use this file except in compliance with the License. 665dfe1eaSMatt Spinler * You may obtain a copy of the License at 765dfe1eaSMatt Spinler * 865dfe1eaSMatt Spinler * http://www.apache.org/licenses/LICENSE-2.0 965dfe1eaSMatt Spinler * 1065dfe1eaSMatt Spinler * Unless required by applicable law or agreed to in writing, software 1165dfe1eaSMatt Spinler * distributed under the License is distributed on an "AS IS" BASIS, 1265dfe1eaSMatt Spinler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365dfe1eaSMatt Spinler * See the License for the specific language governing permissions and 1465dfe1eaSMatt Spinler * limitations under the License. 1565dfe1eaSMatt Spinler */ 1665dfe1eaSMatt Spinler #pragma once 1765dfe1eaSMatt Spinler 1865dfe1eaSMatt Spinler #include "../zone.hpp" 1965dfe1eaSMatt Spinler #include "action.hpp" 2065dfe1eaSMatt Spinler #include "group.hpp" 2165dfe1eaSMatt Spinler 2265dfe1eaSMatt Spinler #include <nlohmann/json.hpp> 2365dfe1eaSMatt Spinler 2465dfe1eaSMatt Spinler namespace phosphor::fan::control::json 2565dfe1eaSMatt Spinler { 2665dfe1eaSMatt Spinler 2765dfe1eaSMatt Spinler using json = nlohmann::json; 2865dfe1eaSMatt Spinler 2965dfe1eaSMatt Spinler /** 3065dfe1eaSMatt Spinler * @class CountStateFloor - Action to set a floor when members are at a state 3165dfe1eaSMatt Spinler * 3265dfe1eaSMatt Spinler * Sets the fans to a configured floor when a number of members within the 3365dfe1eaSMatt Spinler * group are at a configured state. Once the number of members at the given 3465dfe1eaSMatt Spinler * state falls below the configured count, the floor hold is released. 3565dfe1eaSMatt Spinler * 3665dfe1eaSMatt Spinler * Example JSON: 3765dfe1eaSMatt Spinler * { 3865dfe1eaSMatt Spinler * "name": "count_state_floor", 3965dfe1eaSMatt Spinler * "count": 1, 4065dfe1eaSMatt Spinler * "state": false, 4165dfe1eaSMatt Spinler * "floor": 5000 4265dfe1eaSMatt Spinler * } 43*7fa4d16eSMatt Spinler * 44*7fa4d16eSMatt Spinler * There is an optional 'delay' field that that prevents the floor from 45*7fa4d16eSMatt Spinler * being changed until the count has been met for that amount of time. 4665dfe1eaSMatt Spinler */ 4765dfe1eaSMatt Spinler class CountStateFloor : 4865dfe1eaSMatt Spinler public ActionBase, 4965dfe1eaSMatt Spinler public ActionRegister<CountStateFloor> 5065dfe1eaSMatt Spinler { 5165dfe1eaSMatt Spinler public: 5265dfe1eaSMatt Spinler /* Name of this action */ 5365dfe1eaSMatt Spinler static constexpr auto name = "count_state_floor"; 5465dfe1eaSMatt Spinler 5565dfe1eaSMatt Spinler CountStateFloor() = delete; 5665dfe1eaSMatt Spinler CountStateFloor(const CountStateFloor&) = delete; 5765dfe1eaSMatt Spinler CountStateFloor(CountStateFloor&&) = delete; 5865dfe1eaSMatt Spinler CountStateFloor& operator=(const CountStateFloor&) = delete; 5965dfe1eaSMatt Spinler CountStateFloor& operator=(CountStateFloor&&) = delete; 6065dfe1eaSMatt Spinler ~CountStateFloor() = default; 6165dfe1eaSMatt Spinler 6265dfe1eaSMatt Spinler /** 6365dfe1eaSMatt Spinler * @brief Set floor when a number of members are at a given state 6465dfe1eaSMatt Spinler * 6565dfe1eaSMatt Spinler * @param[in] jsonObj - JSON configuration of this action 6665dfe1eaSMatt Spinler * @param[in] groups - Groups of dbus objects the action uses 6765dfe1eaSMatt Spinler */ 6865dfe1eaSMatt Spinler CountStateFloor(const json& jsonObj, const std::vector<Group>& groups); 6965dfe1eaSMatt Spinler 7065dfe1eaSMatt Spinler /** 7165dfe1eaSMatt Spinler * @brief Run the action 7265dfe1eaSMatt Spinler * 7365dfe1eaSMatt Spinler * Counts the number of members within a group are equal to a given state 7465dfe1eaSMatt Spinler * and when the number of members are at or above the given state, the 7565dfe1eaSMatt Spinler * zone is set to the configured floor, with a hold. The hold is released 7665dfe1eaSMatt Spinler * when the number of members goes below the configured count. 7765dfe1eaSMatt Spinler * 7865dfe1eaSMatt Spinler * @param[in] zone - Zone to run the action on 7965dfe1eaSMatt Spinler */ 8065dfe1eaSMatt Spinler void run(Zone& zone) override; 8165dfe1eaSMatt Spinler 8265dfe1eaSMatt Spinler private: 8365dfe1eaSMatt Spinler /** 8465dfe1eaSMatt Spinler * @brief Parse and set the count 8565dfe1eaSMatt Spinler * 8665dfe1eaSMatt Spinler * @param[in] jsonObj - JSON object for the action 8765dfe1eaSMatt Spinler * 8865dfe1eaSMatt Spinler * Sets the number of members that must equal the state 8965dfe1eaSMatt Spinler */ 9065dfe1eaSMatt Spinler void setCount(const json& jsonObj); 9165dfe1eaSMatt Spinler 9265dfe1eaSMatt Spinler /** 9365dfe1eaSMatt Spinler * @brief Parse and set the state 9465dfe1eaSMatt Spinler * 9565dfe1eaSMatt Spinler * @param[in] jsonObj - JSON object for the action 9665dfe1eaSMatt Spinler * 9765dfe1eaSMatt Spinler * Sets the state for each member to equal to set the floor 9865dfe1eaSMatt Spinler */ 9965dfe1eaSMatt Spinler void setState(const json& jsonObj); 10065dfe1eaSMatt Spinler 10165dfe1eaSMatt Spinler /** 10265dfe1eaSMatt Spinler * @brief Parse and set the floor 10365dfe1eaSMatt Spinler * 10465dfe1eaSMatt Spinler * @param[in] jsonObj - JSON object for the action 10565dfe1eaSMatt Spinler * 10665dfe1eaSMatt Spinler * Sets the floor to use when running the action 10765dfe1eaSMatt Spinler */ 10865dfe1eaSMatt Spinler void setFloor(const json& jsonObj); 10965dfe1eaSMatt Spinler 110*7fa4d16eSMatt Spinler /** 111*7fa4d16eSMatt Spinler * @brief Parse and set the delay 112*7fa4d16eSMatt Spinler * 113*7fa4d16eSMatt Spinler * @param[in] jsonObj - JSON object for the action 114*7fa4d16eSMatt Spinler */ 115*7fa4d16eSMatt Spinler void setDelayTime(const json& jsonObj); 116*7fa4d16eSMatt Spinler 117*7fa4d16eSMatt Spinler /** 118*7fa4d16eSMatt Spinler * @brief Counts the number of members that are equal to the state. 119*7fa4d16eSMatt Spinler * 120*7fa4d16eSMatt Spinler * @return bool - If the count is reached or not. 121*7fa4d16eSMatt Spinler */ 122*7fa4d16eSMatt Spinler bool doCount(); 123*7fa4d16eSMatt Spinler 12465dfe1eaSMatt Spinler /* Number of group members */ 12565dfe1eaSMatt Spinler size_t _count; 12665dfe1eaSMatt Spinler 12765dfe1eaSMatt Spinler /* State the members must be at to set the floor */ 12865dfe1eaSMatt Spinler PropertyVariantType _state; 12965dfe1eaSMatt Spinler 13065dfe1eaSMatt Spinler /* Floor for this action */ 13165dfe1eaSMatt Spinler uint64_t _floor; 132*7fa4d16eSMatt Spinler 133*7fa4d16eSMatt Spinler /* Amount of time the count has to be met */ 134*7fa4d16eSMatt Spinler std::chrono::seconds _delayTime{0}; 135*7fa4d16eSMatt Spinler 136*7fa4d16eSMatt Spinler /* Timer used for checking the delay */ 137*7fa4d16eSMatt Spinler std::unique_ptr<Timer> _timer; 13865dfe1eaSMatt Spinler }; 13965dfe1eaSMatt Spinler 14065dfe1eaSMatt Spinler } // namespace phosphor::fan::control::json 141