tof_control/
switch_control.rs

1pub mod switch_info;
2pub mod switch_port;
3pub mod switch_util;
4
5use crate::constant::*;
6use crate::helper::switch_type::{AllSwitchData, SwitchData, SwitchInfo, SwitchPort, SwitchError};
7
8use switch_util::print_switch_data;
9use switch_port::clear_port_statistics;
10
11impl AllSwitchData {
12    pub fn new() -> Self {
13        match Self::get_all_switch_data() {
14            Ok(all_switch_data) => {
15                all_switch_data
16            }
17            Err(_) => {
18                Self {
19                    switch1: None,
20                    switch2: None,
21                    switch3: None,
22                }
23            }
24        }
25    }
26    pub fn get_all_switch_data() -> Result<AllSwitchData, SwitchError> {
27        let switch1_data = Self::get_switch_data(1)?;
28        let switch2_data = Self::get_switch_data(2)?;
29        let switch3_data = Self::get_switch_data(3)?;
30
31        Ok(
32            AllSwitchData {
33                switch1: Some(switch1_data),
34                switch2: Some(switch2_data),
35                switch3: Some(switch3_data),
36            }
37        )
38    }
39    pub fn get_switch_data(switch: u8) -> Result<SwitchData, SwitchError> {
40        let switch_addr: &str;
41        match switch {
42            1 => switch_addr = SWITCH1_ADDRESS,
43            2 => switch_addr = SWITCH2_ADDRESS,
44            3 => switch_addr = SWITCH3_ADDRESS,
45            4 => switch_addr = SWITCH4_ADDRESS,
46            _ => return Err(SwitchError::Address),
47        }
48        let switch1_info = SwitchInfo::read_info(switch_addr)?;
49        let switch1_port = SwitchPort::read_port(switch_addr)?;
50        
51        Ok(
52            SwitchData {
53                info: switch1_info,
54                port: switch1_port,
55            }
56        )
57    }
58    pub fn print_all_switch() {
59        let all_switch_data = Self::new();
60
61        if let Some(switch1_data) = all_switch_data.switch1 {
62            println!("TOF-SWITCH1");
63            print_switch_data(&switch1_data);
64        } else {
65            println!("TOF-SWITCH1 is not connected.");
66        }
67
68        if let Some(switch2_data) = all_switch_data.switch2 {
69            println!("TOF-SWITCH2");
70            print_switch_data(&switch2_data);
71        } else {
72            println!("TOF-SWITCH2 is not connected.");
73        }
74
75        if let Some(switch3_data) = all_switch_data.switch3 {
76            println!("TOF-SWITCH3");
77            print_switch_data(&switch3_data);
78        } else {
79            println!("TOF-SWITCH3 is not connected.");
80        }
81    }
82}
83
84pub fn clear_port_statistics_all() -> Result<(), SwitchError> {
85    
86    clear_port_statistics(SWITCH1_ADDRESS)?;
87    clear_port_statistics(SWITCH2_ADDRESS)?;
88    clear_port_statistics(SWITCH3_ADDRESS)?;
89
90    Ok(())
91}