Skip to content

Commit 25f7220

Browse files
committed
Clearing measurement units
Signed-off-by: Didier Wenzek <[email protected]>
1 parent ca14c5d commit 25f7220

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

crates/extensions/c8y_mapper_ext/src/converter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,9 +1185,11 @@ impl CumulocityConverter {
11851185
message: &MqttMessage,
11861186
) {
11871187
if let Ok(payload) = message.payload_str() {
1188+
let group = format!("{source}/{measurement_type}");
11881189
if let Ok(meta) = serde_json::from_str(payload) {
1189-
let group = format!("{source}/{measurement_type}");
11901190
self.units.set_group_units(group, meta)
1191+
} else if payload.is_empty() {
1192+
self.units.unset_group_units(group)
11911193
}
11921194
}
11931195
}

crates/extensions/c8y_mapper_ext/src/json.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,17 @@ impl Units {
107107

108108
pub fn set_group_units(&mut self, group: String, meta: serde_json::Value) {
109109
let units = Units::from_metadata(meta);
110-
if !units.is_empty() {
110+
if units.is_empty() {
111+
self.group_units.remove(&group);
112+
} else {
111113
self.group_units.insert(group, units);
112114
}
113115
}
114116

117+
pub fn unset_group_units(&mut self, group: String) {
118+
self.group_units.remove(&group);
119+
}
120+
115121
/// Retrieve the unit to be used for a measurement, if any
116122
pub fn get_unit(&self, measurement: &str) -> Option<&str> {
117123
self.units.get(measurement).map(|x| x.as_str())

tests/RobotFramework/tests/cumulocity/telemetry/thin-edge_device_telemetry.robot

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ Thin-edge devices support sending simple measurements with units
3939

4040
Thin-edge devices support sending complex measurements with units
4141
# Using a test-specific measurement is required to not interfer with other tests
42-
Execute Command tedge mqtt pub -r te/device/main///m/t2/meta '{ "Climate": { "Temperature": { "unit": "°C" }, "Pressure": { "unit": "bar" }}}'
43-
Execute Command tedge mqtt pub te/device/main///m/t2 '{ "Climate": { "Temperature": 19.42, "Pressure": 1.013 }, "Engine": { "Temperature": 350.42, "Pressure": 321.0 }}'
42+
Execute Command
43+
... tedge mqtt pub -r te/device/main///m/t2/meta '{ "Climate.Temperature": { "unit": "°C" }, "Climate.Pressure": { "unit": "bar" }}'
44+
Execute Command
45+
... tedge mqtt pub te/device/main///m/t2 '{ "Climate": { "Temperature": 19.42, "Pressure": 1.013 }, "Engine": { "Temperature": 350.42, "Pressure": 321.0 }}'
4446
${measurements}= Device Should Have Measurements
4547
... minimum=1
4648
... maximum=1
@@ -58,6 +60,73 @@ Thin-edge devices support sending complex measurements with units
5860
Should Be Equal As Numbers ${measurements[0]["Engine"]["Pressure"]["value"]} 321.0
5961
Should Not Contain ${measurements[0]["Engine"]["Pressure"]} unit
6062

63+
Measurement units are specific to each measurement type
64+
# Using a test-specific measurement is required to not interfer with other tests
65+
Execute Command tedge mqtt pub -r te/device/main///m/t3/meta '{ "temperature": { "unit": "°C" } }'
66+
Execute Command tedge mqtt pub -r te/device/main///m/t4/meta '{ "temperature": { "unit": "°F" } }'
67+
Execute Command tedge mqtt pub te/device/main///m/t3 '{ "temperature": 25 }'
68+
Execute Command tedge mqtt pub te/device/main///m/t4 '{ "temperature": 25 }'
69+
${measurements}= Device Should Have Measurements
70+
... minimum=1
71+
... maximum=1
72+
... type=t3
73+
... value=temperature
74+
... series=temperature
75+
Log ${measurements}
76+
Should Be Equal ${measurements[0]["temperature"]["temperature"]["unit"]} °C
77+
${measurements}= Device Should Have Measurements
78+
... minimum=1
79+
... maximum=1
80+
... type=t4
81+
... value=temperature
82+
... series=temperature
83+
Log ${measurements}
84+
Should Be Equal ${measurements[0]["temperature"]["temperature"]["unit"]} °F
85+
86+
Measurement units can be updated
87+
# Using a test-specific measurement is required to not interfer with other tests
88+
Execute Command tedge mqtt pub -r te/device/main///m/t5/meta '{ "temperature": { "unit": "°C" } }'
89+
Execute Command tedge mqtt pub te/device/main///m/t5 '{ "temperature": 25 }'
90+
Execute Command tedge mqtt pub -r te/device/main///m/t5/meta '{ "temperature": { "unit": "°F" } }'
91+
Execute Command tedge mqtt pub te/device/main///m/t5 '{ "temperature": 298.15 }'
92+
${measurements}= Device Should Have Measurements
93+
... minimum=2
94+
... maximum=2
95+
... type=t5
96+
... value=temperature
97+
... series=temperature
98+
Log ${measurements}
99+
Should Be Equal ${measurements[0]["temperature"]["temperature"]["unit"]} °C
100+
Should Be Equal ${measurements[1]["temperature"]["temperature"]["unit"]} °F
101+
102+
Measurement units can be cleared
103+
# Using a test-specific measurement is required to not interfer with other tests
104+
Execute Command tedge mqtt pub -r te/device/main///m/t6/meta '{ "temperature": { "unit": "°C" } }'
105+
Execute Command tedge mqtt pub te/device/main///m/t6 '{ "temperature": 25 }'
106+
${measurements}= Device Should Have Measurements
107+
... minimum=1
108+
... maximum=1
109+
... type=t6
110+
... value=temperature
111+
... series=temperature
112+
... reverse=${True}
113+
... after=1970-01-01
114+
Should Be Equal ${measurements[0]["temperature"]["temperature"]["unit"]} °C
115+
# Execute Command sleep 1
116+
Execute Command tedge mqtt pub -r te/device/main///m/t6/meta ''
117+
Execute Command sleep 1
118+
Execute Command tedge mqtt pub te/device/main///m/t6 '{ "temperature": 298.15 }'
119+
${measurements}= Device Should Have Measurements
120+
... minimum=2
121+
... maximum=2
122+
... type=t6
123+
... value=temperature
124+
... series=temperature
125+
... after=1970-01-01
126+
... reverse=${True}
127+
Log ${measurements}
128+
Should Not Contain ${measurements[0]["temperature"]["temperature"]} unit
129+
61130
Thin-edge devices support sending simple measurements with custom type
62131
Execute Command tedge mqtt pub te/device/main///m/ '{ "type":"CustomType", "temperature": 25 }'
63132
${measurements}= Device Should Have Measurements

0 commit comments

Comments
 (0)