blob: f5449ebc72862be0fd8e015f6405b698db7f3665 [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
24using json = nlohmann::json;
25
James Feistf81f2882019-02-26 11:26:36 -080026namespace conf
27{
28void from_json(const json& j, conf::SensorConfig& s)
Patrick Ventureeeeb8672019-02-08 11:47:42 -080029{
30 j.at("type").get_to(s.type);
Patrick Venture69c51062019-02-11 09:46:03 -080031 j.at("readPath").get_to(s.readPath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080032
Patrick Venture69c51062019-02-11 09:46:03 -080033 /* The writePath field is optional in a configuration */
34 auto writePath = j.find("writePath");
35 if (writePath == j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080036 {
Patrick Venture69c51062019-02-11 09:46:03 -080037 s.writePath = "";
Patrick Ventureeeeb8672019-02-08 11:47:42 -080038 }
39 else
40 {
Patrick Venture69c51062019-02-11 09:46:03 -080041 j.at("writePath").get_to(s.writePath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080042 }
43
Patrick Venturec7ab57e2019-08-29 09:35:19 -070044 s.min = 0;
45 s.max = 0;
46
Patrick Ventureeeeb8672019-02-08 11:47:42 -080047 /* The min field is optional in a configuration. */
48 auto min = j.find("min");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070049 if (min != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080050 {
51 j.at("min").get_to(s.min);
52 }
53
54 /* The max field is optional in a configuration. */
55 auto max = j.find("max");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070056 if (max != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080057 {
58 j.at("max").get_to(s.max);
59 }
60
61 /* The timeout field is optional in a configuration. */
62 auto timeout = j.find("timeout");
63 if (timeout == j.end())
64 {
65 s.timeout = Sensor::getDefaultTimeout(s.type);
66 }
67 else
68 {
69 j.at("timeout").get_to(s.timeout);
70 }
71}
James Feistf81f2882019-02-26 11:26:36 -080072} // namespace conf
Patrick Ventureeeeb8672019-02-08 11:47:42 -080073
James Feistf81f2882019-02-26 11:26:36 -080074std::map<std::string, struct conf::SensorConfig>
Patrick Ventureeeeb8672019-02-08 11:47:42 -080075 buildSensorsFromJson(const json& data)
76{
James Feistf81f2882019-02-26 11:26:36 -080077 std::map<std::string, struct conf::SensorConfig> config;
Patrick Ventureeeeb8672019-02-08 11:47:42 -080078 auto sensors = data["sensors"];
79
Patrick Venture6f59cf22019-02-08 14:59:44 -080080 /* TODO: If no sensors, this is invalid, and we should except here or during
81 * parsing.
82 */
Patrick Ventureeeeb8672019-02-08 11:47:42 -080083 for (const auto& sensor : sensors)
84 {
James Feistf81f2882019-02-26 11:26:36 -080085 config[sensor["name"]] = sensor.get<struct conf::SensorConfig>();
Patrick Ventureeeeb8672019-02-08 11:47:42 -080086 }
87
88 return config;
89}