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 | |
| 24 | using json = nlohmann::json; |
| 25 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 26 | namespace conf |
| 27 | { |
| 28 | void from_json(const json& j, conf::SensorConfig& s) |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 29 | { |
| 30 | j.at("type").get_to(s.type); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 31 | j.at("readPath").get_to(s.readPath); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 32 | |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 33 | /* The writePath field is optional in a configuration */ |
| 34 | auto writePath = j.find("writePath"); |
| 35 | if (writePath == j.end()) |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 36 | { |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 37 | s.writePath = ""; |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 38 | } |
| 39 | else |
| 40 | { |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 41 | j.at("writePath").get_to(s.writePath); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* The min field is optional in a configuration. */ |
| 45 | auto min = j.find("min"); |
| 46 | if (min == j.end()) |
| 47 | { |
| 48 | s.min = 0; |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | j.at("min").get_to(s.min); |
| 53 | } |
| 54 | |
| 55 | /* The max field is optional in a configuration. */ |
| 56 | auto max = j.find("max"); |
| 57 | if (max == j.end()) |
| 58 | { |
| 59 | s.max = 0; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | j.at("max").get_to(s.max); |
| 64 | } |
| 65 | |
| 66 | /* The timeout field is optional in a configuration. */ |
| 67 | auto timeout = j.find("timeout"); |
| 68 | if (timeout == j.end()) |
| 69 | { |
| 70 | s.timeout = Sensor::getDefaultTimeout(s.type); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | j.at("timeout").get_to(s.timeout); |
| 75 | } |
| 76 | } |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 77 | } // namespace conf |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 78 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 79 | std::map<std::string, struct conf::SensorConfig> |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 80 | buildSensorsFromJson(const json& data) |
| 81 | { |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 82 | std::map<std::string, struct conf::SensorConfig> config; |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 83 | auto sensors = data["sensors"]; |
| 84 | |
Patrick Venture | 6f59cf2 | 2019-02-08 14:59:44 -0800 | [diff] [blame] | 85 | /* TODO: If no sensors, this is invalid, and we should except here or during |
| 86 | * parsing. |
| 87 | */ |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 88 | for (const auto& sensor : sensors) |
| 89 | { |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 90 | config[sensor["name"]] = sensor.get<struct conf::SensorConfig>(); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return config; |
| 94 | } |