Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 1 | /** |
| 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 "timer.hpp" |
| 17 | |
| 18 | #include "../manager.hpp" |
Matthew Barth | cd6f379 | 2021-09-30 15:13:25 -0500 | [diff] [blame^] | 19 | #include "group.hpp" |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 20 | #include "trigger_aliases.hpp" |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 21 | |
| 22 | #include <fmt/format.h> |
| 23 | |
| 24 | #include <nlohmann/json.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
| 26 | |
| 27 | #include <chrono> |
| 28 | |
| 29 | namespace phosphor::fan::control::json::trigger::timer |
| 30 | { |
| 31 | |
| 32 | using json = nlohmann::json; |
| 33 | using namespace phosphor::logging; |
| 34 | |
| 35 | TimerType getType(const json& jsonObj) |
| 36 | { |
| 37 | if (!jsonObj.contains("type")) |
| 38 | { |
| 39 | log<level::ERR>("Missing required timer trigger type", |
| 40 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 41 | throw std::runtime_error("Missing required timer trigger type"); |
| 42 | } |
| 43 | auto type = jsonObj["type"].get<std::string>(); |
| 44 | if (type == "oneshot") |
| 45 | { |
| 46 | return TimerType::oneshot; |
| 47 | } |
| 48 | else if (type == "repeating") |
| 49 | { |
| 50 | return TimerType::repeating; |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | log<level::ERR>( |
| 55 | fmt::format("Timer trigger type '{}' is not supported", type) |
| 56 | .c_str(), |
| 57 | entry("AVAILABLE_TYPES={oneshot, repeating}")); |
| 58 | throw std::runtime_error("Unsupported timer trigger type given"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::chrono::microseconds getInterval(const json& jsonObj) |
| 63 | { |
| 64 | if (!jsonObj.contains("interval")) |
| 65 | { |
| 66 | log<level::ERR>("Missing required timer trigger interval", |
| 67 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 68 | throw std::runtime_error("Missing required timer trigger interval"); |
| 69 | } |
| 70 | return static_cast<std::chrono::microseconds>( |
| 71 | jsonObj["interval"].get<uint64_t>()); |
| 72 | } |
| 73 | |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 74 | enableTrigger triggerTimer(const json& jsonObj, const std::string& eventName, |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 75 | std::vector<std::unique_ptr<ActionBase>>& actions) |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 76 | { |
| 77 | // Get the type and interval of this timer from the JSON |
| 78 | auto type = getType(jsonObj); |
| 79 | auto interval = getInterval(jsonObj); |
| 80 | |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 81 | return [type = std::move(type), interval = std::move(interval)]( |
| 82 | const std::string& eventName, Manager* mgr, |
Matthew Barth | cd6f379 | 2021-09-30 15:13:25 -0500 | [diff] [blame^] | 83 | const std::vector<Group>&, |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 84 | std::vector<std::unique_ptr<ActionBase>>& actions) { |
| 85 | auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions)); |
| 86 | mgr->addTimer(type, interval, std::move(tpPtr)); |
| 87 | }; |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace phosphor::fan::control::json::trigger::timer |