it closes properly and EASY now

This commit is contained in:
2026-01-03 15:34:58 +01:00
parent 4b00625470
commit 4720239ebd
2 changed files with 31 additions and 39 deletions

View File

@@ -12,17 +12,10 @@
#include <string>
#include <sys.hpp>
#include <thread>
#include <utility>
#include <utils.hpp>
#include <vector>
Application* Application::s_instance = nullptr;
void Application::signalHandler(int) {
applog(LOG::WRN, "got SIGINT/SIGTERM, exiting...");
if (s_instance)
s_instance->m_control.quit.store(true, std::memory_order_relaxed);
}
void Application::hassThread(const std::stop_token& st, Sensors& s) {
applog(LOG::START, "Thread started");
HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc");
@@ -64,7 +57,7 @@ void Application::sensorsThread(const std::stop_token& st, Sensors& s) {
m_control.display.release(); // unlocks display loop
std::this_thread::sleep_until(start + std::chrono::milliseconds(m_config.lcd_interval_ms));
}
applog(LOG::INF, "Stopped thread");
applog(LOG::INF, "Thread stopped");
}
void Application::displayThread(const std::stop_token& st, Sensors& s) {
@@ -96,8 +89,11 @@ void Application::displayThread(const std::stop_token& st, Sensors& s) {
// prepare some tricky sensors
std::string gpu_temps = std::to_string(s.gpu.temp_edge);
if (s.gpu.temp_hotspot)
gpu_temps = utils::pad(LCDDisplay::cc(1) + std::to_string(s.gpu.temp_hotspot - s.gpu.temp_edge) + LCDDisplay::cc(0), 4) + utils::pad(gpu_temps + "/" + std::to_string(s.gpu.temp_hotspot), 6);
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));
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)
@@ -139,18 +135,20 @@ void Application::displayThread(const std::stop_token& st, Sensors& s) {
lcd.flushBuf();
}
Application::Application(const Config& config) : m_config(config) {
Application::Application(Config config) : m_config(std::move(config)) {
// set up signals
sigemptyset(&m_control.sigset);
sigaddset(&m_control.sigset, SIGINT);
sigaddset(&m_control.sigset, SIGTERM);
}
Application::~Application() {
applog(LOG::START, "Application stopped");
applog(LOG::START, "Application quit");
}
void Application::Run() {
applog(LOG::START, "Application started");
s_instance = this;
std::signal(SIGINT, &Application::signalHandler);
std::signal(SIGTERM, &Application::signalHandler);
applog(LOG::START, "starting");
pthread_sigmask(SIG_BLOCK, &m_control.sigset, nullptr);
{
Sensors sensors;
@@ -168,8 +166,8 @@ void Application::Run() {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // start threads with delay so displayThread can quit the app if theres no lcd
}
// hold until signal recieved
while (!m_control.quit.load(std::memory_order_relaxed))
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // ugly
int sig;
sigwait(&m_control.sigset, &sig);
applog(LOG::WRN, "got stop signal, shutting down...");
}
}