it closes properly and EASY now
This commit is contained in:
@@ -8,29 +8,27 @@
|
|||||||
#include <semaphore>
|
#include <semaphore>
|
||||||
#include <stop_token>
|
#include <stop_token>
|
||||||
#include <sys.hpp>
|
#include <sys.hpp>
|
||||||
#include <vector>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
class Application {
|
class Application {
|
||||||
|
public:
|
||||||
public:
|
|
||||||
struct Config {
|
struct Config {
|
||||||
uint16_t lcd_interval_ms = 500;
|
uint16_t lcd_interval_ms;
|
||||||
uint8_t lcd_contrast = 215;
|
uint8_t lcd_contrast;
|
||||||
|
|
||||||
std::string lcd_port;
|
std::string lcd_port;
|
||||||
std::string cpu_pciaddr = "0000:00:18.3";
|
std::string cpu_pciaddr;
|
||||||
std::string gpu_pciaddr = "0000:0b:00.0";
|
std::string gpu_pciaddr;
|
||||||
std::string sys_sensor = "nct6775.656";
|
std::string sys_sensor;
|
||||||
std::string sys_ifname = "enp4s0f0";
|
std::string sys_ifname;
|
||||||
std::string home_dir = "/home/drath";
|
std::string home_dir;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit Application(const Config& config);
|
explicit Application(Config config);
|
||||||
~Application();
|
~Application();
|
||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Sensors {
|
struct Sensors {
|
||||||
sys::TimeDate td;
|
sys::TimeDate td;
|
||||||
double avg[3] = {0.0f};
|
double avg[3] = {0.0f};
|
||||||
@@ -46,17 +44,13 @@ private:
|
|||||||
|
|
||||||
struct Control {
|
struct Control {
|
||||||
std::binary_semaphore display{0};
|
std::binary_semaphore display{0};
|
||||||
std::binary_semaphore main{0};
|
sigset_t sigset;
|
||||||
std::atomic_bool quit{false};
|
|
||||||
std::stop_source stop;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Config m_config; // m_ is member variable
|
Config m_config; // m_ is prefix for member variable
|
||||||
Control m_control;
|
Control m_control;
|
||||||
|
|
||||||
static Application* s_instance;
|
void hassThread(const std::stop_token& st, Sensors& s);
|
||||||
static void signalHandler(int);
|
|
||||||
static void hassThread(const std::stop_token& st, Sensors& s);
|
|
||||||
void sensorsThread(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);
|
void displayThread(const std::stop_token& st, Sensors& s);
|
||||||
};
|
};
|
||||||
|
|||||||
38
src/app.cpp
38
src/app.cpp
@@ -12,17 +12,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sys.hpp>
|
#include <sys.hpp>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <utility>
|
||||||
#include <utils.hpp>
|
#include <utils.hpp>
|
||||||
#include <vector>
|
#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) {
|
void Application::hassThread(const std::stop_token& st, Sensors& s) {
|
||||||
applog(LOG::START, "Thread started");
|
applog(LOG::START, "Thread started");
|
||||||
HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc");
|
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
|
m_control.display.release(); // unlocks display loop
|
||||||
std::this_thread::sleep_until(start + std::chrono::milliseconds(m_config.lcd_interval_ms));
|
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) {
|
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
|
// prepare some tricky sensors
|
||||||
std::string gpu_temps = std::to_string(s.gpu.temp_edge);
|
std::string gpu_temps = std::to_string(s.gpu.temp_edge);
|
||||||
if (s.gpu.temp_hotspot)
|
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);
|
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;
|
uint16_t cpu_fan = 0;
|
||||||
for (const auto& fan : s.sys.fan_speed)
|
for (const auto& fan : s.sys.fan_speed)
|
||||||
@@ -139,18 +135,20 @@ void Application::displayThread(const std::stop_token& st, Sensors& s) {
|
|||||||
lcd.flushBuf();
|
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() {
|
Application::~Application() {
|
||||||
applog(LOG::START, "Application stopped");
|
applog(LOG::START, "Application quit");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::Run() {
|
void Application::Run() {
|
||||||
applog(LOG::START, "Application started");
|
applog(LOG::START, "starting");
|
||||||
s_instance = this;
|
pthread_sigmask(SIG_BLOCK, &m_control.sigset, nullptr);
|
||||||
std::signal(SIGINT, &Application::signalHandler);
|
|
||||||
std::signal(SIGTERM, &Application::signalHandler);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
Sensors sensors;
|
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
|
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
|
int sig;
|
||||||
while (!m_control.quit.load(std::memory_order_relaxed))
|
sigwait(&m_control.sigset, &sig);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // ugly
|
applog(LOG::WRN, "got stop signal, shutting down...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user