Skip to content

Commit

Permalink
Simply split JSON over MQTT over \n
Browse files Browse the repository at this point in the history
Instead of using a function made for splitting smartrest, just split on
\n since that function will unescape \" sequences which we want to do in
smartrest but avoid in JSON over MQTT.

Signed-off-by: Marcel Guzik <[email protected]>
  • Loading branch information
Bravo555 committed Dec 16, 2024
1 parent a927e40 commit 9eee7ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,11 @@ impl CumulocityConverter {
) -> Result<Vec<MqttMessage>, ConversionError> {
// JSON over MQTT messages on c8y/devicecontrol/notifications can contain multiple operations in a single MQTT
// message, so split them
let operation_payloads = collect_smartrest_messages(message.payload_str()?);
let operation_payloads = message.payload_str()?.lines();

let mut output = vec![];
for operation_payload in operation_payloads {
let operation = C8yOperation::from_json(operation_payload.as_str())?;
let operation = C8yOperation::from_json(operation_payload)?;
let device_xid = operation.external_source.external_id;
let cmd_id = self.command_id.new_id_with_str(&operation.op_id);

Expand Down
26 changes: 20 additions & 6 deletions crates/extensions/c8y_mapper_ext/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ async fn json_custom_operation_status_multiple_operations_in_one_mqtt_message()
"status":"PENDING",
"id": "111",
"c8y_Command": {
"text": "do something 1"
"text": "do something \"1\""
},
"externalSource":{
"externalId":"test-device",
Expand All @@ -2059,7 +2059,7 @@ async fn json_custom_operation_status_multiple_operations_in_one_mqtt_message()
"status":"PENDING",
"id": "222",
"c8y_Command": {
"text": "do something 2"
"text": "do something \"2\""
},
"externalSource":{
"externalId":"test-device",
Expand All @@ -2071,7 +2071,7 @@ async fn json_custom_operation_status_multiple_operations_in_one_mqtt_message()
"status":"PENDING",
"id": "333",
"c8y_Command": {
"text": "do something 3"
"text": "do something \"3\""
},
"externalSource":{
"externalId":"test-device",
Expand All @@ -2090,9 +2090,23 @@ async fn json_custom_operation_status_multiple_operations_in_one_mqtt_message()
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "504,222")]).await;
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "504,333")]).await;

assert_received_contains_str(&mut mqtt, [("c8y/s/us", "506,111,\"do something 1\n\"")]).await;
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "506,222,\"do something 2\n\"")]).await;
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "506,333,\"do something 3\n\"")]).await;
// escapes: we input JSON over MQTT, but emit Smartrest, thus initial: `do something "1"` becomes `"do something
// ""1""\n"` (outer "" for the Smartrest record field, and then inside double quotes escape a single quote)
assert_received_contains_str(
&mut mqtt,
[("c8y/s/us", "506,111,\"do something \"\"1\"\"\n\"")],
)
.await;
assert_received_contains_str(
&mut mqtt,
[("c8y/s/us", "506,222,\"do something \"\"2\"\"\n\"")],
)
.await;
assert_received_contains_str(
&mut mqtt,
[("c8y/s/us", "506,333,\"do something \"\"3\"\"\n\"")],
)
.await;
}

#[tokio::test]
Expand Down

0 comments on commit 9eee7ce

Please sign in to comment.