ng_gateway_driver/
modbus.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{NGConverter, NGDriver};
use anyhow::anyhow;
use async_trait::async_trait;
use std::any::Any;

#[derive(Debug, Clone)]
pub struct ModbusDriver {}

#[async_trait]
impl NGDriver for ModbusDriver {
    async fn initialize(&self) -> Result<(), anyhow::Error> {
        Ok(())
    }

    async fn run(&self) -> Result<(), anyhow::Error> {
        Err(anyhow!("Modbus driver not implemented"))
    }

    async fn execute_command(
        &self,
        device_id: i32,
        command: &str,
        params: Box<dyn Any + Send>,
    ) -> Result<Box<dyn Any + Send>, anyhow::Error> {
        todo!()
    }

    async fn shutdown(&self) -> Result<(), anyhow::Error> {
        todo!()
    }

    fn converter(&self) -> Box<dyn NGConverter> {
        todo!()
    }
}

impl ModbusDriver {
    pub fn new() -> Self {
        ModbusDriver {}
    }
}

pub struct ModbusConverter;

impl NGConverter for ModbusConverter {
    fn convert_in(&self, input: Box<dyn Any + Send>) -> Result<Box<dyn Any + Send>, anyhow::Error> {
        if let Some(input) = input.downcast_ref::<String>() {
            Ok(Box::new(format!("Processed Downlink: {}", input)))
        } else if let Some(input) = input.downcast_ref::<i32>() {
            Ok(Box::new(format!("Processed Downlink: {}", input)))
        } else if let Some(input) = input.downcast_ref::<f64>() {
            Ok(Box::new(format!("Processed Downlink: {:.2}", input)))
        } else {
            Err(anyhow!("Invalid input type"))
        }
    }

    fn convert_out(
        &self,
        output: Box<dyn Any + Send>,
    ) -> Result<Box<dyn Any + Send>, anyhow::Error> {
        Ok(Box::new("Processed Uplink".to_string()))
    }
}