blob: 4512f49117710b393d078943e109606c9f2b4227 [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
28Fan::Fan(sdbusplus::bus::bus& bus, const json& jsonObj) :
29 ConfigBase(jsonObj), _bus(bus)
Matthew Barthbff10802020-08-25 10:07:57 -050030{
31 setProfiles(jsonObj);
32 setZone(jsonObj);
33 setSensors(jsonObj);
34 setInterface(jsonObj);
35}
36
37void Fan::setProfiles(const json& jsonObj)
38{
39 _profiles = {};
40 if (jsonObj.contains("profiles"))
41 {
42 for (const auto& profile : jsonObj["profiles"])
43 {
44 _profiles.emplace_back(profile.get<std::string>());
45 }
46 }
47}
48
49void Fan::setZone(const json& jsonObj)
50{
51 if (!jsonObj.contains("zone"))
52 {
53 log<level::ERR>("Missing required fan zone",
54 entry("JSON=%s", jsonObj.dump().c_str()));
55 throw std::runtime_error("Missing required fan zone");
56 }
57 _zone = jsonObj["zone"].get<std::string>();
58}
59
60void Fan::setSensors(const json& jsonObj)
61{
62 if (!jsonObj.contains("sensors"))
63 {
64 log<level::ERR>("Missing required fan sensors list",
65 entry("JSON=%s", jsonObj.dump().c_str()));
66 throw std::runtime_error("Missing required fan sensors list");
67 }
68 for (const auto& sensor : jsonObj["sensors"])
69 {
70 _sensors.emplace_back(sensor.get<std::string>());
71 }
72}
73
74void Fan::setInterface(const json& jsonObj)
75{
76 if (!jsonObj.contains("target_interface"))
77 {
78 log<level::ERR>("Missing required fan sensor target interface",
79 entry("JSON=%s", jsonObj.dump().c_str()));
80 throw std::runtime_error(
81 "Missing required fan sensor target interface");
82 }
83 _interface = jsonObj["target_interface"].get<std::string>();
84}
Matthew Barthfcfa0522020-08-24 16:40:24 -050085
86} // namespace phosphor::fan::control::json