aquarium_control/thermal/
heating_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/// Struct collects the channels for communication to relay manager, signal handler, data logger, schedule check, and messaging.
14pub struct HeatingChannels {
15    /// Sender part of the channel for communication to the relay manager
16    pub tx_heating_to_relay_manager: AquaSender<InternalCommand>,
17
18    /// Receiver part of the channel for communication from the relay manager
19    pub rx_heating_from_relay_manager: AquaReceiver<bool>,
20
21    /// Sender part of the channel for communication to the signal handler
22    pub tx_heating_to_signal_handler: AquaSender<bool>,
23
24    /// Receiver part of the channel for communication from the signal handler
25    pub rx_heating_from_signal_handler: AquaReceiver<InternalCommand>,
26
27    /// Sender part of the channel for communication to the schedule checker
28    pub tx_heating_to_schedule_check: AquaSender<InternalCommand>,
29
30    /// Receiver part of the channel for communication from the schedule checker
31    pub rx_heating_from_schedule_check: AquaReceiver<bool>,
32
33    /// Receiver part of the channel for communication from the messaging
34    pub rx_heating_from_messaging_opt: Option<AquaReceiver<InternalCommand>>,
35}
36
37impl HeatingChannels {
38    /// Sends a command to the relay manager.
39    pub fn send_to_relay_manager(
40        &mut self,
41        command: InternalCommand,
42    ) -> Result<(), AquaChannelError> {
43        self.tx_heating_to_relay_manager.send(command)
44    }
45
46    /// Receives a response from the relay manager.
47    pub fn receive_from_relay_manager(&mut self) -> Result<bool, AquaChannelError> {
48        self.rx_heating_from_relay_manager.recv()
49    }
50
51    /// Sends an acknowledgment to the signal handler.
52    pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
53        self.tx_heating_to_signal_handler.send(ack)
54    }
55
56    /// Sends a command to the schedule checker.
57    pub fn send_to_schedule_check(
58        &mut self,
59        command: InternalCommand,
60    ) -> Result<(), AquaChannelError> {
61        self.tx_heating_to_schedule_check.send(command)
62    }
63
64    /// Receives a response from the schedule checker.
65    pub fn receive_from_schedule_check(&mut self) -> Result<bool, AquaChannelError> {
66        self.rx_heating_from_schedule_check.recv()
67    }
68}
69
70#[cfg(feature = "debug_channels")]
71impl fmt::Display for HeatingChannels {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        writeln!(f, "=== HeatingChannels ===")?;
74        writeln!(
75            f,
76            "tx_heating_to_relay_manager: {}",
77            self.tx_heating_to_relay_manager.count
78        )?;
79        writeln!(
80            f,
81            "rx_heating_from_relay_manager: {}",
82            self.rx_heating_from_relay_manager.count
83        )?;
84        writeln!(
85            f,
86            "tx_heating_to_signal_handler: {}",
87            self.tx_heating_to_signal_handler.count
88        )?;
89        writeln!(
90            f,
91            "rx_heating_from_signal_handler: {}",
92            self.rx_heating_from_signal_handler.count
93        )?;
94        writeln!(
95            f,
96            "tx_heating_to_schedule_check: {}",
97            self.tx_heating_to_schedule_check.count
98        )?;
99        writeln!(
100            f,
101            "rx_heating_from_schedule_check: {}",
102            self.rx_heating_from_schedule_check.count
103        )?;
104        write!(
105            f,
106            "rx_heating_from_messaging_opt: {}",
107            self.rx_heating_from_messaging_opt.as_ref().unwrap().count
108        )
109    }
110}
111
112#[cfg(not(feature = "debug_channels"))]
113impl fmt::Display for HeatingChannels {
114    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115        write!(
116            f,
117            "Channel counters are not active. Use --features \"debug_channels\" to enable them."
118        )
119    }
120}