-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasurements.rs
123 lines (114 loc) · 4.18 KB
/
measurements.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
112
113
114
115
116
117
118
119
120
121
122
123
use crate::telegrams::r09::R09SaveTelegram;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
/// Unfinished measurement of a vehicle. Mostly used by **trekkie** or **lofi** for correlation.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct MeasurementInterval {
/// Time the vehicle was entered.
pub start: Option<NaiveDateTime>,
/// Time the vehicle was left.
pub stop: Option<NaiveDateTime>,
/// Line (ger. linie) of the tracked vehicle.
pub line: Option<i32>,
/// Run (ger. kurs nummer) of the tracked vehicle.
pub run: Option<i32>,
/// Integer representing the region in which the vehicle was tracked.
pub region: Option<i64>,
}
/// The FinishedMeasurementInterval struct is primarily used in **Wartrammer-40k** and **lofi**. It
/// defines the time interval and which vehicle was taken while data is actively being recorded.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct FinishedMeasurementInterval {
/// Time the the vehicle was entered.
#[serde(with = "crate::time_serializer")]
pub start: NaiveDateTime,
/// Time the vehicle was left.
#[serde(with = "crate::time_serializer")]
pub stop: NaiveDateTime,
/// Line (ger. linie) of the tracked vehicle.
pub line: i32,
/// Run (ger. kurs nummer) of the tracked vehicle.
pub run: i32,
/// Integer representing the region in which the vehicle was tracked.
pub region: i64,
}
#[allow(missing_docs)] // allowed since enum variants are pretty self-explanatory
/// This enum signifies error during conversion of [`MeasurementInterval`] into
/// [`FinishedMeasurementInterval`]. Variants are pretty self-explanatory
pub enum MeasuerementIntervalError {
MissingStartValue,
MissingStopValue,
MissingLineValue,
MissingRunValue,
MissingRegionValue,
}
impl TryFrom<MeasurementInterval> for FinishedMeasurementInterval {
type Error = MeasuerementIntervalError;
fn try_from(val: MeasurementInterval) -> Result<Self, Self::Error> {
// match every value, if any of them is None, return corresponding error
let start = match val.start {
Some(v) => v,
None => {
return Err(MeasuerementIntervalError::MissingStartValue);
}
};
let stop = match val.stop {
Some(v) => v,
None => {
return Err(MeasuerementIntervalError::MissingStopValue);
}
};
let line = match val.line {
Some(v) => v,
None => {
return Err(MeasuerementIntervalError::MissingLineValue);
}
};
let run = match val.run {
Some(v) => v,
None => {
return Err(MeasuerementIntervalError::MissingRunValue);
}
};
let region = match val.region {
Some(v) => v,
None => {
return Err(MeasuerementIntervalError::MissingRegionValue);
}
};
Ok(FinishedMeasurementInterval {
start,
stop,
line,
run,
region,
})
}
}
impl FinishedMeasurementInterval {
#[deprecated(
since = "0.8.1",
note = "Please use TryFrom<MeasurementInterval> trait from now on!"
)]
/// Converts the intermediate representation into the final measurement.
pub fn from_measurement(measurement: MeasurementInterval) -> FinishedMeasurementInterval {
FinishedMeasurementInterval {
start: measurement.start.unwrap(),
stop: measurement.stop.unwrap(),
line: measurement.line.unwrap(),
run: measurement.run.unwrap(),
region: measurement.region.unwrap(),
}
}
/// Checks if a given Telegram corresponds to the measurement interval.
pub fn fits(&self, telegram: &R09SaveTelegram) -> bool {
if telegram.line.is_none() || telegram.run_number.is_none() {
return false;
}
self.start < telegram.time
&& telegram.time < self.stop
&& telegram.line.unwrap() == self.line
&& telegram.run_number.unwrap() == self.run
&& telegram.region == self.region
}
}