Skip to content

Commit c6cd05a

Browse files
committed
Use a flat list of measurement metadata
Mimicing the structure of grouped measurements as in `"Foo": {"unit: "u", "Temperature": {"unit": "°C"}}` leads to confusion as units are assigned to `"Foo":20` values as well as `"Foo" : { "Temperature": 20 }`. Using a flat structure is less confusing as units for `"Foo"` and `"Foo.Temperature"` are now defined using two independent property sets. Signed-off-by: Didier Wenzek <[email protected]>
1 parent 25f7220 commit c6cd05a

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

crates/extensions/c8y_mapper_ext/src/json.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,20 @@ impl Units {
9393
}
9494

9595
pub fn set_unit(&mut self, measurement: String, meta: serde_json::Value) {
96-
if let Some(unit) = meta.get("unit") {
97-
// "Temperature": {"unit": "°C"},
98-
if let serde_json::Value::String(unit_name) = unit {
99-
self.units.insert(measurement, unit_name.to_owned());
96+
if let Some(serde_json::Value::String(unit)) = meta.get("unit") {
97+
match measurement.split_once('.') {
98+
None => {
99+
// "Temperature": {"unit": "°C"},
100+
self.units.insert(measurement, unit.to_owned());
101+
}
102+
Some((group, measurement)) => {
103+
// "Climate.Temperature": {"unit": "°C"},
104+
self.group_units
105+
.entry(group.to_owned())
106+
.or_default()
107+
.set_unit(measurement.to_owned(), meta);
108+
}
100109
}
101-
} else {
102-
// "Climate": { "Temperature": {"unit": "°C"}, "Humidity": {"unit": "%RH"} }
103-
let group = measurement;
104-
self.set_group_units(group, meta);
105110
}
106111
}
107112

@@ -328,15 +333,11 @@ mod tests {
328333

329334
let units = r#"
330335
{
331-
"Climate":{
332-
"Temperature": {"unit": "°C"},
333-
"Humidity": {"unit": "%RH"}
334-
},
335-
"Acceleration":{
336-
"X-Axis": {"unit": "m/s²"},
337-
"Y-Axis": {"unit": "m/s²"},
338-
"Z-Axis": {"unit": "m/s²"}
339-
}
336+
"Climate.Temperature":{"unit": "°C"},
337+
"Climate.Humidity": {"unit": "%RH"},
338+
"Acceleration.X-Axis": {"unit": "m/s²"},
339+
"Acceleration.Y-Axis": {"unit": "m/s²"},
340+
"Acceleration.Z-Axis": {"unit": "m/s²"}
340341
}"#;
341342

342343
let expected_output = r#"

docs/src/references/mappers/c8y-mapper.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,27 +396,24 @@ graph LR
396396
te --/--- entity_id["&lt;entity id&gt;"] --/--- m --/--- measurement_type["&lt;measurement type&gt;"] --/--- meta
397397
```
398398

399-
The idea is to describe in a single metadata message all the units of the measurements published under the measurement topic.
400-
This message uses the same shape as the measurements and is published as retained on the meta topic of the measurement topic.
399+
The idea is to describe in a single retained message all the units of the measurements published under the measurement topic.
401400

402401
```shell
403402
tedge mqtt pub -r 'te/device/main///m//meta' '{
404-
"Climate":{
405-
"Temperature": {"unit": "°C"},
406-
"Humidity": {"unit": "%RH"}
407-
},
408-
"Acceleration":{
409-
"X-Axis": {"unit": "m/s²"},
410-
"Y-Axis": {"unit": "m/s²"},
411-
"Z-Axis": {"unit": "m/s²"}
412-
}
403+
"Pressure":{"unit": "bar"},
404+
"Climate.Temperature":{"unit": "°C"},
405+
"Climate.Humidity": {"unit": "%RH"},
406+
"Acceleration.X-Axis": {"unit": "m/s²"},
407+
"Acceleration.Y-Axis": {"unit": "m/s²"},
408+
"Acceleration.Z-Axis": {"unit": "m/s²"}
413409
}'
414410
```
415411

416412
The measurements published to the topic for measurements of the same type:
417413

418414
```shell
419415
tedge mqtt pub 'te/device/main///m/' '{
416+
"Pressure": 1.013,
420417
"Climate":{
421418
"Temperature":23.4,
422419
"Humidity":95.0
@@ -434,6 +431,9 @@ are then forwarded to C8Y with their units.
434431
```json
435432
{
436433
"type": "ThinEdgeMeasurement",
434+
"Pressure": {
435+
"Pressure": {"value":1.013,"unit":"bar"}
436+
},
437437
"Climate": {
438438
"Temperature": {"value":23.4,"unit":"°C"},
439439
"Humidity":{"value":95,"unit":"%RH"}
@@ -448,10 +448,12 @@ are then forwarded to C8Y with their units.
448448
```
449449

450450
- A message received on `te/device/main///m/<type>` uses the units defined on `te/device/main///m/<type>/meta`, if any.
451+
- `"Temperature"`, `"Climate": { "Temperature" }` and `"Engine": { "Temperature" }` values can be given different units.
452+
- This is done using a flatten structure,
453+
assigning metadata respectively to `"Temperature"`, `"Climate.Temperature"` and `"Engine.Temperature"`
451454
- If the unit for a measurement is unknown, the measurement value is simply sent with no unit.
452455
- Other metadata such as the precision or the min and max values can be attached to a measurement.
453456
However, these are ignored by the Cumulocity mapper.
454-
- `"Temperature"`, `"Climate": { "Temperature" }` and `"Engine": { "Temperature" }` can be given different units.
455457
- Units and all measurement metadata can be cleared by publishing an empty retained message on `te/device/main///m/<type>/meta`.
456458

457459
### Events

0 commit comments

Comments
 (0)