Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b195d28

Browse files
Add distance type to Cayenne LPP codec. (#704)
1 parent a9031ed commit b195d28

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

chirpstack/src/codec/cayenne_lpp.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const LPP_TEMPERATURE_SENSOR: u8 = 103;
1313
const LPP_HUMIDITY_SENSOR: u8 = 104;
1414
const LPP_ACCELEROMETER: u8 = 113;
1515
const LPP_BAROMETER: u8 = 115;
16+
const LPP_DISTANCE: u8 = 130;
1617
const LPP_GYROMETER: u8 = 134;
1718
const LPP_GPS_LOCATION: u8 = 136;
1819

@@ -56,6 +57,7 @@ struct CayenneLpp {
5657
humidity_sensor: BTreeMap<u8, f64>,
5758
accelerometer: BTreeMap<u8, Accelerometer>,
5859
barometer: BTreeMap<u8, f64>,
60+
distance: BTreeMap<u8, f64>,
5961
gyrometer: BTreeMap<u8, Gyrometer>,
6062
gps_location: BTreeMap<u8, GpsLocation>,
6163
}
@@ -82,6 +84,7 @@ impl CayenneLpp {
8284
LPP_HUMIDITY_SENSOR => lpp.set_humidity_sensor(buf[0], &mut cur)?,
8385
LPP_ACCELEROMETER => lpp.set_accelerometer(buf[0], &mut cur)?,
8486
LPP_BAROMETER => lpp.set_barometer(buf[0], &mut cur)?,
87+
LPP_DISTANCE => lpp.set_distance(buf[0], &mut cur)?,
8588
LPP_GYROMETER => lpp.set_gyrometer(buf[0], &mut cur)?,
8689
LPP_GPS_LOCATION => lpp.set_gps_location(buf[0], &mut cur)?,
8790
_ => {
@@ -124,6 +127,7 @@ impl CayenneLpp {
124127
.set_accelerometer_from_value(v)
125128
.context("accelerometer")?,
126129
"barometer" => lpp.set_barometer_from_value(v).context("barometer")?,
130+
"distance" => lpp.set_distance_from_value(v).context("distance")?,
127131
"gyrometer" => lpp.set_gyrometer_from_value(v).context("gyrometer")?,
128132
"gpsLocation" => lpp.set_gps_location_from_value(v).context("gpsLocation")?,
129133
_ => {
@@ -214,6 +218,14 @@ impl CayenneLpp {
214218
out.extend(val.to_be_bytes());
215219
}
216220

221+
// distance
222+
for (k, v) in &self.distance {
223+
out.extend([*k, LPP_DISTANCE]);
224+
225+
let val = (*v * 1000.0) as u32;
226+
out.extend(val.to_be_bytes());
227+
}
228+
217229
// gyrometer
218230
for (k, v) in &self.gyrometer {
219231
out.extend([*k, LPP_GYROMETER]);
@@ -445,6 +457,24 @@ impl CayenneLpp {
445457
);
446458
}
447459

460+
if !self.distance.is_empty() {
461+
let mut val: pbjson_types::Struct = Default::default();
462+
for (k, v) in &self.distance {
463+
val.fields.insert(
464+
format!("{}", k),
465+
pbjson_types::Value {
466+
kind: Some(pbjson_types::value::Kind::NumberValue(*v)),
467+
},
468+
);
469+
}
470+
out.fields.insert(
471+
"distance".to_string(),
472+
pbjson_types::Value {
473+
kind: Some(pbjson_types::value::Kind::StructValue(val)),
474+
},
475+
);
476+
}
477+
448478
if !self.gyrometer.is_empty() {
449479
let mut val: pbjson_types::Struct = Default::default();
450480
for (k, v) in &self.gyrometer {
@@ -769,6 +799,27 @@ impl CayenneLpp {
769799
Ok(())
770800
}
771801

802+
fn set_distance(&mut self, channel: u8, cur: &mut Cursor<&[u8]>) -> Result<()> {
803+
let mut buf: [u8; 4] = [0; 4];
804+
cur.read_exact(&mut buf)?;
805+
let val = u32::from_be_bytes(buf);
806+
self.distance.insert(channel, (val as f64) / 1000.0);
807+
Ok(())
808+
}
809+
810+
fn set_distance_from_value(&mut self, v: &prost_types::Value) -> Result<()> {
811+
if let Some(prost_types::value::Kind::StructValue(s)) = &v.kind {
812+
for (k, v) in &s.fields {
813+
let c: u8 = k.parse()?;
814+
if let Some(prost_types::value::Kind::NumberValue(v)) = &v.kind {
815+
self.distance.insert(c, *v);
816+
}
817+
}
818+
}
819+
820+
Ok(())
821+
}
822+
772823
fn set_gyrometer(&mut self, channel: u8, cur: &mut Cursor<&[u8]>) -> Result<()> {
773824
let mut buf_x: [u8; 2] = [0; 2];
774825
let mut buf_y: [u8; 2] = [0; 2];
@@ -913,6 +964,7 @@ pub mod test {
913964
3, 104, 41, 5, 104, 150, // humidity sensors
914965
3, 113, 0, 1, 0, 2, 0, 3, 5, 113, 3, 234, 7, 211, 11, 187, // accelerometers
915966
3, 115, 4, 31, 5, 115, 9, 196, // barometers
967+
3, 130, 0, 0, 1, 16, 5, 130, 1, 2, 3, 4, // distance
916968
3, 134, 0, 1, 0, 2, 0, 3, 5, 134, 3, 233, 7, 210, 11, 187, // gyrometers
917969
1, 136, 6, 118, 95, 242, 150, 10, 0, 3, 232, // gps location
918970
];
@@ -1222,6 +1274,30 @@ pub mod test {
12221274
})),
12231275
},
12241276
),
1277+
(
1278+
"distance".to_string(),
1279+
prost_types::Value {
1280+
kind: Some(prost_types::value::Kind::StructValue(prost_types::Struct {
1281+
fields: [
1282+
(
1283+
"3".to_string(),
1284+
prost_types::Value {
1285+
kind: Some(prost_types::value::Kind::NumberValue(0.272)),
1286+
},
1287+
),
1288+
(
1289+
"5".to_string(),
1290+
prost_types::Value {
1291+
kind: Some(prost_types::value::Kind::NumberValue(16909.060)),
1292+
},
1293+
),
1294+
]
1295+
.iter()
1296+
.cloned()
1297+
.collect(),
1298+
})),
1299+
},
1300+
),
12251301
(
12261302
"gyrometer".to_string(),
12271303
prost_types::Value {
@@ -1704,6 +1780,34 @@ pub mod test {
17041780
)),
17051781
},
17061782
),
1783+
(
1784+
"distance".to_string(),
1785+
pbjson_types::Value {
1786+
kind: Some(pbjson_types::value::Kind::StructValue(
1787+
pbjson_types::Struct {
1788+
fields: [
1789+
(
1790+
"3".to_string(),
1791+
pbjson_types::Value {
1792+
kind: Some(pbjson_types::value::Kind::NumberValue(0.272)),
1793+
},
1794+
),
1795+
(
1796+
"5".to_string(),
1797+
pbjson_types::Value {
1798+
kind: Some(pbjson_types::value::Kind::NumberValue(
1799+
16909.060,
1800+
)),
1801+
},
1802+
),
1803+
]
1804+
.iter()
1805+
.cloned()
1806+
.collect(),
1807+
},
1808+
)),
1809+
},
1810+
),
17071811
(
17081812
"gyrometer".to_string(),
17091813
pbjson_types::Value {

0 commit comments

Comments
 (0)