Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 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 <iostream> |
| 18 | #include <libconfig.h++> |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <unordered_map> |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 21 | |
| 22 | /* Configuration. */ |
| 23 | #include "conf.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 24 | #include "sensors/builder.hpp" |
| 25 | #include "sensors/manager.hpp" |
| 26 | |
| 27 | /* |
| 28 | * If there's a configuration file, we build from that, and it requires special |
| 29 | * parsing. I should just ditch the compile-time version to reduce the |
| 30 | * probability of sync bugs. |
| 31 | */ |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 32 | SensorManager BuildSensorsFromConfig(const std::string& path) |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 33 | { |
| 34 | using namespace libconfig; |
| 35 | |
| 36 | std::map<std::string, struct sensor> config; |
| 37 | Config cfg; |
| 38 | |
| 39 | std::cerr << "entered BuildSensorsFromConfig\n"; |
| 40 | |
| 41 | /* The load was modeled after the example source provided. */ |
| 42 | try |
| 43 | { |
| 44 | cfg.readFile(path.c_str()); |
| 45 | } |
| 46 | catch (const FileIOException& fioex) |
| 47 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 48 | std::cerr << "I/O error while reading file: " << fioex.what() |
| 49 | << std::endl; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 50 | throw; |
| 51 | } |
| 52 | catch (const ParseException& pex) |
| 53 | { |
| 54 | std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() |
| 55 | << " - " << pex.getError() << std::endl; |
| 56 | throw; |
| 57 | } |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | const Setting& root = cfg.getRoot(); |
| 62 | |
| 63 | /* Grab the list of sensors and create them all */ |
| 64 | const Setting& sensors = root["sensors"]; |
| 65 | int count = sensors.getLength(); |
| 66 | |
| 67 | for (int i = 0; i < count; ++i) |
| 68 | { |
| 69 | const Setting& sensor = sensors[i]; |
| 70 | |
| 71 | std::string name; |
| 72 | struct sensor thisOne; |
| 73 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 74 | /* Not a super fan of using this library for run-time configuration. |
| 75 | */ |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 76 | name = sensor.lookup("name").c_str(); |
| 77 | thisOne.type = sensor.lookup("type").c_str(); |
| 78 | thisOne.readpath = sensor.lookup("readpath").c_str(); |
| 79 | thisOne.writepath = sensor.lookup("writepath").c_str(); |
| 80 | |
| 81 | /* TODO: Document why this is wonky. The library probably doesn't |
| 82 | * like int64_t |
| 83 | */ |
| 84 | int min = sensor.lookup("min"); |
| 85 | thisOne.min = static_cast<int64_t>(min); |
| 86 | int max = sensor.lookup("max"); |
| 87 | thisOne.max = static_cast<int64_t>(max); |
| 88 | int timeout = sensor.lookup("timeout"); |
| 89 | thisOne.timeout = static_cast<int64_t>(timeout); |
| 90 | |
| 91 | // leaving for verification for now. and yea the above is |
| 92 | // necessary. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 93 | std::cerr << "min: " << min << " max: " << max |
| 94 | << " savedmin: " << thisOne.min |
| 95 | << " savedmax: " << thisOne.max |
| 96 | << " timeout: " << thisOne.timeout << std::endl; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 97 | |
| 98 | config[name] = thisOne; |
| 99 | } |
| 100 | } |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 101 | catch (const SettingTypeException& setex) |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 102 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 103 | std::cerr << "Setting '" << setex.getPath() << "' type exception!" |
| 104 | << std::endl; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 105 | throw; |
| 106 | } |
| 107 | catch (const SettingNotFoundException& snex) |
| 108 | { |
| 109 | std::cerr << "Setting not found!" << std::endl; |
| 110 | throw; |
| 111 | } |
| 112 | |
| 113 | return BuildSensors(config); |
| 114 | } |