Skip to main content

haltian_sdk/
error.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4#[non_exhaustive]
5pub enum HaltianError {
6    /// Invalid TSM ID for the given device type
7    InvalidTsmId {
8        /// The invalid TSM ID
9        tsm_id: u16,
10        /// Expected device type
11        device_type: &'static str,
12    },
13    /// Invalid TSM event type
14    InvalidTsmEvent(u8),
15    /// Serialization error
16    SerializationError,
17    /// Deserialization error
18    DeserializationError,
19    /// Invalid sensor data
20    InvalidSensorData {
21        /// Description of the validation error
22        message: &'static str,
23    },
24    /// Configuration validation error
25    ConfigurationError {
26        /// Description of the configuration error
27        message: &'static str,
28    },
29}
30
31impl core::fmt::Display for HaltianError {
32    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33        match self {
34            HaltianError::InvalidTsmId {
35                tsm_id,
36                device_type,
37            } => {
38                write!(
39                    f,
40                    "Invalid TSM ID {} for device type {}",
41                    tsm_id, device_type
42                )
43            }
44            HaltianError::InvalidTsmEvent(event) => {
45                write!(f, "Invalid TSM event type: {}", event)
46            }
47            HaltianError::SerializationError => {
48                write!(f, "Failed to serialize message")
49            }
50            HaltianError::DeserializationError => {
51                write!(f, "Failed to deserialize message")
52            }
53            HaltianError::InvalidSensorData { message } => {
54                write!(f, "Invalid sensor data: {}", message)
55            }
56            HaltianError::ConfigurationError { message } => {
57                write!(f, "Configuration error: {}", message)
58            }
59        }
60    }
61}
62
63/// Error types reported by Thingsee devices
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[repr(u8)]
66pub enum DeviceErrorType {
67    /// System error
68    System = 1,
69    /// Sensor error
70    Sensor = 2,
71    /// Communication error
72    Communication = 3,
73    /// Configuration error
74    Configuration = 4,
75    /// Battery error
76    Battery = 5,
77    /// Memory error
78    Memory = 6,
79    /// Unknown error
80    Unknown = 255,
81}
82
83/// Error causes reported by Thingsee devices
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
85#[repr(u8)]
86pub enum DeviceErrorCause {
87    /// Hardware failure
88    HardwareFailure = 1,
89    /// Software failure
90    SoftwareFailure = 2,
91    /// Calibration failure
92    CalibrationFailure = 3,
93    /// Communication timeout
94    CommunicationTimeout = 4,
95    /// Invalid configuration
96    InvalidConfiguration = 5,
97    /// Low battery
98    LowBattery = 6,
99    /// Memory full
100    MemoryFull = 7,
101    /// Sensor not responding
102    SensorNotResponding = 8,
103    /// Unknown cause
104    Unknown = 255,
105}
106
107/// Device error information (TSM ID 1403)
108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
109pub struct DeviceErrorInfo {
110    /// Type of error
111    #[serde(rename = "errorType")]
112    pub error_type: DeviceErrorType,
113    /// Cause of error
114    #[serde(rename = "errorCause")]
115    pub error_cause: DeviceErrorCause,
116}
117
118pub type Result<T> = core::result::Result<T, HaltianError>;
119
120pub trait Validate {
121    /// Validate the data and return an error if invalid
122    fn validate(&self) -> Result<()>;
123}
124
125pub trait Configure {
126    /// Apply configuration and validate parameters
127    fn configure(&mut self, config: &Self) -> Result<()>;
128}