move sensors to class

This commit is contained in:
2026-01-03 16:24:00 +01:00
parent 149a8f8b71
commit b8589181a7
2 changed files with 57 additions and 56 deletions

View File

@@ -16,7 +16,7 @@
#include <utils.hpp>
#include <vector>
void Application::hassThread(const std::stop_token& st, Sensors& s) {
void Application::hassThread(const std::stop_token& st) {
applog(LOG::START, "Thread started");
HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc");
@@ -24,33 +24,33 @@ void Application::hassThread(const std::stop_token& st, Sensors& s) {
while (!st.stop_requested()) {
pc_power.service();
{
std::lock_guard<std::mutex> lock(s.mtx);
s.hass_state = pc_power.state;
std::lock_guard<std::mutex> lock(m_sensors.mtx);
m_sensors.hass_state = pc_power.state;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void Application::sensorsThread(const std::stop_token& st, Sensors& s) {
void Application::sensorsThread(const std::stop_token& st) {
applog(LOG::START, "Thread started, poll interval {} ms", m_config.lcd_interval_ms);
cpu::detectHwmon(&s.cpu, m_config.cpu_pciaddr);
gpu::detectHwmon(&s.gpu, m_config.gpu_pciaddr);
sys::detectHwmon(&s.sys, m_config.sys_sensor);
s.home.mountpoint = m_config.home_dir;
cpu::detectHwmon(&m_sensors.cpu, m_config.cpu_pciaddr);
gpu::detectHwmon(&m_sensors.gpu, m_config.gpu_pciaddr);
sys::detectHwmon(&m_sensors.sys, m_config.sys_sensor);
m_sensors.home.mountpoint = m_config.home_dir;
while (!st.stop_requested()) {
auto start = std::chrono::steady_clock::now();
{
std::lock_guard<std::mutex> lock(s.mtx);
std::lock_guard<std::mutex> lock(m_sensors.mtx);
sys::getTimeDate(&s.td);
getloadavg(s.avg, 3);
sys::getRam(&s.ram);
sys::getDisk(&s.home);
sys::getNetwork(&s.net, m_config.sys_ifname);
sys::updateSensors(&s.sys);
cpu::update(&s.cpu);
gpu::update(&s.gpu);
sys::getTimeDate(&m_sensors.td);
getloadavg(m_sensors.avg, 3);
sys::getRam(&m_sensors.ram);
sys::getDisk(&m_sensors.home);
sys::getNetwork(&m_sensors.net, m_config.sys_ifname);
sys::updateSensors(&m_sensors.sys);
cpu::update(&m_sensors.cpu);
gpu::update(&m_sensors.gpu);
}
// log(LOG::DBG, "took {} microseconds", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count());
@@ -60,7 +60,7 @@ void Application::sensorsThread(const std::stop_token& st, Sensors& s) {
applog(LOG::INF, "Thread stopped");
}
void Application::displayThread(const std::stop_token& st, Sensors& s) {
void Application::displayThread(const std::stop_token& st) {
std::stop_callback cb(st, [&] { m_control.display.release(); });
applog(LOG::START, "Thread started");
// init internal variables of LCDDisplay class
@@ -85,45 +85,45 @@ void Application::displayThread(const std::stop_token& st, Sensors& s) {
m_control.display.try_acquire_for(std::chrono::milliseconds(static_cast<int>(m_config.lcd_interval_ms * 1.25))); // holds the loop until sensor data is ready, max 1.25x of the loop time
if (st.stop_requested()) break; // needed, otherwise it wont quit correctly
{
std::lock_guard<std::mutex> lock(s.mtx);
std::lock_guard<std::mutex> lock(m_sensors.mtx);
// prepare some tricky sensors
std::string gpu_temps = std::to_string(s.gpu.temp_edge);
if (s.gpu.temp_hotspot) {
const auto delta = s.gpu.temp_hotspot - s.gpu.temp_edge;
gpu_temps.append("/" + std::to_string(s.gpu.temp_hotspot));
std::string gpu_temps = std::to_string(m_sensors.gpu.temp_edge);
if (m_sensors.gpu.temp_hotspot) {
const auto delta = m_sensors.gpu.temp_hotspot - m_sensors.gpu.temp_edge;
gpu_temps.append("/" + std::to_string(m_sensors.gpu.temp_hotspot));
gpu_temps.insert(0, LCDDisplay::cc(1) + std::to_string(delta) + LCDDisplay::cc(0) + " ");
}
uint16_t cpu_fan = 0;
for (const auto& fan : s.sys.fan_speed)
for (const auto& fan : m_sensors.sys.fan_speed)
cpu_fan += fan;
cpu_fan /= 2;
// write to display buffer
lcd.writeBuf(0, 0, utils::pad(s.td.date, 11, utils::just::left) + " " + s.td.time + lcd.bar(7, s.gpu.usage_core / 100.0));
lcd.writeBuf(0, 26, utils::pNum(s.avg[0], s.avg[0] > 10 ? 1 : 2));
lcd.writeBuf(0, 31, utils::pNum(s.avg[1], s.avg[1] > 10 ? 1 : 2));
lcd.writeBuf(0, 36, utils::pNum(s.avg[2], s.avg[2] > 10 ? 1 : 2));
lcd.writeBuf(0, 0, utils::pad(m_sensors.td.date, 11, utils::just::left) + " " + m_sensors.td.time + lcd.bar(7, m_sensors.gpu.usage_core / 100.0));
lcd.writeBuf(0, 26, utils::pNum(m_sensors.avg[0], m_sensors.avg[0] > 10 ? 1 : 2));
lcd.writeBuf(0, 31, utils::pNum(m_sensors.avg[1], m_sensors.avg[1] > 10 ? 1 : 2));
lcd.writeBuf(0, 36, utils::pNum(m_sensors.avg[2], m_sensors.avg[2] > 10 ? 1 : 2));
lcd.writeBuf(1, 0, utils::pad(std::to_string(s.cpu.tctl) + "/" + std::to_string(s.cpu.tccd1) + LCDDisplay::cc(0), 8, utils::just::left));
lcd.writeBuf(1, 7, utils::pad(utils::pBytes(s.ram.used, true), 6, utils::just::left));
lcd.writeBuf(1, 13, utils::pad(utils::pNum(s.hass_state) + "W", 4));
lcd.writeBuf(1, 19, std::to_string(s.sys.t_sensor) + LCDDisplay::cc(0)); // t_sensor temp
lcd.writeBuf(1, 32 - gpu_temps.size(), utils::pad(utils::pBytes(s.gpu.vram_used), 6));
lcd.writeBuf(1, 0, utils::pad(std::to_string(m_sensors.cpu.tctl) + "/" + std::to_string(m_sensors.cpu.tccd1) + LCDDisplay::cc(0), 8, utils::just::left));
lcd.writeBuf(1, 7, utils::pad(utils::pBytes(m_sensors.ram.used, true), 6, utils::just::left));
lcd.writeBuf(1, 13, utils::pad(utils::pNum(m_sensors.hass_state) + "W", 4));
lcd.writeBuf(1, 19, std::to_string(m_sensors.sys.t_sensor) + LCDDisplay::cc(0)); // t_sensor temp
lcd.writeBuf(1, 32 - gpu_temps.size(), utils::pad(utils::pBytes(m_sensors.gpu.vram_used), 6));
lcd.writeBuf(1, 39 - gpu_temps.size(), gpu_temps + LCDDisplay::cc(0));
lcd.writeBuf(2, 0, utils::pad(std::to_string(static_cast<int>(s.cpu.ptotal)) + "W", 8, utils::just::left));
lcd.writeBuf(2, s.cpu.ptotal < 100 ? 4 : 5, std::to_string(cpu_fan) + "R"); // avg fan speed
lcd.writeBuf(2, 12, LCDDisplay::cc(3) + utils::pBytes(s.net.upload));
lcd.writeBuf(2, 19, LCDDisplay::cc(4) + utils::pBytes(s.net.download));
lcd.writeBuf(2, 26, utils::pad(std::to_string(s.gpu.fan_speed) + "R", 5));
lcd.writeBuf(2, 31, utils::pad(std::to_string(s.gpu.power_avg) + "/" + std::to_string(s.gpu.power_cap) + "W", 9));
lcd.writeBuf(2, 0, utils::pad(std::to_string(static_cast<int>(m_sensors.cpu.ptotal)) + "W", 8, utils::just::left));
lcd.writeBuf(2, m_sensors.cpu.ptotal < 100 ? 4 : 5, std::to_string(cpu_fan) + "R"); // avg fan speed
lcd.writeBuf(2, 12, LCDDisplay::cc(3) + utils::pBytes(m_sensors.net.upload));
lcd.writeBuf(2, 19, LCDDisplay::cc(4) + utils::pBytes(m_sensors.net.download));
lcd.writeBuf(2, 26, utils::pad(std::to_string(m_sensors.gpu.fan_speed) + "R", 5));
lcd.writeBuf(2, 31, utils::pad(std::to_string(m_sensors.gpu.power_avg) + "/" + std::to_string(m_sensors.gpu.power_cap) + "W", 9));
lcd.writeBuf(3, 0, std::to_string(s.cpu.avg_freq) + "MHz");
lcd.writeBuf(3, 14, utils::pad(LCDDisplay::cc(2) + utils::pBytes(s.home.usage.free), 6));
lcd.writeBuf(3, 21, utils::pad(std::to_string(s.gpu.clock_core) + "/" + std::to_string(s.gpu.clock_mem) + "MHz ", 13));
lcd.writeBuf(3, 34, utils::pad(utils::pNum(s.gpu.vcore, 3) + "V", 5));
lcd.writeBuf(3, 0, std::to_string(m_sensors.cpu.avg_freq) + "MHz");
lcd.writeBuf(3, 14, utils::pad(LCDDisplay::cc(2) + utils::pBytes(m_sensors.home.usage.free), 6));
lcd.writeBuf(3, 21, utils::pad(std::to_string(m_sensors.gpu.clock_core) + "/" + std::to_string(m_sensors.gpu.clock_mem) + "MHz ", 13));
lcd.writeBuf(3, 34, utils::pad(utils::pNum(m_sensors.gpu.vcore, 3) + "V", 5));
}
lcd.flushBuf();
@@ -135,7 +135,8 @@ void Application::displayThread(const std::stop_token& st, Sensors& s) {
lcd.flushBuf();
}
Application::Application(Config config) : m_config(std::move(config)) {
Application::Application(Config config) {
m_config = std::move(config);
// set up signals
sigemptyset(&m_control.sigset);
sigaddset(&m_control.sigset, SIGINT);
@@ -148,25 +149,25 @@ Application::~Application() {
void Application::Run() {
applog(LOG::START, "starting");
int sig;
pthread_sigmask(SIG_BLOCK, &m_control.sigset, nullptr);
{
Sensors sensors;
std::vector<std::jthread> threads;
std::function<void(std::stop_token, Sensors&)> funcs[] = {
[this](const std::stop_token& st, Sensors& s) { displayThread(st, s); },
[this](const std::stop_token& st, Sensors& s) { sensorsThread(st, s); },
[](const std::stop_token& st, Sensors& s) { hassThread(st, s); },
std::function<void(std::stop_token)> funcs[] = {
[this](const std::stop_token& st) { displayThread(st); },
[this](const std::stop_token& st) { sensorsThread(st); },
[this](const std::stop_token& st) { hassThread(st); },
};
// start threads in order
for (auto& f : funcs) {
threads.emplace_back(f, std::ref(sensors)); // params: function, stop_token (implied), pointer to sensor struct
threads.emplace_back(f);
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // start threads with delay so displayThread can quit the app if theres no lcd
}
int sig;
// waits for signal, the threads are stopped when we go out of scope
sigwait(&m_control.sigset, &sig);
applog(LOG::WRN, "got stop signal, shutting down...");
}