Skip to content

Commit

Permalink
Fix auto-registration of child device op dirs thin-edge#2389
Browse files Browse the repository at this point in the history
If the child device directory the child device operation directories
follow the default external id naming scheme, they are auto-registered
with the right topic id and name derived from that external id.
  • Loading branch information
albinsuresh committed Oct 31, 2023
1 parent 928f43e commit 5d7c9d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
7 changes: 4 additions & 3 deletions crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,14 +752,15 @@ impl CumulocityConverter {
continue;
}
};
let child_topic_id =
EntityTopicId::default_child_device(child_external_id.as_ref()).unwrap();

let child_name = self.default_device_name_from_external_id(&child_external_id);
let child_topic_id = EntityTopicId::default_child_device(&child_name).unwrap();
let child_device_reg_msg = EntityRegistrationMessage {
topic_id: child_topic_id,
external_id: Some(child_external_id.clone()),
r#type: EntityType::ChildDevice,
parent: None,
other: json!({ "name": child_external_id.as_ref() })
other: json!({ "name": child_name })
.as_object()
.unwrap()
.to_owned(),
Expand Down
37 changes: 37 additions & 0 deletions crates/extensions/c8y_mapper_ext/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,43 @@ async fn mapper_publishes_child_device_create_message() {
.await;
}

#[tokio::test]
async fn mapper_publishes_child_device_create_message_default_naming_scheme() {
let cfg_dir = TempTedgeDir::new();
create_thin_edge_child_devices(&cfg_dir, vec!["test-device:device:child1"]);

let (mqtt, _http, _fs, _timer, _dl) = spawn_c8y_mapper_actor(&cfg_dir, false).await;
let mut mqtt = mqtt.with_timeout(TEST_TIMEOUT_MS);
skip_init_messages(&mut mqtt).await;

mqtt.send(MqttMessage::new(
&C8yTopic::downstream_topic(),
"106,child-one",
))
.await
.expect("Send failed");

// Expect auto-registration message
assert_received_includes_json(
&mut mqtt,
[(
"te/device/child1//",
json!({"@type":"child-device", "name": "child1"}),
)],
)
.await;

// Expect smartrest message on `c8y/s/us` with expected payload "101,child1,child1,thin-edge.io-child".
assert_received_contains_str(
&mut mqtt,
[(
"c8y/s/us",
"101,test-device:device:child1,child1,thin-edge.io-child",
)],
)
.await;
}

#[tokio::test]
async fn mapper_publishes_supported_operations_for_child_device() {
// The test assures tedge-mapper checks if there is a directory for operations for child devices, then it reads and
Expand Down

0 comments on commit 5d7c9d9

Please sign in to comment.