blob: 012b993bfba16e5073393258d1f9c0e6167ebeba [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001/**
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 Bishopd499ca62016-12-19 09:24:50 -050023#include "mainloop.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050024
Brad Bishopd499ca62016-12-19 09:24:50 -050025MainLoop::MainLoop(const std::string& path)
26 : _shutdown(false), _path(path)
27{
28
29}
30
31void MainLoop::shutdown() noexcept
32{
33 _shutdown = true;
34}
35
36void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -050037{
38 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -050039 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -050040 auto sensor_cache = std::make_unique<SensorCache>();
41
42 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
43 // ensure the objects all exist?
44
45 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -050046 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -050047 {
48 // Iterate through all the sensors.
49 for (auto& i : *sensors)
50 {
51 if (i.second.find(hwmon::entry::input) != i.second.end())
52 {
53 // Read value from sensor.
54 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -050055 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -050056 i.first.first, i.first.second,
57 hwmon::entry::input),
58 value);
59
60 // Update sensor cache.
61 if (sensor_cache->update(i.first, value))
62 {
63 // TODO: Issue#4 - dbus event here.
64 std::cout << i.first.first << i.first.second << " = "
65 << value << std::endl;
66 }
67 }
68 }
69
70 // Sleep until next interval.
71 // TODO: Issue#5 - Make this configurable.
72 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
73 {
74 using namespace std::literals::chrono_literals;
75 std::this_thread::sleep_for(1s);
76 }
77
78 // TODO: Issue#7 - Should probably periodically check the SensorSet
79 // for new entries.
80 }
Brad Bishope55ef3d2016-12-19 09:12:40 -050081}
82
83// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4