aquarium_control/sensors/
tank_level_switch_channels.rs1use crate::launch::channels::{AquaChannelError, AquaReceiver, AquaSender};
10use crate::simulator::tcp_communication::TcpCommunicationError;
11use crate::utilities::channel_content::InternalCommand;
12use std::fmt;
13
14pub struct TankLevelSwitchChannels {
15 pub tx_tank_level_switch_to_tcp_opt: Option<AquaSender<InternalCommand>>,
16
17 pub rx_tank_level_switch_from_tcp_opt: Option<AquaReceiver<Result<f32, TcpCommunicationError>>>,
18
19 pub tx_tank_level_switch_to_signal_handler: AquaSender<bool>,
20
21 pub rx_tank_level_switch_from_signal_handler: AquaReceiver<InternalCommand>,
22}
23
24impl TankLevelSwitchChannels {
25 #[allow(unused)]
27 pub fn send_to_tcp(
28 &mut self,
29 command: InternalCommand,
30 ) -> Option<Result<(), AquaChannelError>> {
31 self.tx_tank_level_switch_to_tcp_opt
32 .as_mut()
33 .map(|tx| tx.send(command))
34 }
35
36 #[allow(unused)]
38 pub fn receive_from_tcp(
39 &mut self,
40 ) -> Option<Result<Result<f32, TcpCommunicationError>, AquaChannelError>> {
41 self.rx_tank_level_switch_from_tcp_opt
42 .as_mut()
43 .map(|rx| rx.recv())
44 }
45
46 #[allow(unused)]
48 pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
49 self.tx_tank_level_switch_to_signal_handler.send(ack)
50 }
51
52 #[allow(unused)]
54 pub fn receive_from_signal_handler(&mut self) -> Result<InternalCommand, AquaChannelError> {
55 self.rx_tank_level_switch_from_signal_handler.recv()
56 }
57}
58
59#[cfg(feature = "debug_channels")]
60impl fmt::Display for TankLevelSwitchChannels {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 writeln!(f, "=== TankLevelSwitchChannels ===")?;
63 writeln!(
64 f,
65 "tx_tank_level_switch_to_tcp_opt: {}",
66 self.tx_tank_level_switch_to_tcp_opt.as_ref().unwrap().count
67 )?;
68 writeln!(
69 f,
70 "rx_tank_level_switch_from_tcp_opt: {}",
71 self.rx_tank_level_switch_from_tcp_opt
72 .as_ref()
73 .unwrap()
74 .count
75 )?;
76 writeln!(
77 f,
78 "tx_tank_level_switch_to_signal_handler: {}",
79 self.tx_tank_level_switch_to_signal_handler.count
80 )?;
81 write!(
82 f,
83 "rx_tank_level_switch_from_signal_handler: {}",
84 self.rx_tank_level_switch_from_signal_handler.count
85 )
86 }
87}
88
89#[cfg(not(feature = "debug_channels"))]
90impl fmt::Display for TankLevelSwitchChannels {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 write!(
93 f,
94 "Channel counters are not active. Use --features \"debug_channels\" to enable them."
95 )
96 }
97}