-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.rs
84 lines (71 loc) · 1.68 KB
/
share.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
use core::fmt::Error;
use std::fmt;
//#[derive(Debug, Clone)]
pub struct Swarm {
pub devices: Vec<LiveDevice>,
}
impl Swarm {
pub fn new() -> Result<Self, Error> {
Ok(Self {
devices: Vec::new(),
})
}
pub fn add_device(&mut self, device: LiveDevice) {
self.devices.push(device);
}
}
//#[derive(Debug, Clone)]
pub struct LiveDevice {
pub name: String,
pub sensors: Vec<LiveSensor>,
}
impl LiveDevice {
pub fn new(name: String) -> Result<Self, Error> {
Ok(Self {
name,
sensors: Vec::new(),
})
}
pub fn add_sensor(&mut self, sensor: LiveSensor) -> &mut Self {
self.sensors.push(sensor);
self
}
}
pub struct LiveSensor {
pub name: String,
pub hysteresis: f32,
pub telemetry: Box<dyn TelemetryGenerator>,
}
impl fmt::Debug for LiveSensor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sensor")
.field("name", &self.name)
.field("hysteresis", &self.hysteresis)
.finish()
}
}
pub trait TelemetryGenerator: {
fn next_datapoint(&self) -> f32;
fn previous_datapoint(&self) -> f32;
}
#[derive(Debug, Clone)]
pub struct LinearTelemetryGenerator { // --------
pub previous_value: f32,
pub constant: f32,
}
impl TelemetryGenerator for LinearTelemetryGenerator {
fn next_datapoint(&self) -> f32 {
self.constant
}
fn previous_datapoint(&self) -> f32 {
self.previous_value
}
}
impl LinearTelemetryGenerator {
pub fn new(constant: f32) -> Result<Self, Error> {
Ok(Self {
previous_value: 0.0,
constant,
})
}
}