v0.12.0-preety_code

This commit is contained in:
Mateusz Gruszczyński
2026-09-03 15:33:37 +02:00
parent be67932b47
commit 07be4b85d9
87 changed files with 11691 additions and 3172 deletions
+31 -9
View File
@@ -11,14 +11,21 @@ mod protocol;
mod queries;
mod state;
use std::{sync::{Arc, atomic::AtomicBool}, time::{Duration, Instant}};
use anyhow::{Context, Result};
use config::Config;
use db::Db;
use models::Device;
use protocol::GreeClient;
use state::AppState;
use tokio::{net::TcpListener, signal, sync::{broadcast, Notify, RwLock}};
use std::{
sync::{atomic::AtomicBool, Arc},
time::{Duration, Instant},
};
use tokio::{
net::TcpListener,
signal,
sync::{broadcast, Notify, RwLock},
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
@@ -27,7 +34,9 @@ async fn main() -> Result<()> {
init_tracing();
let db = Db::open(&config.database)?;
let mut runtime_settings = db.load_runtime_settings()?.unwrap_or_else(|| config.runtime_defaults());
let mut runtime_settings = db
.load_runtime_settings()?
.unwrap_or_else(|| config.runtime_defaults());
// Network deployment settings explicitly provided by the service environment are authoritative.
// This makes /etc/gree-controller.env useful even after runtime settings were persisted in SQLite.
if std::env::var_os("GREE_CONTROLLER_DISCOVERY_BROADCAST").is_some() {
@@ -58,7 +67,8 @@ async fn main() -> Result<()> {
config: Arc::new(config.clone()),
gree: GreeClient::new(
runtime_settings.controller_id.clone(),
(!config.gree_interface.trim().is_empty()).then(|| config.gree_interface.trim().to_string()),
(!config.gree_interface.trim().is_empty())
.then(|| config.gree_interface.trim().to_string()),
Some(events.clone()),
debug_gree_frames.clone(),
),
@@ -76,16 +86,23 @@ async fn main() -> Result<()> {
house_operation_lock: Arc::new(tokio::sync::Mutex::new(())),
configuration_operation_lock: Arc::new(tokio::sync::Mutex::new(())),
zone_control_cycle_lock: Arc::new(tokio::sync::Mutex::new(())),
pending_controller_commands: Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new())),
pending_controller_commands: Arc::new(tokio::sync::Mutex::new(
std::collections::HashMap::new(),
)),
started: Instant::now(),
};
engine::start(state.clone());
let app = api::router(state.clone());
let listener = TcpListener::bind(config.bind).await
let listener = TcpListener::bind(config.bind)
.await
.with_context(|| format!("cannot bind HTTP server to {}", config.bind))?;
let gree_interface_log = if config.gree_interface.trim().is_empty() { "auto" } else { config.gree_interface.trim() };
let gree_interface_log = if config.gree_interface.trim().is_empty() {
"auto"
} else {
config.gree_interface.trim()
};
tracing::info!(
address = %config.bind,
database = %config.database.display(),
@@ -113,12 +130,17 @@ fn init_tracing() {
}
async fn shutdown_signal() {
let ctrl_c = async { signal::ctrl_c().await.expect("cannot install Ctrl+C handler"); };
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("cannot install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("cannot install SIGTERM handler")
.recv().await;
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();