Round measures to their significant bits, remove debug statements

This commit is contained in:
Maxime Augier 2024-04-20 12:30:41 +02:00
parent eedf62e304
commit cc63f34e9e

View File

@ -29,11 +29,13 @@ struct Data {
impl Data {
fn from_pkt(addr: Address, b: &[u8]) -> Data {
eprintln!("Packet {:?}", b);
let round = |v: f32| (v*100.0).round() / 100.0;
Data {
addr,
temperature: (u16::from_le_bytes([b[2], b[3]]) as f32) * 175.0 / 65535.0 - 45.0,
humidity: (u16::from_le_bytes([b[4], b[5]]) as f32) * 100.0 / 65535.0,
temperature: round((u16::from_le_bytes([b[2], b[3]]) as f32) * 175.0 / 65535.0 - 45.0),
humidity: round((u16::from_le_bytes([b[4], b[5]]) as f32) * 100.0 / 65535.0),
co2: u16::from_le_bytes([b[6], b[7]]),
}
}
@ -61,9 +63,9 @@ impl DataStore {
for (_, (data, point)) in self.devices.iter() {
let Data { addr, temperature, humidity, co2 } = data;
let time = point.duration_since(UNIX_EPOCH).expect("Time went backwards").as_millis();
scrape += &format!("sensirion_temperature_celsius{{id=\"{addr}\"}} {temperature} {time}\n");
scrape += &format!("sensirion_humidity_percent{{id=\"{addr}\"}} {humidity} {time}\n");
scrape += &format!("sensirion_co2_ppm{{id=\"{addr}\"}} {co2} {time}\n");
scrape += &format!("sensirion_temperature_celsius{{id=\"{addr}\"}} {temperature} {time}\n");
scrape += &format!("sensirion_humidity_percent{{id=\"{addr}\"}} {humidity} {time}\n");
scrape += &format!("sensirion_co2_ppm{{id=\"{addr}\"}} {co2} {time}\n");
}
scrape