This commit is contained in:
Mateusz Gruszczyński
2026-08-25 23:59:04 +02:00
parent 9fa0a5399e
commit b5a4265148
11 changed files with 598 additions and 193 deletions
+11 -2
View File
@@ -1,7 +1,7 @@
use std::{sync::{Arc, atomic::AtomicBool}, time::Instant};
use std::{collections::HashMap, sync::{Arc, atomic::AtomicBool}, time::Instant};
use chrono::Utc;
use serde_json::Value;
use tokio::sync::{broadcast, RwLock};
use tokio::sync::{broadcast, Mutex, OwnedMutexGuard, RwLock};
use crate::{config::Config, db::Db, models::{ApiEvent, RuntimeSettings}, protocol::GreeClient};
#[derive(Clone)]
@@ -14,10 +14,19 @@ pub struct AppState {
pub http: reqwest::Client,
pub outdoor_temperature: Arc<RwLock<Option<f64>>>,
pub debug_gree_frames: Arc<AtomicBool>,
pub(crate) device_operation_locks: Arc<Mutex<HashMap<String, Arc<Mutex<()>>>>>,
pub started: Instant,
}
impl AppState {
pub async fn lock_device_operation(&self, device_id: &str) -> OwnedMutexGuard<()> {
let lock = {
let mut locks = self.device_operation_locks.lock().await;
locks.entry(device_id.to_string()).or_insert_with(|| Arc::new(Mutex::new(()))).clone()
};
lock.lock_owned().await
}
pub fn broadcast(&self, event: impl Into<String>, data: Value) {
let _ = self.events.send(ApiEvent {
event: event.into(),