aquarium_control/relays/
relay_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::utilities::channel_content::InternalCommand;
11use std::fmt;
12
13/// Container for channels used by relay manager
14pub struct RelayManagerChannels {
15    /// sender part of the channel for communication to the refill thread
16    pub tx_relay_manager_to_refill: AquaSender<bool>,
17    /// receiver part of the channel for communication from the refill thread
18    pub rx_relay_manager_from_refill: AquaReceiver<InternalCommand>,
19
20    /// sender part of the channel for communication to the heating thread
21    pub tx_relay_manager_to_heating: AquaSender<bool>,
22    /// receiver part of the channel for communication from the heating thread
23    pub rx_relay_manager_from_heating: AquaReceiver<InternalCommand>,
24
25    /// sender part of the channel for communication to the ventilation thread
26    pub tx_relay_manager_to_ventilation: AquaSender<bool>,
27    /// receiver part of the channel for communication from the ventilation thread
28    pub rx_relay_manager_from_ventilation: AquaReceiver<InternalCommand>,
29
30    /// sender part of the channel for communication to the Balling dosing thread
31    pub tx_relay_manager_to_balling: AquaSender<bool>,
32    /// receiver part of the channel for communication from the Balling dosing thread
33    pub rx_relay_manager_from_balling: AquaReceiver<InternalCommand>,
34
35    /// sender part of the channel for communication to the feed thread
36    pub tx_relay_manager_to_feed: AquaSender<bool>,
37    /// receiver part of the channel for communication from the feed thread
38    pub rx_relay_manager_from_feed: AquaReceiver<InternalCommand>,
39
40    /// sender part of the channel for communication to the signal handler
41    pub tx_relay_manager_to_signal_handler: AquaSender<bool>,
42    /// receiver part of the channel for communication from the signal handler
43    pub rx_relay_manager_from_signal_handler: AquaReceiver<InternalCommand>,
44}
45
46impl RelayManagerChannels {
47    /// Sends a response to the refill module.
48    #[allow(unused)]
49    pub fn send_to_refill(&mut self, ack: bool) -> Result<(), AquaChannelError> {
50        self.tx_relay_manager_to_refill.send(ack)
51    }
52
53    /// Receives a command from the refill module (non-blocking).
54    #[allow(unused)]
55    pub fn receive_from_refill(&mut self) -> Result<InternalCommand, AquaChannelError> {
56        self.rx_relay_manager_from_refill.try_recv()
57    }
58
59    /// Sends a response to the heating module.
60    #[allow(unused)]
61    pub fn send_to_heating(&mut self, ack: bool) -> Result<(), AquaChannelError> {
62        self.tx_relay_manager_to_heating.send(ack)
63    }
64
65    /// Receives a command from the heating module (non-blocking).
66    #[allow(unused)]
67    pub fn receive_from_heating(&mut self) -> Result<InternalCommand, AquaChannelError> {
68        self.rx_relay_manager_from_heating.try_recv()
69    }
70
71    /// Sends a response to the ventilation module.
72    #[allow(unused)]
73    pub fn send_to_ventilation(&mut self, ack: bool) -> Result<(), AquaChannelError> {
74        self.tx_relay_manager_to_ventilation.send(ack)
75    }
76
77    /// Receives a command from the ventilation module (non-blocking).
78    #[allow(unused)]
79    pub fn receive_from_ventilation(&mut self) -> Result<InternalCommand, AquaChannelError> {
80        self.rx_relay_manager_from_ventilation.try_recv()
81    }
82
83    /// Sends a response to the balling module.
84    #[allow(unused)]
85    pub fn send_to_balling(&mut self, ack: bool) -> Result<(), AquaChannelError> {
86        self.tx_relay_manager_to_balling.send(ack)
87    }
88
89    /// Receives a command from the balling module (non-blocking).
90    #[allow(unused)]
91    pub fn receive_from_balling(&mut self) -> Result<InternalCommand, AquaChannelError> {
92        self.rx_relay_manager_from_balling.try_recv()
93    }
94
95    /// Sends a response to the feed module.
96    #[allow(unused)]
97    pub fn send_to_feed(&mut self, ack: bool) -> Result<(), AquaChannelError> {
98        self.tx_relay_manager_to_feed.send(ack)
99    }
100
101    /// Receives a command from the feed module (non-blocking).
102    #[allow(unused)]
103    pub fn receive_from_feed(&mut self) -> Result<InternalCommand, AquaChannelError> {
104        self.rx_relay_manager_from_feed.try_recv()
105    }
106
107    /// Sends a response to the signal handler.
108    #[allow(unused)]
109    pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
110        self.tx_relay_manager_to_signal_handler.send(ack)
111    }
112
113    /// Receives a command from the signal handler (non-blocking).
114    #[allow(unused)]
115    pub fn receive_from_signal_handler(&mut self) -> Result<InternalCommand, AquaChannelError> {
116        self.rx_relay_manager_from_signal_handler.try_recv()
117    }
118}
119
120#[cfg(feature = "debug_channels")]
121impl fmt::Display for RelayManagerChannels {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        writeln!(f, "=== RelayManagerChannels ===")?;
124        writeln!(
125            f,
126            "tx_relay_manager_to_refill: {}",
127            self.tx_relay_manager_to_refill.count
128        )?;
129        writeln!(
130            f,
131            "rx_relay_manager_from_refill: {}",
132            self.rx_relay_manager_from_refill.count
133        )?;
134        writeln!(
135            f,
136            "tx_relay_manager_to_heating: {}",
137            self.tx_relay_manager_to_heating.count
138        )?;
139        writeln!(
140            f,
141            "rx_relay_manager_from_heating: {}",
142            self.rx_relay_manager_from_heating.count
143        )?;
144        writeln!(
145            f,
146            "tx_relay_manager_to_ventilation: {}",
147            self.tx_relay_manager_to_ventilation.count
148        )?;
149        writeln!(
150            f,
151            "rx_relay_manager_from_ventilation: {}",
152            self.rx_relay_manager_from_ventilation.count
153        )?;
154        writeln!(
155            f,
156            "tx_relay_manager_to_balling: {}",
157            self.tx_relay_manager_to_balling.count
158        )?;
159        writeln!(
160            f,
161            "rx_relay_manager_from_balling: {}",
162            self.rx_relay_manager_from_balling.count
163        )?;
164        writeln!(
165            f,
166            "tx_relay_manager_to_feed: {}",
167            self.tx_relay_manager_to_feed.count
168        )?;
169        writeln!(
170            f,
171            "rx_relay_manager_from_feed: {}",
172            self.rx_relay_manager_from_feed.count
173        )?;
174        writeln!(
175            f,
176            "tx_relay_manager_to_signal_handler: {}",
177            self.tx_relay_manager_to_signal_handler.count
178        )?;
179        write!(
180            f,
181            "rx_relay_manager_from_signal_handler: {}",
182            self.rx_relay_manager_from_signal_handler.count
183        )
184    }
185}
186
187#[cfg(not(feature = "debug_channels"))]
188impl fmt::Display for RelayManagerChannels {
189    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190        write!(
191            f,
192            "Channel counters are not active. Use --features \"debug_channels\" to enable them."
193        )
194    }
195}