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
use ::sensor::{
SensorList,
MetzConnectCI4,
RaGasCONO2Mod,
SensorType,
TestSensor,
};
use bincode;
use config::Config;
use error::ServerError;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
#[derive(Debug, Deserialize, Serialize)]
pub struct Server {
service_interval: u32,
sensors: Vec<::runtime_info::sensor::Sensor>,
configuration_path: String,
runtime_info_path: String,
}
impl Server {
pub fn from_runtime_info(cfg: &Config) -> Result<Server, ServerError> {
let mut file = File::open(&cfg.runtime_info_path)?;
let mut s = String::new();
file.read_to_string(&mut s)?;
match bincode::deserialize(&s.as_bytes()) {
Ok(server) => { Ok(server) },
Err(err) => Err(ServerError::CouldNotBuildFromRuntime(err)),
}
}
}
impl From<Server> for ::server::Server {
fn from(server: Server) -> Self {
let mut sensors: SensorList = vec![];
for s in server.sensors {
match s.sensor_type {
SensorType::RaGasCONO2Mod => {
let sensor: RaGasCONO2Mod = s.into();
sensors.push(Arc::new(RwLock::new(Box::new(sensor))));
},
SensorType::MetzConnectCI4 => {
let sensor: MetzConnectCI4 = s.into();
sensors.push(Arc::new(RwLock::new(Box::new(sensor))));
},
SensorType::TestSensor => {
let sensor: TestSensor = s.into();
sensors.push(Arc::new(RwLock::new(Box::new(sensor))));
},
}
}
::server::Server {
service_interval: server.service_interval,
configuration_path: Some(PathBuf::from(server.configuration_path)),
runtime_info_path: Some(PathBuf::from(server.runtime_info_path)),
sensors: sensors,
}
}
}
impl<'r> From<&'r ::server::Server> for Server {
fn from(server: &'r ::server::Server) -> Self {
let mut sensors: Vec<::runtime_info::Sensor> = Vec::new();
for sensor in server.get_sensors() {
sensors.push(sensor.into());
}
let configuration_path = match &server.configuration_path {
Some(path) => path.to_string_lossy().to_string(),
None => "not set".to_string(),
};
let runtime_info_path = match &server.runtime_info_path {
Some(path) => path.to_string_lossy().to_string(),
None => "not set".to_string(),
};
Server {
service_interval: server.service_interval,
configuration_path: configuration_path,
runtime_info_path: runtime_info_path,
sensors: sensors,
}
}
}