br-serial 0.0.1

This is an COM and Serial Port
Documentation
use std::sync::{mpsc, Mutex};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;
use lazy_static::lazy_static;
use log::info;
use serialport::{available_ports, SerialPortType};

mod usb;
pub mod protocol;
lazy_static! {
    /// 全局串口变量
    pub static ref DEVICES: Mutex<HashMap<String,Device>> =Mutex::new(HashMap::new());
    /// 无法连接的设备
    pub static ref BLACKLIST: Mutex<HashMap<String,String>> =Mutex::new(HashMap::new());
}

/// 主设备
#[derive(Clone, Debug)]
pub struct Devices {}

impl Devices {
    pub fn new() -> Receiver<SubDevice> {

        // 创建通道
        let (tx, rx) = mpsc::channel();

        thread::spawn(move || {
            Devices::listen_device();
        });
        let tx2 = tx.clone();
        thread::spawn(move || {
            Devices::start(tx2.clone());
        });
        return rx;
    }
    /// 监听usb设备
    pub fn listen_device() {
        loop {
            let mut devices = HashMap::new();
            match available_ports() {
                Ok(ports) => {
                    for p in ports {
                        match p.port_type {
                            SerialPortType::UsbPort(info) => {
                                let is_none = BLACKLIST.lock().unwrap().get(&*p.port_name.to_string()).is_none();
                                if !is_none {
                                    continue;
                                }
                                devices.insert(p.port_name.to_string(), info);
                                continue;
                            }
                            _ => {}
                        }
                    }
                }
                Err(_) => {}
            }
            for (com, device) in devices.iter() {
                let is_none = DEVICES.lock().unwrap().get(&*com.clone()).is_none();
                if !is_none {
                    continue;
                }
                let uuid = format!("{}_{}", device.vid, device.pid);
                info!("检测到设备: {}",uuid);
                let device = Device {
                    uuid: uuid.clone(),
                    com: com.clone(),
                    supplier: "".to_string().clone(),
                    manufacturer: device.manufacturer.as_ref().map_or("", String::as_str).to_string(),
                    vid: device.vid,
                    pid: device.pid,
                    serial_number: device.serial_number.as_ref().map_or("", String::as_str).to_string(),
                    product_name: device.product.as_ref().map_or("", String::as_str).to_string(),
                    state: 0,
                    device_id: "".to_string(),
                    sub_device: HashMap::new(),
                    rate: 115200,
                    reconnect: 0,
                };
                DEVICES.lock().unwrap().insert(com.clone(), device.clone());
            }
            thread::sleep(Duration::from_secs(1));
        }
    }
    pub fn start(sender: Sender<SubDevice>) {
        loop {
            let mut devices = DEVICES.lock().unwrap().clone();
            let mut sub_devices = HashMap::new();
            for (com, device) in devices.iter_mut() {
                match device.state {
                    0 => {
                        usb::Usb { com: com.clone(), step: 0, tx: sender.clone() }.connect();
                    }
                    _ => {
                        for (x, y) in device.sub_device.iter() {
                            sub_devices.insert(x.clone(), y.clone());
                        }
                    }
                }
            }
            thread::sleep(Duration::from_secs(1));
        }
    }
}

/// 主设备
#[derive(Clone, Debug)]
pub struct Device {
    /// 设备id
    pub device_id: String,
    /// 端口地址
    pub com: String,
    /// 全局唯一ID
    pub uuid: String,
    /// 供应商
    pub supplier: String,
    /// 制造商
    pub manufacturer: String,
    /// 生产厂商ID
    pub vid: u16,
    /// 产品ID
    pub pid: u16,
    /// 序列号
    pub serial_number: String,
    /// 产品名称
    pub product_name: String,
    /// 连接状态
    pub state: usize,
    /// 子设备
    pub sub_device: HashMap<String, SubDevice>,
    /// 波特率
    pub rate: u32,
    /// 重连次数
    pub reconnect: usize,
}

impl Device {
    pub fn save_data(&mut self) {
        DEVICES.lock().unwrap().insert(self.com.clone(), self.clone());
    }
}

/// 子设备
#[derive(Clone, Debug)]
pub struct SubDevice {
    pub mode: String,
    // 子设备ID
    pub id: String,
    // 子设备名称
    pub name: String,
    // 子设备连接状态
    pub state: i32,
    // 主设备 接收状态
    pub device: bool,
    /// usb的uuid
    pub device_id: String,
    /// 数据模式
    pub data_mode: String,
    /// 数据类型
    pub data_type: String,

    pub value: f64,
}

impl FromStr for SubDevice {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let sub = json::parse(s).unwrap();
        Ok(SubDevice {
            mode: "USB".to_string(),
            id: sub["id"].to_string(),
            name: sub["name"].to_string(),
            state: sub["state"].as_i32().unwrap(),
            device: sub["device"].as_bool().unwrap(),
            device_id: sub["device_id"].to_string(),
            data_mode: sub["data_mode"].to_string(),
            data_type: sub["data_type"].to_string(),
            value: sub["value"].as_f64().unwrap(),
        })
    }
}