use crate::ems::Service; use std::sync::Arc; use tokio::sync::{mpsc, Mutex}; /// 设备的类型 #[derive(Eq, PartialEq, Hash)] pub enum DevType { PCS, BMS, } /// 抽象设备结构 /// id: 设备唯一ID /// name: 设备名称 /// service: 设备的服务 /// channel: 广播接收器 pub struct Device { pub id: String, pub name: String, pub service: Arc, pub channel: Arc>>, } impl Device { /// 创建设备 pub fn new( id: String, name: &str, service: Arc, channel: mpsc::Receiver, ) -> Arc { Arc::new(Self { id, name: name.to_string(), service, channel: Arc::new(Mutex::new(channel)), }) } pub async fn start(self: Arc) { self.service.serve().await; } }