blob: 2bfc143c6691a1596b85726a72a5c719134aedc5 [file] [log] [blame]
Patrick Ventureeeeb8672019-02-08 11:47:42 -08001/**
2 * Copyright 2019 Google Inc.
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
17#include "sensors/buildjson.hpp"
18
19#include "conf.hpp"
20#include "sensors/sensor.hpp"
21
22#include <nlohmann/json.hpp>
23
Patrick Venturea83a3ec2020-08-04 09:52:05 -070024#include <cstdio>
25
Patrick Ventureeeeb8672019-02-08 11:47:42 -080026using json = nlohmann::json;
27
Patrick Venturea0764872020-08-08 07:48:43 -070028namespace pid_control
29{
James Feistf81f2882019-02-26 11:26:36 -080030namespace conf
31{
32void from_json(const json& j, conf::SensorConfig& s)
Patrick Ventureeeeb8672019-02-08 11:47:42 -080033{
34 j.at("type").get_to(s.type);
Patrick Venture69c51062019-02-11 09:46:03 -080035 j.at("readPath").get_to(s.readPath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080036
Patrick Venture69c51062019-02-11 09:46:03 -080037 /* The writePath field is optional in a configuration */
38 auto writePath = j.find("writePath");
39 if (writePath == j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080040 {
Patrick Venture69c51062019-02-11 09:46:03 -080041 s.writePath = "";
Patrick Ventureeeeb8672019-02-08 11:47:42 -080042 }
43 else
44 {
Patrick Venture69c51062019-02-11 09:46:03 -080045 j.at("writePath").get_to(s.writePath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080046 }
47
Patrick Venture6b9f5992019-09-10 09:18:28 -070048 /* Default to not ignore dbus MinValue/MaxValue - only used by passive
49 * sensors.
50 */
51 s.ignoreDbusMinMax = false;
Patrick Venturec7ab57e2019-08-29 09:35:19 -070052 s.min = 0;
53 s.max = 0;
54
Patrick Venture6b9f5992019-09-10 09:18:28 -070055 auto ignore = j.find("ignoreDbusMinMax");
56 if (ignore != j.end())
57 {
58 j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
59 }
60
Patrick Ventureeeeb8672019-02-08 11:47:42 -080061 /* The min field is optional in a configuration. */
62 auto min = j.find("min");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070063 if (min != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080064 {
Patrick Venture35906cc2019-08-29 10:06:29 -070065 if (s.type == "fan")
66 {
67 j.at("min").get_to(s.min);
68 }
69 else
70 {
71 std::fprintf(stderr, "Non-fan types ignore min value specified\n");
72 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080073 }
74
75 /* The max field is optional in a configuration. */
76 auto max = j.find("max");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070077 if (max != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080078 {
Patrick Venture35906cc2019-08-29 10:06:29 -070079 if (s.type == "fan")
80 {
81 j.at("max").get_to(s.max);
82 }
83 else
84 {
85 std::fprintf(stderr, "Non-fan types ignore max value specified\n");
86 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080087 }
88
89 /* The timeout field is optional in a configuration. */
90 auto timeout = j.find("timeout");
91 if (timeout == j.end())
92 {
93 s.timeout = Sensor::getDefaultTimeout(s.type);
94 }
95 else
96 {
97 j.at("timeout").get_to(s.timeout);
98 }
99}
James Feistf81f2882019-02-26 11:26:36 -0800100} // namespace conf
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800101
James Feistf81f2882019-02-26 11:26:36 -0800102std::map<std::string, struct conf::SensorConfig>
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800103 buildSensorsFromJson(const json& data)
104{
James Feistf81f2882019-02-26 11:26:36 -0800105 std::map<std::string, struct conf::SensorConfig> config;
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800106 auto sensors = data["sensors"];
107
Patrick Venture6f59cf22019-02-08 14:59:44 -0800108 /* TODO: If no sensors, this is invalid, and we should except here or during
109 * parsing.
110 */
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800111 for (const auto& sensor : sensors)
112 {
James Feistf81f2882019-02-26 11:26:36 -0800113 config[sensor["name"]] = sensor.get<struct conf::SensorConfig>();
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800114 }
115
116 return config;
117}
Patrick Venturea0764872020-08-08 07:48:43 -0700118} // namespace pid_control