aquarium_control/sensors/
tank_level_switch_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 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    /// Sends a command to the TCP simulator if the channel exists.
26    #[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    /// Receives a result from the TCP simulator if the channel exists.
37    #[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    /// Sends an acknowledgment to the signal handler.
47    #[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    /// Receives a command from the signal handler.
53    #[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}