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
124
125
mod commands;
mod connection;
use ea1::connection::Connection;
use std::net::{SocketAddrV4, AddrParseError};
use std::str::FromStr;
use std::io;
use std::error::Error;
use std::io::ErrorKind;
use ea1::commands::{general, network, measurement};
use super::Result;
pub struct EA1 {
conn: Connection,
}
pub trait Mode {
fn measure(&mut self) -> Result<Option<f64>>;
}
pub struct Power<'a> {
handle: &'a mut EA1,
}
impl<'a> Mode for Power<'a> {
fn measure(&mut self) -> Result<Option<f64>> {
let command = measurement::ReadPower::default();
self.handle.conn.measure(command)
}
}
impl EA1 {
pub fn open(device_address: &str) -> io::Result<EA1> {
SocketAddrV4::from_str(device_address)
.map_err(|e| {
io::Error::new(ErrorKind::InvalidInput,
e.description())
})
.and_then(|a|
Connection::new(a.ip().clone(), a.port())
)
.map(|c| EA1 {
conn: c
})
}
fn firmware_version(&mut self) -> Result<String> {
let command = general::Version::default();
self.conn.query_parameter(command)
}
fn device_name(&mut self) -> Result<String> {
let command = network::DeviceUserName::default();
self.conn.query_parameter(command)
}
pub fn new_power_session(&mut self) -> Result<Power> {
let command = measurement::ForcePowerMode::default();
match self.conn.assert(command) {
Ok(_) => Ok(Power { handle: self }),
Err(e) => Err(e),
}
}
}
mod tests {
use std::env;
use std::thread;
use std::time;
use super::EA1;
use super::Mode;
const ea1_address_key: &'static str = "OPHIR_EA1_TEST_ADDR";
#[test]
#[ignore]
fn device_communication() {
let address = env::var(ea1_address_key).unwrap();
let device = EA1::open(&address);
assert!(device.is_ok());
let mut device = device.unwrap();
let version = device.firmware_version();
assert!(version.is_ok());
let measurement = device.new_power_session()
.and_then(|mut s| {
s.measure()
});
assert!(measurement.is_ok());
}
}