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