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