-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
428 lines (368 loc) · 14.4 KB
/
lib.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#![feature(let_chains)]
use std::{collections::HashMap, error::Error};
use chrono::{DateTime, NaiveDateTime, Utc};
use prometheus::{labels, opts, GaugeVec, Registry, Result as PResult, TextEncoder};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use url::Url;
#[macro_use]
extern crate lazy_static;
#[cfg(not(test))]
use log::{debug, info, warn};
#[cfg(test)]
use std::{println as debug, println as warn, println as info};
lazy_static! {
/// Global prometheus registry for all metrics
static ref REGISTRY: Registry = Registry::new_custom(Some("deconz".into()), None)
.expect("Failed to create registry");
static ref INFO: GaugeVec = GaugeVec::new(opts!("gateway_info", "Gateway static info"),
&["name", "apiversion"]).unwrap();
static ref BATTERY: GaugeVec = GaugeVec::new(opts!("battery", "Battery level in percentage"),
&["manufacturername", "modelid", "name", "swversion"]).unwrap();
static ref TEMPERATURE: GaugeVec = GaugeVec::new(opts!("temperature_celsius", "Temperature in degree Celsius"),
&["manufacturername", "modelid", "name", "swversion", "type"]).unwrap();
static ref PRESSURE: GaugeVec = GaugeVec::new(opts!("pressure_hpa", "Pressure in hPa"),
&["manufacturername", "modelid", "name", "swversion", "type"]).unwrap();
static ref HUMIDITY: GaugeVec = GaugeVec::new(opts!("humidity_ratio", "Relative humidity in percentage"),
&["manufacturername", "modelid", "name", "swversion", "type"]).unwrap();
static ref LASTUPDATED: GaugeVec = GaugeVec::new(opts!("sensor_last_updated_ms", "Duration since the sensor was last updated in ms"),
&["manufacturername", "modelid", "name", "swversion"]).unwrap();
static ref LASTSEEN: GaugeVec = GaugeVec::new(opts!("sensor_last_seen_ms", "Duration since the sensor was last seen in ms"),
&["manufacturername", "modelid", "name", "swversion"]).unwrap();
}
/// deCONZ gateway config
#[derive(Serialize, Deserialize, Debug)]
pub struct Gateway {
pub apiversion: String,
pub bridgeid: String,
pub devicename: String,
pub dhcp: bool,
pub gateway: String,
pub ipaddress: String,
pub linkbutton: bool,
pub mac: String,
pub modelid: String,
pub name: String,
pub swversion: String,
pub websocketport: u16,
pub zigbeechannel: u8,
}
/// Sensor config
///
/// Present only for "ZHA{Humidity, Pressure, Switch, Temperature}, null for "Configuration tool"
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct SensorConfig {
pub battery: f64,
pub offset: f64,
pub on: bool,
pub reachable: bool,
}
// State change
//
// Depending on the websocketnotifyall setting: a map containing all or only the
// changed state attributes of a group, light, or sensor resource. Only for
// changed events.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StateChange {
pub temperature: Option<i32>,
pub humidity: Option<i32>,
pub pressure: Option<i32>,
// TODO: Unsure if this is local or UTC, there is no TZ on the timestamp :/
pub lastupdated: NaiveDateTime,
}
/// Sensor info
///
/// See [device
/// spec](https://dresden-elektronik.github.io/deconz-rest-doc/devices/xiaomi/lumi_weather/)
/// for more info.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Sensor {
#[serde(default)]
pub config: Option<SensorConfig>,
pub etag: Option<String>,
// Timestamp of the last power-cycle or rejoin.
pub lastannounced: Option<DateTime<Utc>>,
// Timestamp of the last communication.
#[serde(with = "iso8601_without_seconds")]
pub lastseen: DateTime<Utc>,
pub manufacturername: String,
pub modelid: String,
pub name: String,
#[serde(default)]
pub state: HashMap<String, Value>,
pub swversion: Option<String>,
#[serde(rename = "type")]
pub tipe: String,
pub uniqueid: String,
#[serde(skip)]
dummy: String,
}
/// State carried around between events.
#[derive(Default)]
pub struct State {
sensors: HashMap<String, Sensor>,
}
/// Websocket event from deCONZ for Conbee2
//
// https://dresden-elektronik.github.io/deconz-rest-doc/endpoints/websocket/#message-fields
#[derive(Serialize, Deserialize, Debug)]
pub struct Event {
// "event" - the message holds an event.
#[serde(rename = "t")]
pub type_: String,
// One of added | changed | deleted | scene-called
#[serde(rename = "e")]
pub event: String,
// Resource is one of groups | lights | scenes | sensors
#[serde(rename = "r")]
pub resource: String,
// The id of the resource to which the message relates
pub id: String,
// The uniqueid of the resource to which the message relates
pub uniqueid: String,
// The group id of the resource to which the message relates.
pub gid: Option<String>,
// The scene id of the resource to which the message relates.
pub scid: Option<String>,
// Depending on the `websocketnotifyall` setting: a map containing all or only the changed config attributes of a
// sensor resource. Only for changed events.
#[serde(default)]
pub config: Option<SensorConfig>,
// The (new) name of a resource. Only for changed events.
pub name: Option<String>,
// Depending on the websocketnotifyall setting: a map containing all or only the changed state attributes of a
// group, light, or sensor resource. Only for changed events.
pub state: Option<StateChange>,
// The full group resource. Only for added events of a group resource
#[serde(default)]
pub group: HashMap<String, Value>,
// The full light resource. Only for added events of a light resource.
#[serde(default)]
pub light: HashMap<String, Value>,
// The full sensor resource. Only for added events of a sensor resource.
#[serde(default)]
pub sensor: HashMap<String, Value>,
// Undocumented, but present in API responses.
pub attr: Option<Sensor>,
}
/// Callback function executed for every update event
type Callback = fn(&mut Event, &mut State) -> Result<(), Box<dyn Error>>;
/// Read gateway config from deCONZ REST API
fn gateway(host: &Url, username: &str) -> Result<Gateway, reqwest::Error> {
let mut host = host.clone();
host.set_path(&format!("/api/{}/config", username));
info!("Connecting to API gateway at {host}");
reqwest::blocking::get(host)?.json()
}
/// Discover websocket port from gateway config
fn websocket(host: &Url, username: &str) -> Result<Url, Box<dyn Error>> {
let gw = gateway(host, username)?;
INFO.with(&labels! {"name" => gw.name.as_str(), "apiversion" => gw.apiversion.as_str()})
.set(1.0);
let mut host = host.clone();
host.set_scheme("ws").unwrap();
host.set_port(Some(gw.websocketport)).unwrap();
info!("Discovered websocket port at {}", host);
Ok(host)
}
/// Run listener for websocket events.
pub fn run(host: &Url, username: &str) -> Result<(), Box<dyn Error>> {
let socket = websocket(host, username)?;
register_metrics()?;
stream(&socket, &mut State::default(), process)
}
/// Run a callback for each event received over websocket.
//
// NOTE: A stream of Events would have been much neater than a callback, but Rust makes that API significantly more
// painful to implement. Revisit this later.
fn stream(url: &Url, state: &mut State, callback: Callback) -> Result<(), Box<dyn Error>> {
info!("🔌 Start listening for websocket events at {url}");
let (mut socket, _) = tungstenite::client::connect(url)?;
loop {
match serde_json::from_str::<Event>(socket.read_message()?.to_text()?) {
Ok(mut event) => {
// Failing to process a single event is alright, and this process should just continue. Non recoverable
// errors should bubble up so that the whole stream can be reestablished.
if let Err(err) = callback(&mut event, state) {
warn!("Failed to handle event: `{:?}`: {:?}", event, err)
}
}
Err(err) => {
warn!("Failed to serialize, ignoring message: {:?}", err)
}
}
}
}
/// Process events that can be handled and throw away everything else with a warning log.
///
/// The events structure is a bit messy and not in a good shape. See documentation of `Event` for details.
///
/// Events with `attrs` are used to get human readable labels and stored in a static map for future lookup, when state
/// updates arrive without these attributes.
fn process(e: &mut Event, state: &mut State) -> Result<(), Box<dyn Error>> {
debug!("Received event. id:{} type: {}", e.id, e.type_);
// Sensor attributes contains human friendly names and labels. Store them
// now for future events with no attributes.
if let Some(attr) = &e.attr {
if e.type_ == "event" && e.event == "changed" {
debug!("Updating attrs for {}: {:?}", e.id, attr);
state.sensors.insert(e.id.to_string(), attr.clone());
LASTSEEN
.with(&attr.labels(false))
.set(attr.lastseen.timestamp_millis() as f64);
return Ok(());
}
}
// State often has 2 keys, `lastupdated` and another one that is the actual data. Handle those, ignore the rest
if let Some(change) = &e.state &&
let Some(sensor) = state.sensors.get(&e.id) &&
e.type_ == "event" && e.event == "changed" {
debug!("Update state for sensor '{}': {:?}", sensor.name, change);
LASTUPDATED.with(&sensor.labels(false))
.set(change.lastupdated.timestamp_millis() as f64);
if let Some(p) = change.pressure {
PRESSURE.with(&sensor.labels(true)).set(p as f64);
}
// Xiomi Aqara sensors report the temperature as 2134 instead of
// 21.34°C. Same for humidity. Scale it down.
if let Some(t) = change.temperature {
TEMPERATURE
.with(&sensor.labels(true))
.set(if t.abs() > 100 { t as f64 / 100.0 } else { t as f64 });
}
if let Some(h) = change.humidity {
HUMIDITY
.with(&sensor.labels(true))
.set(if h.abs() > 100 { h as f64 / 100.0 } else { h as f64 });
}
return Ok(());
}
// Config change should be pretty much identical to state change
if let Some(config) = &e.config {
if e.type_ == "event" && e.event == "changed" {
if let Some(s) = state.sensors.get(&e.id) {
debug!(
"Updating battery for sensor '{}': {}",
s.name, config.battery
);
BATTERY.with(&s.labels(false)).set(config.battery);
} else {
warn!("Unknown config change, ignoring it: {:?}", config)
}
return Ok(());
}
}
warn!("Ignoring unknown event {:?}", e);
Ok(())
}
/// Export prometheus metrics as a string
pub fn metrics() -> String {
let encoder = TextEncoder::new();
let metric_families = REGISTRY.gather();
encoder.encode_to_string(&metric_families).unwrap()
}
// Register metrics
fn register_metrics() -> PResult<()> {
info!("Registering metrics",);
REGISTRY.register(Box::new(INFO.clone()))?;
REGISTRY.register(Box::new(BATTERY.clone()))?;
REGISTRY.register(Box::new(TEMPERATURE.clone()))?;
REGISTRY.register(Box::new(PRESSURE.clone()))?;
REGISTRY.register(Box::new(HUMIDITY.clone()))?;
REGISTRY.register(Box::new(LASTUPDATED.clone()))?;
REGISTRY.register(Box::new(LASTSEEN.clone()))
}
impl Sensor {
/// Convert sensor into prometheus labels
fn labels(&self, tipe: bool) -> HashMap<&str, &str> {
vec![
("manufacturername", &self.manufacturername),
("modelid", &self.modelid),
("name", &self.name),
("swversion", self.swversion.as_ref().unwrap_or(&self.dummy)),
if tipe {
("type", &self.tipe)
} else {
("", &self.dummy)
},
]
.into_iter()
.filter(|(name, _)| !name.is_empty())
.map(|(name, value)| (name, value.as_str()))
.collect()
}
}
mod iso8601_without_seconds {
use chrono::{DateTime, TimeZone, Utc};
use serde::{self, Deserialize, Deserializer, Serializer};
const FORMAT: &str = "%Y-%m-%dT%H:%MZ";
pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = format!("{}", date.format(FORMAT));
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
#[allow(deprecated)]
// DateTime::parse_from_str doesn't work with the custom format, so
// stick with these deprecated APIs for now.
Utc.datetime_from_str(&s, FORMAT)
.map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[ignore]
fn read_config() {
let resp = gateway(
&Url::parse("http://nyx.jabid.in:4501").unwrap(),
"381412B455",
);
match resp {
Ok(cfg) => {
assert_eq!(cfg.apiversion, "1.16.0");
assert_eq!(cfg.bridgeid, "00212EFFFF07D25D")
}
Err(e) => {
panic!("Failed to read gateway config from home assistant: {}", e)
}
}
}
#[test]
fn test_process() {
let events = include_str!("../events.json");
register_metrics().unwrap();
let mut state = State::default();
for (linum, event) in events
.lines()
.enumerate()
.filter(|(_, line)| !line.trim().is_empty())
{
let mut e = serde_json::from_str::<Event>(event).unwrap_or_else(|err| {
panic!(
"Failed to parse event in line {} \nEvent: {} \nError: {}",
linum + 1,
&event,
err
)
});
process(&mut e, &mut state)
.unwrap_or_else(|err| panic!("Failed to process event {:?}: {}", &e, err));
}
// Now that all the data is handled, make sure metrics are present.
let m = metrics();
let m = m
.lines()
.filter(|line| !line.starts_with('#'))
.collect::<Vec<_>>();
dbg!(&m);
assert!(m.len() > 10, "Too few metrics exported")
}
}