From 42d3d4359bd600a73b9e946930414460b56a88d6 Mon Sep 17 00:00:00 2001 From: Drath Date: Sat, 3 Jan 2026 17:37:08 +0100 Subject: [PATCH] move hass config to main --- include/app.hpp | 7 +++++-- include/hass.hpp | 30 ++++++++++++++++-------------- main.cpp | 9 ++++++--- src/app.cpp | 20 ++++++++++++-------- src/hass.cpp | 17 ++++++++--------- 5 files changed, 47 insertions(+), 36 deletions(-) diff --git a/include/app.hpp b/include/app.hpp index 40f33ad..c4f0491 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -21,6 +21,9 @@ class Application { std::string sys_sensor; std::string sys_ifname; std::string home_dir; + std::string hass_url; + std::string hass_entity; + std::string hass_token; }; explicit Application(Config config); @@ -30,7 +33,7 @@ class Application { private: struct Sensors { sys::TimeDate td; - double avg[3] = {0.0f}; + double loadavg[3] = {0.0f}; sys::Memory ram; sys::Disk home; sys::Network net; @@ -38,11 +41,11 @@ class Application { cpu::cpuSensors cpu; gpu::gpuSensors gpu; double hass_state = 0.0f; - std::mutex mtx; // making sure the struct isnt accessed by multiple threads at once }; struct Control { std::binary_semaphore display{0}; + std::mutex sensors; // making sure the sensors struct isnt accessed by multiple threads at once sigset_t sigset; }; diff --git a/include/hass.hpp b/include/hass.hpp index 0d90c59..3c448d6 100644 --- a/include/hass.hpp +++ b/include/hass.hpp @@ -6,27 +6,29 @@ #include #include -#define HASS_TOKEN "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI0MzUxMTZiY2VjNTg0MTI3OGI4YmU0NTY4ZGQ5MzIyYiIsImlhdCI6MTc1NzQzMDI0MCwiZXhwIjoyMDcyNzkwMjQwfQ.6wKTlxEA0FLWfv-qW35vewUSvTxdVrZWqFwHT5Khu6E" - class HomeAssistant { - private: - std::string address; - std::string entity; - uint16_t id = 2; - std::string tx; - struct lws_context* context = nullptr; - lws* websock = nullptr; - void event_handler(struct lws* wsi, enum lws_callback_reasons reason, char* in); - static int callback(struct lws* wsi, enum lws_callback_reasons reason, void* user, void* in, size_t len); - - public: - HomeAssistant(const std::string& addr, const std::string& ent); + public: + struct Config { + std::string address; + std::string entity; + std::string token; + }; + explicit HomeAssistant(const Config& config); ~HomeAssistant(); float state = 0.0f; int connect(); void disconnect() const; void service(int timeout_ms = 0) const; + + private: + uint16_t id = 2; + std::string tx; + lws_context* context = nullptr; + lws* websock = nullptr; + void event_handler(lws* wsi, lws_callback_reasons reason, char* in); + static int callback(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t len); + Config m_config; }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index badc200..9d04248 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,7 @@ int main() { Application::Config config; - config.lcd_port = "/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00"; + config.lcd_port = "/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00"; // tis a symlink, can also be just /dev/ttyACM0) config.lcd_interval_ms = 500; config.lcd_contrast = 215; config.cpu_pciaddr = "0000:00:18.3"; @@ -11,8 +11,11 @@ int main() { config.sys_sensor = "nct6775.656"; config.sys_ifname = "enp4s0f0"; config.home_dir = "/home/drath"; + config.hass_url = "home.drath.win"; + config.hass_entity = "sensor.gniazdko_komputer_moc"; + config.hass_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI0MzUxMTZiY2VjNTg0MTI3OGI4YmU0NTY4ZGQ5MzIyYiIsImlhdCI6MTc1NzQzMDI0MCwiZXhwIjoyMDcyNzkwMjQwfQ.6wKTlxEA0FLWfv-qW35vewUSvTxdVrZWqFwHT5Khu6E"; - Application app(config); // tis a symlink, can also be just /dev/ttyACM0) - app.Run(); + Application App(config); + App.Run(); return 0; } diff --git a/src/app.cpp b/src/app.cpp index dd4b04c..5ffecd1 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -18,13 +18,17 @@ void Application::hassThread(const std::stop_token& st) { applog(LOG::START, "Thread started"); - HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc"); + HomeAssistant::Config config; + config.address = m_config.hass_url; + config.entity = m_config.hass_entity; + config.token = m_config.hass_token; + HomeAssistant pc_power(config); pc_power.connect(); while (!st.stop_requested()) { pc_power.service(); { - std::lock_guard lock(m_sensors.mtx); + std::lock_guard lock(m_control.sensors); m_sensors.hass_state = pc_power.state; } std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -41,10 +45,10 @@ void Application::sensorsThread(const std::stop_token& st) { while (!st.stop_requested()) { auto start = std::chrono::steady_clock::now(); { - std::lock_guard lock(m_sensors.mtx); + std::lock_guard lock(m_control.sensors); sys::getTimeDate(&m_sensors.td); - getloadavg(m_sensors.avg, 3); + getloadavg(m_sensors.loadavg, 3); sys::getRam(&m_sensors.ram); sys::getDisk(&m_sensors.home); sys::getNetwork(&m_sensors.net, m_config.sys_ifname); @@ -85,7 +89,7 @@ void Application::displayThread(const std::stop_token& st) { m_control.display.try_acquire_for(std::chrono::milliseconds(static_cast(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 lock(m_sensors.mtx); + std::lock_guard lock(m_control.sensors); // prepare some tricky sensors std::string gpu_temps = std::to_string(m_sensors.gpu.temp_edge); @@ -102,9 +106,9 @@ void Application::displayThread(const std::stop_token& st) { // write to display buffer 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(0, 26, utils::pNum(m_sensors.loadavg[0], m_sensors.loadavg[0] > 10 ? 1 : 2)); + lcd.writeBuf(0, 31, utils::pNum(m_sensors.loadavg[1], m_sensors.loadavg[1] > 10 ? 1 : 2)); + lcd.writeBuf(0, 36, utils::pNum(m_sensors.loadavg[2], m_sensors.loadavg[2] > 10 ? 1 : 2)); 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)); diff --git a/src/hass.cpp b/src/hass.cpp index 7ff1689..e68d919 100644 --- a/src/hass.cpp +++ b/src/hass.cpp @@ -6,9 +6,8 @@ #include #include -HomeAssistant::HomeAssistant(const std::string& addr, const std::string& ent) { - address = addr; - entity = ent; +HomeAssistant::HomeAssistant(const Config& config){ + m_config = config; } HomeAssistant::~HomeAssistant() { @@ -29,17 +28,17 @@ void HomeAssistant::event_handler(struct lws* wsi, const enum lws_callback_reaso if (ha_type == "auth_required") { json.clear(); json["type"] = "auth"; - json["access_token"] = HASS_TOKEN; + json["access_token"] = m_config.token; tx = json.dump(); break; } if (ha_type == "auth_ok") { - applog(LOG::INF, "subscribing to entity \"{}\"", entity); + applog(LOG::INF, "subscribing to entity \"{}\"", m_config.entity); json.clear(); json["id"] = id++; json["type"] = "subscribe_entities"; - json["entity_ids"][0] = entity.c_str(); + json["entity_ids"][0] = m_config.entity.c_str(); tx = json.dump(); break; } @@ -53,9 +52,9 @@ void HomeAssistant::event_handler(struct lws* wsi, const enum lws_callback_reaso if (ha_type == "event") { json = json["event"]; if (json.contains("a")) - state = std::stof(json["a"][entity]["s"].get()); + state = std::stof(json["a"][m_config.entity]["s"].get()); if (json.contains("c")) - state = std::stof(json["c"][entity]["+"]["s"].get()); + state = std::stof(json["c"][m_config.entity]["+"]["s"].get()); break; } @@ -134,7 +133,7 @@ int HomeAssistant::connect() { i.context = context; i.port = 443; - i.address = address.c_str(); + i.address = m_config.address.c_str(); i.path = "/api/websocket"; i.host = i.address; i.origin = i.address;