aquarium_control/sensors/
sensor_manager_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 SensorManagerChannels {
15 pub tx_sensor_manager_to_tcp_opt: Option<AquaSender<InternalCommand>>,
17
18 pub rx_sensor_manager_from_tcp_opt: Option<AquaReceiver<Result<f32, TcpCommunicationError>>>,
20
21 pub tx_sensor_manager_to_signal_handler: AquaSender<bool>,
23
24 pub rx_sensor_manager_from_signal_handler: AquaReceiver<InternalCommand>,
26}
27
28impl SensorManagerChannels {
29 #[allow(unused)]
31 pub fn send_to_tcp(
32 &mut self,
33 command: InternalCommand,
34 ) -> Option<Result<(), AquaChannelError>> {
35 self.tx_sensor_manager_to_tcp_opt
36 .as_mut()
37 .map(|tx| tx.send(command))
38 }
39
40 #[allow(unused)]
42 pub fn receive_from_tcp(
43 &mut self,
44 ) -> Option<Result<Result<f32, TcpCommunicationError>, AquaChannelError>> {
45 self.rx_sensor_manager_from_tcp_opt
46 .as_mut()
47 .map(|rx| rx.recv())
48 }
49
50 #[allow(unused)]
52 pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
53 self.tx_sensor_manager_to_signal_handler.send(ack)
54 }
55
56 #[allow(unused)]
58 pub fn receive_from_signal_handler(&mut self) -> Result<InternalCommand, AquaChannelError> {
59 self.rx_sensor_manager_from_signal_handler.recv()
60 }
61}
62
63#[cfg(feature = "debug_channels")]
64impl fmt::Display for SensorManagerChannels {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 writeln!(f, "=== SensorManagerChannels ===")?;
67 if self.tx_sensor_manager_to_tcp_opt.is_some() {
68 writeln!(
69 f,
70 "tx_sensor_manager_to_tcp_opt: {}",
71 self.tx_sensor_manager_to_tcp_opt.as_ref().unwrap().count
72 )?;
73 }
74 if self.rx_sensor_manager_from_tcp_opt.is_some() {
75 writeln!(
76 f,
77 "rx_sensor_manager_from_tcp_opt: {}",
78 self.rx_sensor_manager_from_tcp_opt.as_ref().unwrap().count
79 )?;
80 }
81 writeln!(
82 f,
83 "tx_sensor_manager_to_signal_handler: {}",
84 self.tx_sensor_manager_to_signal_handler.count
85 )?;
86 write!(
87 f,
88 "rx_sensor_manager_from_signal_handler: {}",
89 self.rx_sensor_manager_from_signal_handler.count
90 )
91 }
92}
93
94#[cfg(not(feature = "debug_channels"))]
95impl fmt::Display for SensorManagerChannels {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(
98 f,
99 "Channel counters are not active. Use --features \"debug_channels\" to enable them."
100 )
101 }
102}