add some return values to some shit so it shuts down elegantly on crash
This commit is contained in:
105
src/app.cpp
105
src/app.cpp
@@ -16,12 +16,59 @@
|
||||
#include <utils.hpp>
|
||||
#include <vector>
|
||||
|
||||
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);
|
||||
applog(LOG::START, "Application initialized");
|
||||
}
|
||||
|
||||
Application::~Application() {
|
||||
auto level = LOG::START;
|
||||
if (m_ret < 0) level = LOG::WRN;
|
||||
applog(level, "Application destroyed");
|
||||
}
|
||||
|
||||
int Application::Run() {
|
||||
int sig;
|
||||
m_mainPid = getpid();
|
||||
pthread_sigmask(SIG_BLOCK, &m_control.sigset, nullptr);
|
||||
|
||||
{
|
||||
std::vector<std::jthread> threads;
|
||||
|
||||
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::this_thread::sleep_for(std::chrono::milliseconds(10)); // start threads with delay so displayThread can quit the app if theres no lcd
|
||||
}
|
||||
|
||||
// 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...");
|
||||
}
|
||||
return m_ret;
|
||||
}
|
||||
|
||||
void Application::Crash(const int ret) {
|
||||
m_ret = ret;
|
||||
kill(m_mainPid, SIGINT);
|
||||
}
|
||||
|
||||
void Application::hassThread(const std::stop_token& st) {
|
||||
applog(LOG::START, "Thread started");
|
||||
HomeAssistant::Config config;
|
||||
config.address = m_config.hass.url;
|
||||
config.entity = m_config.hass.entity;
|
||||
config.token = m_config.hass.token;
|
||||
config.entity = m_config.hass.entity;
|
||||
config.token = m_config.hass.token;
|
||||
HomeAssistant pc_power(config);
|
||||
|
||||
pc_power.connect();
|
||||
@@ -37,10 +84,15 @@ void Application::hassThread(const std::stop_token& st) {
|
||||
|
||||
void Application::sensorsThread(const std::stop_token& st) {
|
||||
applog(LOG::START, "Thread started, poll interval {} ms", m_config.lcd.refresh_interval_ms);
|
||||
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);
|
||||
int ret = cpu::detect(&m_sensors.cpu, m_config.cpu_pciaddr);
|
||||
ret += gpu::detect(&m_sensors.gpu, m_config.gpu_pciaddr);
|
||||
ret += sys::detect(&m_sensors.sys, m_config.sys_sensor);
|
||||
m_sensors.home.mountpoint = m_config.home_dir;
|
||||
if (ret < 0) {
|
||||
applog(LOG::ERR, "Failed to detect some sensors");
|
||||
this->Crash(ret);
|
||||
return;
|
||||
}
|
||||
|
||||
while (!st.stop_requested()) {
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
@@ -69,6 +121,11 @@ void Application::displayThread(const std::stop_token& st) {
|
||||
applog(LOG::START, "Thread started");
|
||||
// init internal variables of LCDDisplay class
|
||||
LCDDisplay lcd(m_config.lcd.port);
|
||||
if (const int ret = lcd.Init(); ret < 0) {
|
||||
applog(LOG::ERR, "Failed to initialize LCDDisplay");
|
||||
this->Crash(ret);
|
||||
return;
|
||||
}
|
||||
|
||||
// basic lcd setup
|
||||
lcd.setContrast(m_config.lcd.contrast);
|
||||
@@ -137,42 +194,4 @@ void Application::displayThread(const std::stop_token& st) {
|
||||
// goodbye msg
|
||||
lcd.writeBuf(2, 0, utils::center("Goodbye", 40));
|
||||
lcd.flushBuf();
|
||||
}
|
||||
|
||||
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 quit");
|
||||
}
|
||||
|
||||
void Application::Run() {
|
||||
applog(LOG::START, "starting");
|
||||
int sig;
|
||||
pthread_sigmask(SIG_BLOCK, &m_control.sigset, nullptr);
|
||||
|
||||
{
|
||||
std::vector<std::jthread> threads;
|
||||
|
||||
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::this_thread::sleep_for(std::chrono::milliseconds(10)); // start threads with delay so displayThread can quit the app if theres no lcd
|
||||
}
|
||||
|
||||
// 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...");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user