blob: 7c4c7d6f811152c8403f81c283f88e21a36748d5 [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
26void from_json(const json& j, SensorConfig& s)
27{
28 j.at("type").get_to(s.type);
29 j.at("readpath").get_to(s.readpath);
30
31 /* The writepath field is optional in a configuration */
32 auto writepath = j.find("writepath");
33 if (writepath == j.end())
34 {
35 s.writepath = "";
36 }
37 else
38 {
39 j.at("writepath").get_to(s.writepath);
40 }
41
42 /* The min field is optional in a configuration. */
43 auto min = j.find("min");
44 if (min == j.end())
45 {
46 s.min = 0;
47 }
48 else
49 {
50 j.at("min").get_to(s.min);
51 }
52
53 /* The max field is optional in a configuration. */
54 auto max = j.find("max");
55 if (max == j.end())
56 {
57 s.max = 0;
58 }
59 else
60 {
61 j.at("max").get_to(s.max);
62 }
63
64 /* The timeout field is optional in a configuration. */
65 auto timeout = j.find("timeout");
66 if (timeout == j.end())
67 {
68 s.timeout = Sensor::getDefaultTimeout(s.type);
69 }
70 else
71 {
72 j.at("timeout").get_to(s.timeout);
73 }
74}
75
76std::map<std::string, struct SensorConfig>
77 buildSensorsFromJson(const json& data)
78{
79 std::map<std::string, struct SensorConfig> config;
80 auto sensors = data["sensors"];
81
82 for (const auto& sensor : sensors)
83 {
84 config[sensor["name"]] = sensor.get<struct SensorConfig>();
85 }
86
87 return config;
88}