uhppote_rs/types/
status.rs

1use chrono::{NaiveDate, NaiveTime};
2
3use super::event::Event;
4use crate::messages::GetStatusResponse;
5use anyhow::Result;
6
7/// Status of a [`Device`]
8#[derive(Debug)]
9pub struct Status {
10    pub device_id: u32,
11    pub system_time: NaiveTime,
12    pub system_date: NaiveDate,
13    pub doors: Vec<bool>,
14    pub buttons: Vec<bool>,
15    pub relay_state: u8,
16    pub input_state: u8,
17    pub system_error: u8,
18    pub special_info: u8,
19    pub sequence_number: u32,
20    pub last_event: Option<Event>,
21}
22
23impl TryFrom<GetStatusResponse> for Status {
24    type Error = anyhow::Error;
25    fn try_from(response: GetStatusResponse) -> Result<Self> {
26        let event = match response.event_index {
27            0 => None,
28            x => Some(Event {
29                index: x,
30                event_type: response.event_type.into(),
31                granted: response.granted,
32                door: response.door,
33                direction: response.direction.into(),
34                card_number: response.card_number,
35                timestamp: response.timestamp.try_into()?,
36                reason: response.reason.into(),
37            }),
38        };
39
40        Ok(Status {
41            device_id: response.device_id,
42            system_time: response.system_time.try_into()?,
43            system_date: response.system_date.try_into()?,
44            doors: vec![
45                response.door1_state,
46                response.door2_state,
47                response.door3_state,
48                response.door4_state,
49            ],
50            buttons: vec![
51                response.door1_button,
52                response.door2_button,
53                response.door3_button,
54                response.door4_button,
55            ],
56            relay_state: response.relay_state,
57            input_state: response.input_state,
58            system_error: response.system_error,
59            special_info: response.special_info,
60            sequence_number: response.sequence_id,
61            last_event: event,
62        })
63    }
64}