Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 1 | /** |
| 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 Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 24 | #include <cstdio> |
| 25 | |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 26 | using json = nlohmann::json; |
| 27 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 28 | namespace conf |
| 29 | { |
| 30 | void from_json(const json& j, conf::SensorConfig& s) |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 31 | { |
| 32 | j.at("type").get_to(s.type); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 33 | j.at("readPath").get_to(s.readPath); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 34 | |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 35 | /* The writePath field is optional in a configuration */ |
| 36 | auto writePath = j.find("writePath"); |
| 37 | if (writePath == j.end()) |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 38 | { |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 39 | s.writePath = ""; |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 40 | } |
| 41 | else |
| 42 | { |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 43 | j.at("writePath").get_to(s.writePath); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 46 | /* Default to not ignore dbus MinValue/MaxValue - only used by passive |
| 47 | * sensors. |
| 48 | */ |
| 49 | s.ignoreDbusMinMax = false; |
Patrick Venture | c7ab57e | 2019-08-29 09:35:19 -0700 | [diff] [blame] | 50 | s.min = 0; |
| 51 | s.max = 0; |
| 52 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 53 | auto ignore = j.find("ignoreDbusMinMax"); |
| 54 | if (ignore != j.end()) |
| 55 | { |
| 56 | j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax); |
| 57 | } |
| 58 | |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 59 | /* The min field is optional in a configuration. */ |
| 60 | auto min = j.find("min"); |
Patrick Venture | c7ab57e | 2019-08-29 09:35:19 -0700 | [diff] [blame] | 61 | if (min != j.end()) |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 62 | { |
Patrick Venture | 35906cc | 2019-08-29 10:06:29 -0700 | [diff] [blame] | 63 | if (s.type == "fan") |
| 64 | { |
| 65 | j.at("min").get_to(s.min); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | std::fprintf(stderr, "Non-fan types ignore min value specified\n"); |
| 70 | } |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* The max field is optional in a configuration. */ |
| 74 | auto max = j.find("max"); |
Patrick Venture | c7ab57e | 2019-08-29 09:35:19 -0700 | [diff] [blame] | 75 | if (max != j.end()) |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 76 | { |
Patrick Venture | 35906cc | 2019-08-29 10:06:29 -0700 | [diff] [blame] | 77 | if (s.type == "fan") |
| 78 | { |
| 79 | j.at("max").get_to(s.max); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | std::fprintf(stderr, "Non-fan types ignore max value specified\n"); |
| 84 | } |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* The timeout field is optional in a configuration. */ |
| 88 | auto timeout = j.find("timeout"); |
| 89 | if (timeout == j.end()) |
| 90 | { |
| 91 | s.timeout = Sensor::getDefaultTimeout(s.type); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | j.at("timeout").get_to(s.timeout); |
| 96 | } |
| 97 | } |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 98 | } // namespace conf |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 99 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 100 | std::map<std::string, struct conf::SensorConfig> |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 101 | buildSensorsFromJson(const json& data) |
| 102 | { |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 103 | std::map<std::string, struct conf::SensorConfig> config; |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 104 | auto sensors = data["sensors"]; |
| 105 | |
Patrick Venture | 6f59cf2 | 2019-02-08 14:59:44 -0800 | [diff] [blame] | 106 | /* TODO: If no sensors, this is invalid, and we should except here or during |
| 107 | * parsing. |
| 108 | */ |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 109 | for (const auto& sensor : sensors) |
| 110 | { |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 111 | config[sensor["name"]] = sensor.get<struct conf::SensorConfig>(); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return config; |
| 115 | } |