Implement basic packed decoding checks.

master
flabbergast 4 years ago
parent 10f3bc4ad8
commit 245ca6f204
  1. 2
      README.md
  2. 88
      src/decode_jee.rs

@ -40,7 +40,7 @@ The program panics/errors out in some cases and does not in some others. In part
* If MongoDB is enabled and the connection to the database and/or posting a record to it fails, the whole program panics.
* If writing to `csv` is enabled and writing to a file fails, the whole program panics.
* If MQTT is enabled and the initial connection to the server fails, the program fails; however a failure of an individual publish message is only logged and the program continues.
* If decoding of an incoming message (from the gateway) fails at any point (e.g. "not-a-message", "not-enough-bytes", "corrupted-data"), it is simply logged and ignored further. (TODO)
* If decoding of an incoming message (from the gateway) fails at any point (e.g. "not-a-message", "not-enough-bytes", "corrupted-data"), it is simply logged and ignored further.

@ -36,54 +36,79 @@ impl DeviceType {
//- 2bytes|LE|temp*100
//- 2bytes|LE|humid*100
//- 2bytes|LE|volt*1000 {1.5--4.0}
DeviceType::TempHumBattV1 =>
Ok(vec![
SensorDatum::Temperature(le64fs(&bytes[1..3],100)),
SensorDatum::Humidity(le64f(&bytes[3..5],100)),
SensorDatum::BatteryVolts(le64f(&bytes[5..7],1000)),
]),
DeviceType::TempHumBattV1 => {
if bytes.len() < 6 {
bail!("not enough bytes in message from TempHumBattV1 type");
} else { // TODO: perhaps should also implement sanity checks on the resulting values
Ok(vec![
SensorDatum::Temperature(le64fs(&bytes[1..3],100)),
SensorDatum::Humidity(le64f(&bytes[3..5],100)),
SensorDatum::BatteryVolts(le64f(&bytes[5..7],1000)),
])
}
},
//TempHumBattV2
//- 2bytes|LE|temp*100
//- 2bytes|LE|humid*100
//- 1byte|volt*100 {0.5--2.0}
DeviceType::TempHumBattV2 =>
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],100)),
SensorDatum::Humidity(le64f(&bytes[3..5],100)),
SensorDatum::BatteryVolts((bytes[5] as f64)/100.0),
]),
DeviceType::TempHumBattV2 => {
if bytes.len() < 5 {
bail!("not enough bytes in message from TempHumBattV2 type");
} else {
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],100)),
SensorDatum::Humidity(le64f(&bytes[3..5],100)),
SensorDatum::BatteryVolts((bytes[5] as f64)/100.0),
])
}
},
//TempBattV1
//- 2bytes|LE|temp*1000
//- 2bytes|LE|volt*1000 {1.0--4.0}
DeviceType::TempBattV1 =>
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],1000)),
SensorDatum::BatteryVolts(le64f(&bytes[3..5],1000)),
]),
DeviceType::TempBattV1 => {
if bytes.len() < 4 {
bail!("not enough bytes in message from TempBattV1 type");
} else {
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],1000)),
SensorDatum::BatteryVolts(le64f(&bytes[3..5],1000)),
])
}
},
//TempBattV2
//- 2bytes|LE|temp*16
//- 2bytes|LE|volt*1000 {1.5--4.0}
DeviceType::TempBattV2 =>
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],16)),
SensorDatum::BatteryVolts(le64f(&bytes[3..5],1000)),
]),
DeviceType::TempBattV2 => {
if bytes.len() < 4 {
bail!("not enough bytes in message from TempBattV2 type");
} else {
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],16)),
SensorDatum::BatteryVolts(le64f(&bytes[3..5],1000)),
])
}
},
//TempHumLuxBattV1
//- 2bytes|LE|temp*100
//- 2bytes|LE|humid*100
//- 2bytes|LE|lux
//- 2bytes|LE|volt*1000 {1.5--4.0}
DeviceType::TempHumLuxBattV1 =>
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],100)),
SensorDatum::Humidity(le64f(&bytes[3..5],100)),
SensorDatum::Light(le64f(&bytes[5..7],1)),
SensorDatum::BatteryVolts(le64f(&bytes[7..9],1000)),
]),
DeviceType::TempHumLuxBattV1 => {
if bytes.len() < 8 {
bail!("not enough bytes in message from TempBattV2 type");
} else {
Ok(vec![
SensorDatum::Temperature(le64f(&bytes[1..3],100)),
SensorDatum::Humidity(le64f(&bytes[3..5],100)),
SensorDatum::Light(le64f(&bytes[5..7],1)),
SensorDatum::BatteryVolts(le64f(&bytes[7..9],1000)),
])
}
},
}
}
}
@ -137,7 +162,10 @@ pub fn process_s2n_incoming(msg: &str,
Err(_) => (),
}
}
if bytes.len() == 0 { return Ok(()); }
if bytes.len() == 0 {
trace!("no bytes in message");
return Ok(());
}
trace!("packet into bytes: {:?}", bytes);

Loading…
Cancel
Save