move hass config to main
This commit is contained in:
20
src/app.cpp
20
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<std::mutex> 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<std::mutex> 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<int>(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<std::mutex> 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));
|
||||
|
||||
17
src/hass.cpp
17
src/hass.cpp
@@ -6,9 +6,8 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
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<std::string>());
|
||||
state = std::stof(json["a"][m_config.entity]["s"].get<std::string>());
|
||||
if (json.contains("c"))
|
||||
state = std::stof(json["c"][entity]["+"]["s"].get<std::string>());
|
||||
state = std::stof(json["c"][m_config.entity]["+"]["s"].get<std::string>());
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user