1 /**
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>
19 #include <phosphor-logging/log.hpp>
20 #include <sdbusplus/bus.hpp>
21 
22 namespace phosphor::fan::control::json
23 {
24 
25 using json = nlohmann::json;
26 using namespace phosphor::logging;
27 
28 Fan::Fan(sdbusplus::bus::bus& bus, const json& jsonObj) : ConfigBase(jsonObj)
29 {
30     if (jsonObj.contains("profiles"))
31     {
32         for (const auto& profile : jsonObj["profiles"])
33         {
34             _profiles.emplace_back(profile.get<std::string>());
35         }
36     }
37     setZone(jsonObj);
38     setSensors(jsonObj);
39     setInterface(jsonObj);
40 }
41 
42 void 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 
53 void 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 
67 void 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 }
78 
79 } // namespace phosphor::fan::control::json
80