From 369e4d02132511ee958c46ab082dbdee730e86a2 Mon Sep 17 00:00:00 2001 From: Drath Date: Tue, 24 Feb 2026 19:53:54 +0100 Subject: [PATCH] smarter cmake, conditional hass, still need to do conditional gzip logging --- CMakeLists.txt | 21 +++++++++++++++------ include/hass.hpp | 3 +++ src/app.cpp | 25 ++++++++++++++++++++----- src/hass.cpp | 4 ++++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e392a9..9c8e744 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,10 +36,19 @@ target_compile_options(linuxSmartie -fdiagnostics-color=always ) -# Link libraries -target_link_libraries(linuxSmartie - PRIVATE - websockets - z -) +find_package(libwebsockets QUIET) +find_package(ZLIB QUIET) +if(libwebsockets_FOUND) + target_link_libraries(linuxSmartie PRIVATE websockets) + target_compile_definitions(linuxSmartie PRIVATE ENABLE_HOME_ASSISTANT) +else() + message(STATUS "no libwebsockets found, disabling hass") +endif() + +if(ZLIB_FOUND) + target_link_libraries(linuxSmartie PRIVATE z) + target_compile_definitions(linuxSmartie PRIVATE ENABLE_ZLIB) +else() + message(STATUS "no zlib found, disabling saving logs to gz") +endif() diff --git a/include/hass.hpp b/include/hass.hpp index b606800..01b7685 100644 --- a/include/hass.hpp +++ b/include/hass.hpp @@ -1,6 +1,7 @@ #ifndef HASS_HPP #define HASS_HPP +#ifdef ENABLE_HOME_ASSISTANT #include #include @@ -31,4 +32,6 @@ class HomeAssistant { Config m_config; }; +#endif + #endif \ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index a84b4dd..9c25cda 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -63,8 +63,11 @@ void Application::Crash(const int ret) { kill(m_mainPid, SIGINT); } -void Application::hassThread(const std::stop_token& st) { + +void Application::hassThread(const std::stop_token& st) { //NOLINT applog(LOG::START, "Thread started"); +#ifdef ENABLE_HOME_ASSISTANT + static double power; HomeAssistant::Config config; config.address = m_config.hass.url; config.entity = m_config.hass.entity; @@ -74,12 +77,23 @@ void Application::hassThread(const std::stop_token& st) { pc_power.connect(); while (!st.stop_requested()) { pc_power.service(); - { - std::lock_guard lock(m_control.sensors); - m_sensors.hass_state = pc_power.state; + if (power != pc_power.state) { + power = pc_power.state; + { + std::lock_guard lock(m_control.sensors); + m_sensors.hass_state = power; + } } std::this_thread::sleep_for(std::chrono::milliseconds(10)); } +#else + (void)st; + applog(LOG::WRN, "feature disabled at compile time, probably no libwebsockets on host. exiting thread"); + { + std::lock_guard lock(m_control.sensors); + m_sensors.hass_state = -1.0; + } +#endif } void Application::sensorsThread(const std::stop_token& st) { @@ -169,7 +183,8 @@ void Application::displayThread(const std::stop_token& st) { 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)); - lcd.writeBuf(1, 13, utils::pad(utils::pNum(m_sensors.hass_state) + "W", 4)); + if (m_sensors.hass_state >= 0) + lcd.writeBuf(1, 13, utils::pad(utils::pNum(m_sensors.hass_state) + "W", 4)); lcd.writeBuf(1, 19, std::to_string(m_sensors.sys.t_sensor) + LCDDisplay::cc(0)); // t_sensor temp lcd.writeBuf(1, 32 - gpu_temps.size(), utils::pad(utils::pBytes(m_sensors.gpu.vram_used), 6)); lcd.writeBuf(1, 39 - gpu_temps.size(), gpu_temps + LCDDisplay::cc(0)); diff --git a/src/hass.cpp b/src/hass.cpp index fc12015..68da34b 100644 --- a/src/hass.cpp +++ b/src/hass.cpp @@ -6,6 +6,8 @@ #include #include +#ifdef ENABLE_HOME_ASSISTANT + HomeAssistant::HomeAssistant(const Config& config) { m_config = config; } @@ -158,3 +160,5 @@ void HomeAssistant::service(const int timeout_ms) const { void HomeAssistant::disconnect() const { lws_context_destroy(context); } + +#endif \ No newline at end of file