control: Complete fan configuration parsing
Fan control's complete configuration includes a configuration of the
fan(s) that will be controlled. These fan(s) are made up of
configuration attributes that determine what zones they are included in
and how to interface with their sensor(s) that control their speeds.
Tested:
Fan object created for each entry within "fans.json"
A "fans.json" configuration is required
Each supported attribute is parsed and stored on each fan object
Change-Id: Ic5db2a685d672a6f5fb642a1dc0ec67cc6ac16a3
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/fan.cpp b/control/json/fan.cpp
index b2f152c..4512f49 100644
--- a/control/json/fan.cpp
+++ b/control/json/fan.cpp
@@ -16,15 +16,71 @@
#include "fan.hpp"
#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
namespace phosphor::fan::control::json
{
using json = nlohmann::json;
+using namespace phosphor::logging;
Fan::Fan(sdbusplus::bus::bus& bus, const json& jsonObj) :
ConfigBase(jsonObj), _bus(bus)
-{}
+{
+ setProfiles(jsonObj);
+ setZone(jsonObj);
+ setSensors(jsonObj);
+ setInterface(jsonObj);
+}
+
+void Fan::setProfiles(const json& jsonObj)
+{
+ _profiles = {};
+ if (jsonObj.contains("profiles"))
+ {
+ for (const auto& profile : jsonObj["profiles"])
+ {
+ _profiles.emplace_back(profile.get<std::string>());
+ }
+ }
+}
+
+void Fan::setZone(const json& jsonObj)
+{
+ if (!jsonObj.contains("zone"))
+ {
+ log<level::ERR>("Missing required fan zone",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required fan zone");
+ }
+ _zone = jsonObj["zone"].get<std::string>();
+}
+
+void Fan::setSensors(const json& jsonObj)
+{
+ if (!jsonObj.contains("sensors"))
+ {
+ log<level::ERR>("Missing required fan sensors list",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required fan sensors list");
+ }
+ for (const auto& sensor : jsonObj["sensors"])
+ {
+ _sensors.emplace_back(sensor.get<std::string>());
+ }
+}
+
+void Fan::setInterface(const json& jsonObj)
+{
+ if (!jsonObj.contains("target_interface"))
+ {
+ log<level::ERR>("Missing required fan sensor target interface",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error(
+ "Missing required fan sensor target interface");
+ }
+ _interface = jsonObj["target_interface"].get<std::string>();
+}
} // namespace phosphor::fan::control::json