1eeeb867dSPatrick Venture /**
2eeeb867dSPatrick Venture * Copyright 2019 Google Inc.
3eeeb867dSPatrick Venture *
4eeeb867dSPatrick Venture * Licensed under the Apache License, Version 2.0 (the "License");
5eeeb867dSPatrick Venture * you may not use this file except in compliance with the License.
6eeeb867dSPatrick Venture * You may obtain a copy of the License at
7eeeb867dSPatrick Venture *
8eeeb867dSPatrick Venture * http://www.apache.org/licenses/LICENSE-2.0
9eeeb867dSPatrick Venture *
10eeeb867dSPatrick Venture * Unless required by applicable law or agreed to in writing, software
11eeeb867dSPatrick Venture * distributed under the License is distributed on an "AS IS" BASIS,
12eeeb867dSPatrick Venture * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eeeb867dSPatrick Venture * See the License for the specific language governing permissions and
14eeeb867dSPatrick Venture * limitations under the License.
15eeeb867dSPatrick Venture */
16eeeb867dSPatrick Venture
17eeeb867dSPatrick Venture #include "sensors/buildjson.hpp"
18eeeb867dSPatrick Venture
19eeeb867dSPatrick Venture #include "conf.hpp"
20eeeb867dSPatrick Venture #include "sensors/sensor.hpp"
21eeeb867dSPatrick Venture
22eeeb867dSPatrick Venture #include <nlohmann/json.hpp>
23eeeb867dSPatrick Venture
24a83a3eccSPatrick Venture #include <cstdio>
25a83a3eccSPatrick Venture
26eeeb867dSPatrick Venture using json = nlohmann::json;
27eeeb867dSPatrick Venture
28a076487aSPatrick Venture namespace pid_control
29a076487aSPatrick Venture {
30f81f2886SJames Feist namespace conf
31f81f2886SJames Feist {
from_json(const json & j,conf::SensorConfig & s)32f81f2886SJames Feist void from_json(const json& j, conf::SensorConfig& s)
33eeeb867dSPatrick Venture {
34eeeb867dSPatrick Venture j.at("type").get_to(s.type);
3569c51061SPatrick Venture j.at("readPath").get_to(s.readPath);
36eeeb867dSPatrick Venture
3769c51061SPatrick Venture /* The writePath field is optional in a configuration */
3869c51061SPatrick Venture auto writePath = j.find("writePath");
3969c51061SPatrick Venture if (writePath == j.end())
40eeeb867dSPatrick Venture {
4169c51061SPatrick Venture s.writePath = "";
42eeeb867dSPatrick Venture }
43eeeb867dSPatrick Venture else
44eeeb867dSPatrick Venture {
4569c51061SPatrick Venture j.at("writePath").get_to(s.writePath);
46eeeb867dSPatrick Venture }
47eeeb867dSPatrick Venture
486b9f5999SPatrick Venture /* Default to not ignore dbus MinValue/MaxValue - only used by passive
496b9f5999SPatrick Venture * sensors.
506b9f5999SPatrick Venture */
516b9f5999SPatrick Venture s.ignoreDbusMinMax = false;
52*8f73ad76SAlex.Song s.unavailableAsFailed = true;
53c7ab57e9SPatrick Venture s.min = 0;
54c7ab57e9SPatrick Venture s.max = 0;
55c7ab57e9SPatrick Venture
566b9f5999SPatrick Venture auto ignore = j.find("ignoreDbusMinMax");
576b9f5999SPatrick Venture if (ignore != j.end())
586b9f5999SPatrick Venture {
596b9f5999SPatrick Venture j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
606b9f5999SPatrick Venture }
616b9f5999SPatrick Venture
62*8f73ad76SAlex.Song auto findunAsF = j.find("unavailableAsFailed");
63*8f73ad76SAlex.Song if (findunAsF != j.end())
64*8f73ad76SAlex.Song {
65*8f73ad76SAlex.Song j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
66*8f73ad76SAlex.Song }
67*8f73ad76SAlex.Song
68eeeb867dSPatrick Venture /* The min field is optional in a configuration. */
69eeeb867dSPatrick Venture auto min = j.find("min");
70c7ab57e9SPatrick Venture if (min != j.end())
71eeeb867dSPatrick Venture {
7235906cc3SPatrick Venture if (s.type == "fan")
7335906cc3SPatrick Venture {
74eeeb867dSPatrick Venture j.at("min").get_to(s.min);
75eeeb867dSPatrick Venture }
7635906cc3SPatrick Venture else
7735906cc3SPatrick Venture {
7835906cc3SPatrick Venture std::fprintf(stderr, "Non-fan types ignore min value specified\n");
7935906cc3SPatrick Venture }
8035906cc3SPatrick Venture }
81eeeb867dSPatrick Venture
82eeeb867dSPatrick Venture /* The max field is optional in a configuration. */
83eeeb867dSPatrick Venture auto max = j.find("max");
84c7ab57e9SPatrick Venture if (max != j.end())
85eeeb867dSPatrick Venture {
8635906cc3SPatrick Venture if (s.type == "fan")
8735906cc3SPatrick Venture {
88eeeb867dSPatrick Venture j.at("max").get_to(s.max);
89eeeb867dSPatrick Venture }
9035906cc3SPatrick Venture else
9135906cc3SPatrick Venture {
9235906cc3SPatrick Venture std::fprintf(stderr, "Non-fan types ignore max value specified\n");
9335906cc3SPatrick Venture }
9435906cc3SPatrick Venture }
95eeeb867dSPatrick Venture
96eeeb867dSPatrick Venture /* The timeout field is optional in a configuration. */
97eeeb867dSPatrick Venture auto timeout = j.find("timeout");
98eeeb867dSPatrick Venture if (timeout == j.end())
99eeeb867dSPatrick Venture {
100eeeb867dSPatrick Venture s.timeout = Sensor::getDefaultTimeout(s.type);
101eeeb867dSPatrick Venture }
102eeeb867dSPatrick Venture else
103eeeb867dSPatrick Venture {
104eeeb867dSPatrick Venture j.at("timeout").get_to(s.timeout);
105eeeb867dSPatrick Venture }
106eeeb867dSPatrick Venture }
107f81f2886SJames Feist } // namespace conf
108eeeb867dSPatrick Venture
buildSensorsFromJson(const json & data)1091df9e879SPatrick Venture std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
110eeeb867dSPatrick Venture {
1111df9e879SPatrick Venture std::map<std::string, conf::SensorConfig> config;
112eeeb867dSPatrick Venture auto sensors = data["sensors"];
113eeeb867dSPatrick Venture
1146f59cf25SPatrick Venture /* TODO: If no sensors, this is invalid, and we should except here or during
1156f59cf25SPatrick Venture * parsing.
1166f59cf25SPatrick Venture */
117eeeb867dSPatrick Venture for (const auto& sensor : sensors)
118eeeb867dSPatrick Venture {
1191df9e879SPatrick Venture config[sensor["name"]] = sensor.get<conf::SensorConfig>();
120eeeb867dSPatrick Venture }
121eeeb867dSPatrick Venture
122eeeb867dSPatrick Venture return config;
123eeeb867dSPatrick Venture }
124a076487aSPatrick Venture } // namespace pid_control
125