Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 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 | #include <iostream> |
| 17 | #include <memory> |
| 18 | #include <thread> |
| 19 | #include "sensorset.hpp" |
| 20 | #include "sensorcache.hpp" |
| 21 | #include "hwmon.hpp" |
| 22 | #include "sysfs.hpp" |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 23 | #include "mainloop.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 24 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame^] | 25 | MainLoop::MainLoop( |
| 26 | const std::string& path, |
| 27 | const char* prefix, |
| 28 | const char* root) |
| 29 | : _shutdown(false), |
| 30 | _path(path), |
| 31 | _prefix(prefix), |
| 32 | _root(root) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | void MainLoop::shutdown() noexcept |
| 38 | { |
| 39 | _shutdown = true; |
| 40 | } |
| 41 | |
| 42 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 43 | { |
| 44 | // Check sysfs for available sensors. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 45 | auto sensors = std::make_unique<SensorSet>(_path); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 46 | auto sensor_cache = std::make_unique<SensorCache>(); |
| 47 | |
| 48 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 49 | // ensure the objects all exist? |
| 50 | |
| 51 | // Polling loop. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 52 | while (!_shutdown) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 53 | { |
| 54 | // Iterate through all the sensors. |
| 55 | for (auto& i : *sensors) |
| 56 | { |
| 57 | if (i.second.find(hwmon::entry::input) != i.second.end()) |
| 58 | { |
| 59 | // Read value from sensor. |
| 60 | int value = 0; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 61 | read_sysfs(make_sysfs_path(_path, |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 62 | i.first.first, i.first.second, |
| 63 | hwmon::entry::input), |
| 64 | value); |
| 65 | |
| 66 | // Update sensor cache. |
| 67 | if (sensor_cache->update(i.first, value)) |
| 68 | { |
| 69 | // TODO: Issue#4 - dbus event here. |
| 70 | std::cout << i.first.first << i.first.second << " = " |
| 71 | << value << std::endl; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Sleep until next interval. |
| 77 | // TODO: Issue#5 - Make this configurable. |
| 78 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
| 79 | { |
| 80 | using namespace std::literals::chrono_literals; |
| 81 | std::this_thread::sleep_for(1s); |
| 82 | } |
| 83 | |
| 84 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 85 | // for new entries. |
| 86 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |