aquarium_control/watchmen/
monitors_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::utilities::channel_content::InternalCommand;
11use crate::water::refill_monitor_view::RefillMonitorView;
12use std::fmt;
13
14/// Struct collects the channels for communication between Monitors and other components.
15pub struct MonitorsChannels {
16    /// Receiver part of the channel for communication from Refill
17    pub rx_monitors_from_refill: AquaReceiver<RefillMonitorView>,
18
19    /// Sender part of the channel for communication to the signal handler
20    pub tx_monitors_to_signal_handler: AquaSender<bool>,
21
22    /// Receiver part of the channel for communication from the signal handler
23    pub rx_monitors_from_signal_handler: AquaReceiver<InternalCommand>,
24
25    /// Receiver part of the channel for communication from the messaging
26    pub rx_monitors_from_messaging_opt: Option<AquaReceiver<InternalCommand>>,
27}
28
29impl MonitorsChannels {
30    /// Receives a monitor view from the refill module.
31    ///
32    /// This wrapper performs a non-blocking receive-operation and increments the debug counter on success.
33    #[allow(unused)]
34    pub fn receive_from_refill(&mut self) -> Result<RefillMonitorView, AquaChannelError> {
35        self.rx_monitors_from_refill.try_recv()
36    }
37
38    /// Sends an acknowledgment to the signal handler.
39    ///
40    /// This wrapper sends a boolean acknowledgment and increments the debug counter.
41    #[allow(unused)]
42    pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
43        self.tx_monitors_to_signal_handler.send(ack)
44    }
45
46    /// Receives a command from the signal handler.
47    ///
48    /// This wrapper performs a non-blocking receive-operation and increments the debug counter on success.
49    #[allow(unused)]
50    pub fn receive_from_signal_handler(&mut self) -> Result<InternalCommand, AquaChannelError> {
51        self.rx_monitors_from_signal_handler.try_recv()
52    }
53
54    /// Receives a command from messaging if the channel exists.
55    ///
56    /// This wrapper performs a non-blocking receive-operation and increments the debug counter on success.
57    #[allow(unused)]
58    pub fn receive_from_messaging(&mut self) -> Option<Result<InternalCommand, AquaChannelError>> {
59        self.rx_monitors_from_messaging_opt
60            .as_mut()
61            .map(|rx| rx.try_recv())
62    }
63}
64
65#[cfg(feature = "debug_channels")]
66impl fmt::Display for MonitorsChannels {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        writeln!(f, "=== MonitorsChannels ===")?;
69        writeln!(
70            f,
71            "rx_monitors_from_refill: {}",
72            self.rx_monitors_from_refill.count
73        )?;
74        writeln!(
75            f,
76            "tx_monitors_to_signal_handler: {}",
77            self.tx_monitors_to_signal_handler.count
78        )?;
79        writeln!(
80            f,
81            "rx_monitors_from_signal_handler: {}",
82            self.rx_monitors_from_signal_handler.count
83        )?;
84        write!(
85            f,
86            "rx_monitors_from_messaging_opt: {}",
87            self.rx_monitors_from_messaging_opt.as_ref().unwrap().count
88        )
89    }
90}
91
92#[cfg(not(feature = "debug_channels"))]
93impl fmt::Display for MonitorsChannels {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        write!(
96            f,
97            "Channel counters are not active. Use --features \"debug_channels\" to enable them."
98        )
99    }
100}