blob: 7a65da16b07a2ac8d2d6d6ca64bdd62ffda45a0a [file] [log] [blame]
Matthew Barthfcfa0522020-08-24 16:40:24 -05001/**
2 * Copyright © 2020 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 "fan.hpp"
17
18#include <nlohmann/json.hpp>
Matthew Barthbff10802020-08-25 10:07:57 -050019#include <phosphor-logging/log.hpp>
Matthew Barthfcfa0522020-08-24 16:40:24 -050020#include <sdbusplus/bus.hpp>
21
22namespace phosphor::fan::control::json
23{
24
25using json = nlohmann::json;
Matthew Barthbff10802020-08-25 10:07:57 -050026using namespace phosphor::logging;
Matthew Barthfcfa0522020-08-24 16:40:24 -050027
Matthew Barth08576c52020-08-27 11:07:55 -050028Fan::Fan(sdbusplus::bus::bus& bus, const json& jsonObj) : ConfigBase(jsonObj)
Matthew Barthbff10802020-08-25 10:07:57 -050029{
Matthew Barthbff10802020-08-25 10:07:57 -050030 if (jsonObj.contains("profiles"))
31 {
32 for (const auto& profile : jsonObj["profiles"])
33 {
34 _profiles.emplace_back(profile.get<std::string>());
35 }
36 }
Matthew Barthce957262020-08-27 10:35:57 -050037 setZone(jsonObj);
38 setSensors(jsonObj);
39 setInterface(jsonObj);
Matthew Barthbff10802020-08-25 10:07:57 -050040}
41
42void Fan::setZone(const json& jsonObj)
43{
44 if (!jsonObj.contains("zone"))
45 {
46 log<level::ERR>("Missing required fan zone",
47 entry("JSON=%s", jsonObj.dump().c_str()));
48 throw std::runtime_error("Missing required fan zone");
49 }
50 _zone = jsonObj["zone"].get<std::string>();
51}
52
53void Fan::setSensors(const json& jsonObj)
54{
55 if (!jsonObj.contains("sensors"))
56 {
57 log<level::ERR>("Missing required fan sensors list",
58 entry("JSON=%s", jsonObj.dump().c_str()));
59 throw std::runtime_error("Missing required fan sensors list");
60 }
61 for (const auto& sensor : jsonObj["sensors"])
62 {
63 _sensors.emplace_back(sensor.get<std::string>());
64 }
65}
66
67void Fan::setInterface(const json& jsonObj)
68{
69 if (!jsonObj.contains("target_interface"))
70 {
71 log<level::ERR>("Missing required fan sensor target interface",
72 entry("JSON=%s", jsonObj.dump().c_str()));
73 throw std::runtime_error(
74 "Missing required fan sensor target interface");
75 }
76 _interface = jsonObj["target_interface"].get<std::string>();
77}
Matthew Barthfcfa0522020-08-24 16:40:24 -050078
79} // namespace phosphor::fan::control::json