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

@@ -8,29 +8,27 @@
#include <semaphore>
#include <stop_token>
#include <sys.hpp>
#include <vector>
#include <thread>
class Application {
public:
public:
struct Config {
uint16_t lcd_interval_ms = 500;
uint8_t lcd_contrast = 215;
uint16_t lcd_interval_ms;
uint8_t lcd_contrast;
std::string lcd_port;
std::string cpu_pciaddr = "0000:00:18.3";
std::string gpu_pciaddr = "0000:0b:00.0";
std::string sys_sensor = "nct6775.656";
std::string sys_ifname = "enp4s0f0";
std::string home_dir = "/home/drath";
std::string cpu_pciaddr;
std::string gpu_pciaddr;
std::string sys_sensor;
std::string sys_ifname;
std::string home_dir;
};
explicit Application(const Config& config);
explicit Application(Config config);
~Application();
void Run();
private:
private:
struct Sensors {
sys::TimeDate td;
double avg[3] = {0.0f};
@@ -46,17 +44,13 @@ private:
struct Control {
std::binary_semaphore display{0};
std::binary_semaphore main{0};
std::atomic_bool quit{false};
std::stop_source stop;
sigset_t sigset;
};
Config m_config; // m_ is member variable
Config m_config; // m_ is prefix for member variable
Control m_control;
static Application* s_instance;
static void signalHandler(int);
static void hassThread(const std::stop_token& st, Sensors& s);
void hassThread(const std::stop_token& st, Sensors& s);
void sensorsThread(const std::stop_token& st, Sensors& s);
void displayThread(const std::stop_token& st, Sensors& s);
};

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...");
}
}