aquarium_control/relays/
relay_error.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::utilities::channel_content::{AquariumDevice, InternalCommand};
10use thiserror::Error;
11
12/// Enum codifies the errors which the program may encounter in preparation or execution of communication with the relay manager
13#[derive(Debug, Error)]
14pub enum RelayError {
15    /// Received an irrelevant command.
16    #[error("[{0}] Received an irrelevant command {1}")]
17    IrrelevantCommand(String, InternalCommand),
18
19    /// Failure occurred in command message creation.
20    #[error("[{0}] Failure occurred in command message creation")]
21    FailureCommandMessageCreation(String),
22
23    /// Error occurred when writing to relay.
24    #[error("[{0}] Error occurred when writing to relay")]
25    WriteError(String),
26
27    /// Error occurred when reading relay state.
28    #[error("[{0}] Error occurred when reading relay state")]
29    ReadError(String),
30
31    /// Encountered an incorrect checksum in communication with relay provider.
32    #[error("[{0}] Encountered an incorrect checksum in communication with relay provider")]
33    IncorrectChecksum(String),
34
35    /// The pulse-command is not allowed for the device.
36    #[error("[{0}] The pulse-command is not allowed for the device {1}")]
37    PulseNotAllowedForDevice(String, AquariumDevice),
38
39    /// The actuation failed after several retries.
40    #[error(
41        "[{location}] Actuation failed (attempt {last_attempt_number}/{max_attempt_allowed})."
42    )]
43    ActuationFailed {
44        location: String,
45        actuation_name: String,
46        last_attempt_number: u64,
47        max_attempt_allowed: u64,
48
49        #[source]
50        source: Box<RelayError>,
51    },
52
53    /// Used to initialize variable. Value is overwritten with real error.
54    #[error("[{0}] Initial value")]
55    InitialValue(String),
56
57    /// Serial port has not been initialized. This is a configuration error.
58    #[error("[{0}] Serial port has not been initialized. This is a configuration error.")]
59    SerialPortNotConfigured(String),
60
61    /// Sending to the serial port failed
62    #[error("[{location}] Sending to serial port failed.")]
63    SendToSerialPortFailed {
64        location: String,
65
66        #[source]
67        source: std::io::Error,
68    },
69
70    /// Flushing the serial port buffer failed.
71    #[error("[{location}] Flushing serial port buffer failed.")]
72    SerialPortFlushFailed {
73        location: String,
74
75        #[source]
76        source: serialport::Error,
77    },
78
79    /// Could not get pin from GPIO interface.
80    #[cfg(all(target_os = "linux", feature = "target_hw"))]
81    #[error("[{location}] Could not get pin {pin_number} from GPIO interface for {device}")]
82    GpioGetPinFailure {
83        location: String,
84        pin_number: u8,
85        device: String,
86
87        #[source]
88        source: rppal::gpio::Error,
89    },
90}