unsure
This commit is contained in:
parent
0e3233009b
commit
1a00daf6a8
11 changed files with 1192 additions and 67 deletions
|
|
@ -1,8 +1,11 @@
|
|||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use bread_shared::RawEvent;
|
||||
use tokio::sync::{mpsc, watch};
|
||||
use tokio::sync::{mpsc, watch, RwLock};
|
||||
use tracing::info;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::core::config::Config;
|
||||
use crate::core::supervisor::spawn_supervised;
|
||||
|
|
@ -14,6 +17,13 @@ pub mod udev;
|
|||
pub mod network_rtnetlink;
|
||||
pub mod power_upower;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum AdapterStatus {
|
||||
Connected,
|
||||
Disconnected,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Adapter: Send + Sync {
|
||||
fn name(&self) -> &'static str;
|
||||
|
|
@ -30,6 +40,7 @@ pub struct Manager {
|
|||
raw_tx: mpsc::Sender<RawEvent>,
|
||||
config: Config,
|
||||
shutdown_rx: watch::Receiver<bool>,
|
||||
status: Arc<RwLock<HashMap<String, AdapterStatus>>>,
|
||||
}
|
||||
|
||||
impl Manager {
|
||||
|
|
@ -42,9 +53,14 @@ impl Manager {
|
|||
raw_tx,
|
||||
config,
|
||||
shutdown_rx,
|
||||
status: Arc::new(RwLock::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status_handle(&self) -> Arc<RwLock<HashMap<String, AdapterStatus>>> {
|
||||
self.status.clone()
|
||||
}
|
||||
|
||||
pub async fn start_all(&self) -> Result<()> {
|
||||
info!("starting adapters");
|
||||
|
||||
|
|
@ -91,17 +107,27 @@ impl Manager {
|
|||
let tx = self.raw_tx.clone();
|
||||
let shutdown_rx = self.shutdown_rx.clone();
|
||||
let shutdown_for_task = shutdown_rx.clone();
|
||||
let status = self.status.clone();
|
||||
spawn_supervised(name, shutdown_rx, move || {
|
||||
let adapter = adapter.clone();
|
||||
let tx = tx.clone();
|
||||
let mut shutdown_rx = shutdown_for_task.clone();
|
||||
let status = status.clone();
|
||||
async move {
|
||||
adapter.on_connect().await?;
|
||||
{
|
||||
let mut guard = status.write().await;
|
||||
guard.insert(adapter.name().to_string(), AdapterStatus::Connected);
|
||||
}
|
||||
let result = tokio::select! {
|
||||
result = adapter.run(tx) => result,
|
||||
_ = shutdown_rx.changed() => Ok(()),
|
||||
};
|
||||
adapter.on_disconnect().await?;
|
||||
{
|
||||
let mut guard = status.write().await;
|
||||
guard.insert(adapter.name().to_string(), AdapterStatus::Disconnected);
|
||||
}
|
||||
result
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue