aquarium_control/sensors/
sensor_manager_channels.rs

1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use 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    /// Sender part of the channel for communication via TCP wrapped as optional
16    pub tx_sensor_manager_to_tcp_opt: Option<AquaSender<InternalCommand>>,
17
18    /// Receiver part of the channel for communication via TCP wrapped as optional
19    pub rx_sensor_manager_from_tcp_opt: Option<AquaReceiver<Result<f32, TcpCommunicationError>>>,
20
21    /// Sender part of the channel for communication to the signal handler
22    pub tx_sensor_manager_to_signal_handler: AquaSender<bool>,
23
24    /// Receiver part of the channel for communication from the signal handler
25    pub rx_sensor_manager_from_signal_handler: AquaReceiver<InternalCommand>,
26}
27
28impl SensorManagerChannels {
29    /// Sends a command to the TCP simulator if the channel exists.
30    #[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    /// Receives a result from the TCP simulator if the channel exists.
41    #[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    /// Sends an acknowledgment to the signal handler.
51    #[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    /// Receives a command from the signal handler.
57    #[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}