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 #include <cstdio> 25 #include <map> 26 27 using json = nlohmann::json; 28 29 namespace pid_control 30 { 31 namespace conf 32 { 33 void from_json(const json& j, conf::SensorConfig& s) 34 { 35 j.at("type").get_to(s.type); 36 j.at("readPath").get_to(s.readPath); 37 38 /* The writePath field is optional in a configuration */ 39 auto writePath = j.find("writePath"); 40 if (writePath == j.end()) 41 { 42 s.writePath = ""; 43 } 44 else 45 { 46 j.at("writePath").get_to(s.writePath); 47 } 48 49 /* Default to not ignore dbus MinValue/MaxValue - only used by passive 50 * sensors. 51 */ 52 s.ignoreDbusMinMax = false; 53 s.unavailableAsFailed = true; 54 s.min = 0; 55 s.max = 0; 56 57 auto ignore = j.find("ignoreDbusMinMax"); 58 if (ignore != j.end()) 59 { 60 j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax); 61 } 62 63 auto findunAsF = j.find("unavailableAsFailed"); 64 if (findunAsF != j.end()) 65 { 66 j.at("unavailableAsFailed").get_to(s.unavailableAsFailed); 67 } 68 69 /* The min field is optional in a configuration. */ 70 auto min = j.find("min"); 71 if (min != j.end()) 72 { 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 } 81 } 82 83 /* The max field is optional in a configuration. */ 84 auto max = j.find("max"); 85 if (max != j.end()) 86 { 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 } 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 } 108 } // namespace conf 109 110 std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data) 111 { 112 std::map<std::string, conf::SensorConfig> config; 113 auto sensors = data["sensors"]; 114 115 /* TODO: If no sensors, this is invalid, and we should except here or during 116 * parsing. 117 */ 118 for (const auto& sensor : sensors) 119 { 120 config[sensor["name"]] = sensor.get<conf::SensorConfig>(); 121 } 122 123 return config; 124 } 125 } // namespace pid_control 126