blob: 8d34b1804d19d9a358f7d6323b6053df4c67c349 [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 Bishopb9e2b072016-12-19 13:47:10 -050025MainLoop::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 Bishopd499ca62016-12-19 09:24:50 -050033{
34
35}
36
37void MainLoop::shutdown() noexcept
38{
39 _shutdown = true;
40}
41
42void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -050043{
44 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -050045 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -050046 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 Bishopd499ca62016-12-19 09:24:50 -050052 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -050053 {
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 Bishopd499ca62016-12-19 09:24:50 -050061 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -050062 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 Bishope55ef3d2016-12-19 09:12:40 -050087}
88
89// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4