-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.rs
111 lines (96 loc) · 3.09 KB
/
plugin.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use nom_teltonika::{AVLDatagram, AVLFrame, AVLRecord, Codec};
#[derive(Debug, Clone)]
/// An event from a Teltonika device
pub struct TeltonikaEvent {
/// The codec by the device
pub codec: Codec,
/// The records sent by the device
pub records: Vec<AVLRecord>,
}
impl From<AVLFrame> for TeltonikaEvent {
fn from(value: AVLFrame) -> Self {
Self {
codec: value.codec,
records: value.records,
}
}
}
impl From<&AVLFrame> for TeltonikaEvent {
fn from(value: &AVLFrame) -> Self {
Self {
codec: value.codec,
records: value.records.clone(),
}
}
}
impl From<AVLDatagram> for TeltonikaEvent {
fn from(value: AVLDatagram) -> Self {
Self {
codec: value.codec,
records: value.records,
}
}
}
impl From<&AVLDatagram> for TeltonikaEvent {
fn from(value: &AVLDatagram) -> Self {
Self {
codec: value.codec,
records: value.records.clone(),
}
}
}
/// A trait for listening to events from the server
///
/// ### Thread-safety
///
/// The internal [`Vec`] of [`Plugin`]s is wrapped in an [`std::sync::Arc`] and an [`std::sync::RwLock`], so it can be
/// possible to add plugins while the server is running.
/// Each [`Plugin`] will be modified by atmost one thread, thanks to the use of [`std::sync::Mutex`]
///
/// _See the [`PluginHandle`](crate::PluginHandle) and [`PluginHandles`](crate::PluginHandles) for more info._
///
/// ### Startup/Shutdown
///
/// To implement a startup routine just implement a [`Plugin`]::new method or [`Default`] trait.
/// Same goes for shutdown, just implement the [`Drop`] trait.
pub trait Plugin {
#[allow(unused_variables)]
/// Check if a device is allowed to connect
fn can_teltonika_connect(&mut self, imei: &str) -> bool {
true
}
/// Notify that a new device has connected
fn on_teltonika_connected(&mut self, imei: &str);
/// Notify that a device has disconnected
fn on_teltonika_disconnected(&mut self, imei: &str);
/// Notify that a device has sent an event
fn on_teltonika_event(&mut self, imei: &str, event: &TeltonikaEvent);
}
pub struct DebugPlugin;
impl Plugin for DebugPlugin {
fn on_teltonika_connected(&mut self, imei: &str) {
log::debug!(
target: &format!("{} {imei:15}", module_path!()),
"connected"
);
}
fn on_teltonika_disconnected(&mut self, imei: &str) {
log::debug!(
target: &format!("{} {imei:15}", module_path!()),
"disconnected"
);
}
fn on_teltonika_event(&mut self, imei: &str, event: &TeltonikaEvent) {
let mut event_str = format!("Sent an event:");
for record in event.records.iter() {
event_str.push_str(&format!(
"\r\n{:>10}- Position: {2}, {3}, {4} at {1}",
"", record.timestamp, record.latitude, record.longitude, record.altitude
));
}
log::debug!(
target: &format!("{} {imei:15}", module_path!()),
"{event_str}"
);
}
}