device.rs 889 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use crate::ems::Service;
  2. use std::sync::Arc;
  3. use tokio::sync::{mpsc, Mutex};
  4. /// 设备的类型
  5. #[derive(Eq, PartialEq, Hash)]
  6. pub enum DevType {
  7. PCS,
  8. BMS,
  9. }
  10. /// 抽象设备结构
  11. /// id: 设备唯一ID
  12. /// name: 设备名称
  13. /// service: 设备的服务
  14. /// channel: 广播接收器
  15. pub struct Device {
  16. pub id: String,
  17. pub name: String,
  18. pub service: Arc<dyn Service>,
  19. pub channel: Arc<Mutex<mpsc::Receiver<String>>>,
  20. }
  21. impl Device {
  22. /// 创建设备
  23. pub fn new(
  24. id: String,
  25. name: &str,
  26. service: Arc<dyn Service>,
  27. channel: mpsc::Receiver<String>,
  28. ) -> Arc<Self> {
  29. Arc::new(Self {
  30. id,
  31. name: name.to_string(),
  32. service,
  33. channel: Arc::new(Mutex::new(channel)),
  34. })
  35. }
  36. pub async fn start(self: Arc<Self>) {
  37. self.service.serve().await;
  38. }
  39. }