1use chrono::NaiveDateTime;
2
3use super::direction::Direction;
4use crate::messages::GetEventResponse;
5use anyhow::Result;
6
7#[derive(Debug)]
9pub struct Event {
10 pub timestamp: NaiveDateTime,
11 pub index: u32,
12 pub event_type: EventType,
13 pub granted: bool,
14 pub door: u8,
15 pub direction: Direction,
16 pub card_number: u32,
17 pub reason: EventReason,
18}
19
20impl TryFrom<GetEventResponse> for Event {
21 type Error = anyhow::Error;
22 fn try_from(response: GetEventResponse) -> Result<Self> {
23 Ok(Event {
24 timestamp: response.timestamp.try_into()?,
25 index: response.index,
26 event_type: response.type_.into(),
27 granted: response.granted,
28 door: response.door,
29 direction: response.direction.into(),
30 card_number: response.card_number,
31 reason: response.reason.into(),
32 })
33 }
34}
35
36#[derive(Debug)]
37pub enum EventType {
38 None = 0,
39 Swipe = 1,
40 Door = 2,
41 Alarm = 3,
42 Overwritten = 255,
43}
44
45impl From<u8> for EventType {
46 fn from(event_type: u8) -> EventType {
47 match event_type {
48 0 => EventType::None,
49 1 => EventType::Swipe,
50 2 => EventType::Door,
51 3 => EventType::Alarm,
52 255 => EventType::Overwritten,
53 _ => EventType::None,
54 }
55 }
56}
57
58#[derive(Debug)]
59pub enum EventReason {
60 None = 0,
61 Swipe = 1,
62 Denied = 5,
63 NoAccessRights = 6,
64 IncorrectPassword = 7,
65 AntiPassback = 8,
66 MoreCards = 9,
67 FirstCardOpen = 10,
68 DoorIsNormallyClosed = 11,
69 Interlock = 12,
70 NotInAllowedTimePeriod = 13,
71 InvalidTimeZone = 15,
72 AccessDenied = 18,
73 PushButtonOk = 20,
74 DoorOpen = 23,
75 DoorClosed = 24,
76 DoorOpenedSupervisorPassword = 25,
77 ControllerPowerOn = 28,
78 ControllerReset = 29,
79 PushbuttonInvalidDoorLocked = 31,
80 PushbuttonInvalidDoorOffline = 32,
81 PushbuttonInvalidDoorInterlock = 33,
82 PushbuttonInvalidDoorThreat = 34,
83 DoorOpenTooLong = 37,
84 ForcedOpen = 38,
85 Fire = 39,
86 ForcedClosed = 40,
87 TheftPrevention = 41,
88 TwentyFourSevenZone = 42,
89 Emergency = 43,
90 RemoteOpenDoor = 44,
91 RemoteOpenDoorUsbReader = 45,
92}
93
94impl From<u8> for EventReason {
95 fn from(reason: u8) -> EventReason {
96 match reason {
97 0 => EventReason::None,
98 1 => EventReason::Swipe,
99 5 => EventReason::Denied,
100 6 => EventReason::NoAccessRights,
101 7 => EventReason::IncorrectPassword,
102 8 => EventReason::AntiPassback,
103 9 => EventReason::MoreCards,
104 10 => EventReason::FirstCardOpen,
105 11 => EventReason::DoorIsNormallyClosed,
106 12 => EventReason::Interlock,
107 13 => EventReason::NotInAllowedTimePeriod,
108 15 => EventReason::InvalidTimeZone,
109 18 => EventReason::AccessDenied,
110 20 => EventReason::PushButtonOk,
111 23 => EventReason::DoorOpen,
112 24 => EventReason::DoorClosed,
113 25 => EventReason::DoorOpenedSupervisorPassword,
114 28 => EventReason::ControllerPowerOn,
115 29 => EventReason::ControllerReset,
116 31 => EventReason::PushbuttonInvalidDoorLocked,
117 32 => EventReason::PushbuttonInvalidDoorOffline,
118 33 => EventReason::PushbuttonInvalidDoorInterlock,
119 34 => EventReason::PushbuttonInvalidDoorThreat,
120 37 => EventReason::DoorOpenTooLong,
121 38 => EventReason::ForcedOpen,
122 39 => EventReason::Fire,
123 40 => EventReason::ForcedClosed,
124 41 => EventReason::TheftPrevention,
125 42 => EventReason::TwentyFourSevenZone,
126 43 => EventReason::Emergency,
127 44 => EventReason::RemoteOpenDoor,
128 45 => EventReason::RemoteOpenDoorUsbReader,
129 _ => EventReason::None,
130 }
131 }
132}