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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! The Meter Command Class defines the Commands necessary to read accumulated values in physical
//! units from a water meter or metering device (gas, electric etc.) and thereby enabling automatic
//! meter reading capabilities.
//!
//! Automatic meter reading (AMR), is the technology of automatically collecting data from water
//! meter or energy metering devices and transferring that data to a central database for billing
//! and/or analyzing.
use cmds::{CommandClass, Message, MeterData};
use enum_primitive::FromPrimitive;
use error::{Error, ErrorKind};
use num::PrimInt;
enum_from_primitive! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(non_camel_case_types)]
/// List of the different meter types.
enum MeterType {
Electric = 0x01,
Gas = 0x02,
Water = 0x03,
}}
enum_from_primitive! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(non_camel_case_types)]
/// List of the different electric meter values.
enum ElectricMeter {
kWh = 0x00,
kVAh = 0x01,
W = 0x02,
PulseCount = 0x03,
}}
enum_from_primitive! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(non_camel_case_types)]
/// List of the different gas meter values.
enum GasMeter {
CubicMeters = 0x00,
CubicFeet = 0x01,
PulseCount = 0x03,
}}
enum_from_primitive! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(non_camel_case_types)]
/// List of the different water meter values.
enum WaterMeter {
CubicMeters = 0x00,
CubicFeet = 0x01,
USGallons = 0x02,
PulseCount = 0x03,
}}
#[derive(Debug, Clone)]
/// Meter Command Class
pub struct Meter;
impl Meter {
/// The Meter Get Command is used to request the accumulated consumption in physical units
/// from a metering device.
pub fn get<N>(node_id: N) -> Message
where
N: Into<u8>,
{
// _________________________________________________________________
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | Command Class = COMMAND_CLASS_METER(0x32) |
// | Command = METER_GET(0x01) |
// -----------------------------------------------------------------
Message::new(node_id.into(), CommandClass::METER, 0x01, vec![])
}
/// The Meter Get Command is used to request the accumulated consumption in physical units
/// from a metering device.
pub fn get_v2<N, S>(node_id: N, scale: S) -> Message
where
N: Into<u8>,
S: Into<MeterData>,
{
// _________________________________________________________________
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | Command Class = COMMAND_CLASS_METER(0x32) |
// | Command = METER_GET(0x01) |
// | Reserved | Scale | Reserved |
// -----------------------------------------------------------------
Message::new(
node_id.into(),
CommandClass::METER,
0x01,
vec![(scale.into().get_scale() << 3)],
)
}
/// The Meter Report Command is used to advertise a meter reading.
pub fn report<M>(msg: M) -> Result<MeterData, Error>
where
M: Into<Vec<u8>>,
{
// _________________________________________________________________
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | Command Class = COMMAND_CLASS_METER(0x32) |
// | Command = METER_GET(0x01) |
// | Meter Type |
// | Precision | Scale | Size |
// | Meter Value 1 |
// | ... |
// | Meter Value n |
// -----------------------------------------------------------------
// get the message
let msg = msg.into();
// the message need to be exact 6 digits long
if msg.len() < 8 {
return Err(Error::new(ErrorKind::UnknownZWave, "Message is to short"));
}
// check the CommandClass and command
if msg[3] != CommandClass::METER as u8 || msg[4] != 0x02 {
return Err(Error::new(
ErrorKind::UnknownZWave,
"Answer contained wrong command class",
));
}
// get the meter type
let typ = MeterType::from_u8(msg[5]).ok_or(Error::new(
ErrorKind::UnknownZWave,
"Answer contained wrong meter type",
))?;
// get the precission
let (precision, scale, size) = Meter::get_precision_scale_size(msg[6]);
// check the message length coorectly
if msg.len() != 7 + size as usize {
return Err(Error::new(
ErrorKind::UnknownZWave,
"Message has the wrong length",
));
}
// get the value
let value = Meter::calc_value(&msg[7..7 + size as usize], precision);
// return the value in MeterData format
Meter::to_meter_data(value, typ, scale)
}
/// The Meter Report Command is used to advertise a meter reading.
pub fn report_v2<M>(msg: M) -> Result<(MeterData, u16, MeterData), Error>
where
M: Into<Vec<u8>>,
{
// _________________________________________________________________
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | Command Class = COMMAND_CLASS_METER(0x32) |
// | Command = METER_GET(0x01) |
// | none | Rate Type | Meter Type |
// | Precision | Scale | Size |
// | Meter Value 1 |
// | ... |
// | Meter Value n |
// | Delta Time 1 |
// | Delta Time 2 |
// | Previous Meter Value 1 |
// | ... |
// | Previous Meter Value n |
// -----------------------------------------------------------------
// get the message
let msg = msg.into();
println!("Message {:?}", msg);
// the message need to be exact 6 digits long
if msg.len() < 8 {
return Err(Error::new(ErrorKind::UnknownZWave, "Message is to short"));
}
// check the CommandClass and command
if msg[3] != CommandClass::METER as u8 || msg[4] != 0x02 {
return Err(Error::new(
ErrorKind::UnknownZWave,
"Answer contained wrong command class",
));
}
// get the meter type
let (_, typ) = Meter::get_rate_meter_type(msg[5])?;
// get the precission, scale and size
let (precision, scale, size) = Meter::get_precision_scale_size(msg[6]);
// check the message length coorectly
if msg.len() < 9 + size as usize {
return Err(Error::new(
ErrorKind::UnknownZWave,
"Message has the wrong length",
));
}
// get the value
let value = Meter::calc_value(&msg[7..7 + size as usize], precision);
// get the time between this and the last report
let time = ((msg[7 + size as usize] as u16) << 8) | msg[8 + size as usize] as u16;
// get the pre value
let pre_value;
if time == 0x00 || msg.len() < 10 + (2 * size) as usize {
pre_value = 0.0;
} else {
pre_value = Meter::calc_value(
&msg[10 + size as usize..10 + (2 * size) as usize],
precision,
);
}
// return the value in MeterData format
Ok((
Meter::to_meter_data(pre_value, typ, scale)?,
time,
Meter::to_meter_data(value, typ, scale)?,
))
}
// extract the precision, scale and size as bit information
fn get_precision_scale_size(input: u8) -> (u8, u8, u8) {
(
(input >> 5),
((input >> 3) & 0b00000011),
(input & 0b00000111),
)
}
/// generate the value out of the scale and byte vector
fn calc_value(bytes: &[u8], precision: u8) -> f64 {
// pow the prevision and set as f64
let precision = (10.pow(precision as u32)) as f64;
// transform for one byte
if bytes.len() == 1 {
return (bytes[0] as i8) as f64 / precision;
}
// transform for two bytes
if bytes.len() == 2 {
return (((bytes[0] as i16) << 8) | bytes[1] as i16) as f64 / precision;
}
// transform for four bytes
if bytes.len() == 4 {
return (((((bytes[0] as i32) << 24) | (bytes[1] as i32) << 16) | (bytes[2] as i32) << 8)
| (bytes[3] as i32)) as f64 / precision;
}
0.0
}
/// format the value into the right MeterData format
fn to_meter_data(data: f64, typ: MeterType, scale: u8) -> Result<MeterData, Error> {
if typ == MeterType::Electric && scale == ElectricMeter::kWh as u8 {
return Ok(MeterData::Electric_kWh(data));
} else if typ == MeterType::Electric && scale == ElectricMeter::kVAh as u8 {
return Ok(MeterData::Electric_kVAh(data));
} else if typ == MeterType::Electric && scale == ElectricMeter::W as u8 {
return Ok(MeterData::Electric_W(data));
} else if typ == MeterType::Electric && scale == ElectricMeter::PulseCount as u8 {
return Ok(MeterData::Electric_PulseCount(data));
} else if typ == MeterType::Gas && scale == GasMeter::CubicMeters as u8 {
return Ok(MeterData::Gas_meter2(data));
} else if typ == MeterType::Gas && scale == GasMeter::CubicFeet as u8 {
return Ok(MeterData::Gas_feet2(data));
} else if typ == MeterType::Gas && scale == GasMeter::PulseCount as u8 {
return Ok(MeterData::Gas_PulseCount(data));
} else if typ == MeterType::Water && scale == WaterMeter::CubicMeters as u8 {
return Ok(MeterData::Water_meter2(data));
} else if typ == MeterType::Water && scale == WaterMeter::CubicFeet as u8 {
return Ok(MeterData::Water_feet2(data));
} else if typ == MeterType::Water && scale == WaterMeter::USGallons as u8 {
return Ok(MeterData::Water_Gallons(data));
} else if typ == MeterType::Water && scale == WaterMeter::PulseCount as u8 {
return Ok(MeterData::Water_PulseCount(data));
}
// return error if no right match was found
Err(Error::new(
ErrorKind::UnknownZWave,
"The meter value can't be created",
))
}
fn get_rate_meter_type(input: u8) -> Result<(u8, MeterType), Error> {
let typ = MeterType::from_u8(input & 0b00011111).ok_or(Error::new(
ErrorKind::UnknownZWave,
"Answer contained wrong meter type",
))?;
let rate = (input >> 5) & 0b00000011;
Ok((rate, typ))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
/// test the right conversion
fn precision_scale_size() {
assert_eq!(
(0x00, 0x00, 0x00),
Meter::get_precision_scale_size(0b00000000)
);
assert_eq!(
(0x07, 0x00, 0x00),
Meter::get_precision_scale_size(0b11100000)
);
assert_eq!(
(0x01, 0x03, 0x00),
Meter::get_precision_scale_size(0b00111000)
);
assert_eq!(
(0x01, 0x01, 0x00),
Meter::get_precision_scale_size(0b00101000)
);
assert_eq!(
(0x01, 0x01, 0x07),
Meter::get_precision_scale_size(0b00101111)
);
assert_eq!(
(0x01, 0x01, 0x01),
Meter::get_precision_scale_size(0b00101001)
);
}
#[test]
/// test the right conversion
fn calc_value() {
assert_eq!(0.0, Meter::calc_value(&[0x00], 0));
assert_eq!(1.27, Meter::calc_value(&[0x7F], 2));
assert_eq!(-12.8, Meter::calc_value(&[0x80], 1));
assert_eq!(0.00, Meter::calc_value(&[0x00, 0x00], 0));
assert_eq!(32.767, Meter::calc_value(&[0x7F, 0xFF], 3));
assert_eq!(-327.68, Meter::calc_value(&[0x80, 0x00], 2));
assert_eq!(0.00, Meter::calc_value(&[0x00, 0x00, 0x00, 0x00], 0));
assert_eq!(2147483.647, Meter::calc_value(&[0x7F, 0xFF, 0xFF, 0xFF], 3));
assert_eq!(
-21474836.48,
Meter::calc_value(&[0x80, 0x00, 0x00, 0x00], 2)
);
}
}