v0.7.4
This commit is contained in:
+11
-2
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user