123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 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<dyn Service>,
- pub channel: Arc<Mutex<mpsc::Receiver<String>>>,
- }
- impl Device {
- /// 创建设备
- pub fn new(
- id: String,
- name: &str,
- service: Arc<dyn Service>,
- channel: mpsc::Receiver<String>,
- ) -> Arc<Self> {
- Arc::new(Self {
- id,
- name: name.to_string(),
- service,
- channel: Arc::new(Mutex::new(channel)),
- })
- }
- pub async fn start(self: Arc<Self>) {
- self.service.serve().await;
- }
- }
|