-
Notifications
You must be signed in to change notification settings - Fork 22
/
buildjson.cpp
124 lines (108 loc) · 2.98 KB
/
buildjson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sensors/buildjson.hpp"
#include "conf.hpp"
#include "sensors/sensor.hpp"
#include <nlohmann/json.hpp>
#include <cstdio>
using json = nlohmann::json;
namespace pid_control
{
namespace conf
{
void from_json(const json& j, conf::SensorConfig& s)
{
j.at("type").get_to(s.type);
j.at("readPath").get_to(s.readPath);
/* The writePath field is optional in a configuration */
auto writePath = j.find("writePath");
if (writePath == j.end())
{
s.writePath = "";
}
else
{
j.at("writePath").get_to(s.writePath);
}
/* Default to not ignore dbus MinValue/MaxValue - only used by passive
* sensors.
*/
s.ignoreDbusMinMax = false;
s.unavailableAsFailed = true;
s.min = 0;
s.max = 0;
auto ignore = j.find("ignoreDbusMinMax");
if (ignore != j.end())
{
j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
}
auto findunAsF = j.find("unavailableAsFailed");
if (findunAsF != j.end())
{
j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
}
/* The min field is optional in a configuration. */
auto min = j.find("min");
if (min != j.end())
{
if (s.type == "fan")
{
j.at("min").get_to(s.min);
}
else
{
std::fprintf(stderr, "Non-fan types ignore min value specified\n");
}
}
/* The max field is optional in a configuration. */
auto max = j.find("max");
if (max != j.end())
{
if (s.type == "fan")
{
j.at("max").get_to(s.max);
}
else
{
std::fprintf(stderr, "Non-fan types ignore max value specified\n");
}
}
/* The timeout field is optional in a configuration. */
auto timeout = j.find("timeout");
if (timeout == j.end())
{
s.timeout = Sensor::getDefaultTimeout(s.type);
}
else
{
j.at("timeout").get_to(s.timeout);
}
}
} // namespace conf
std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
{
std::map<std::string, conf::SensorConfig> config;
auto sensors = data["sensors"];
/* TODO: If no sensors, this is invalid, and we should except here or during
* parsing.
*/
for (const auto& sensor : sensors)
{
config[sensor["name"]] = sensor.get<conf::SensorConfig>();
}
return config;
}
} // namespace pid_control