From 094ec0b2da58726cc6af7ed775e517170b41e25e Mon Sep 17 00:00:00 2001 From: Rina Fujino Date: Fri, 2 Aug 2024 12:01:05 +0200 Subject: [PATCH] refactor: CommandStatus::Executing takes extra MQTT messages Signed-off-by: Rina Fujino --- .../src/operations/handlers/config_snapshot.rs | 4 +++- .../src/operations/handlers/config_update.rs | 4 +++- .../src/operations/handlers/firmware_update.rs | 4 +++- .../src/operations/handlers/log_upload.rs | 4 +++- .../src/operations/handlers/mod.rs | 16 ++++++++++------ .../src/operations/handlers/restart.rs | 4 +++- .../src/operations/handlers/software_update.rs | 4 +++- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_snapshot.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_snapshot.rs index d396cc85f99..377e2f1d745 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_snapshot.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_snapshot.rs @@ -49,7 +49,9 @@ impl OperationContext { let cmd_id = command.cmd_id.as_str(); match command.status() { - CommandStatus::Executing => Ok(OperationOutcome::Executing), + CommandStatus::Executing => Ok(OperationOutcome::Executing { + extra_messages: vec![], + }), CommandStatus::Successful => { // Send a request to the Downloader to download the file asynchronously from FTS let config_filename = format!( diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_update.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_update.rs index 14fd149e06d..6e97dbb60bb 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_update.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/config_update.rs @@ -44,7 +44,9 @@ impl OperationContext { let sm_topic = &target.smartrest_publish_topic; match command.status() { - CommandStatus::Executing => Ok(OperationOutcome::Executing), + CommandStatus::Executing => Ok(OperationOutcome::Executing { + extra_messages: vec![], + }), CommandStatus::Successful => { let smartrest_operation_status = succeed_operation_no_payload( CumulocitySupportedOperations::C8yDownloadConfigFile, diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/firmware_update.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/firmware_update.rs index cb1b938cf60..f6fe9ecf9f1 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/firmware_update.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/firmware_update.rs @@ -49,7 +49,9 @@ impl OperationContext { let sm_topic = &target.smartrest_publish_topic; match command.status() { - CommandStatus::Executing => Ok(OperationOutcome::Executing), + CommandStatus::Executing => Ok(OperationOutcome::Executing { + extra_messages: vec![], + }), CommandStatus::Successful => { let smartrest_operation_status = succeed_operation_no_payload(CumulocitySupportedOperations::C8yFirmware); diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/log_upload.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/log_upload.rs index 9dadf147b76..d38d9f01d44 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/log_upload.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/log_upload.rs @@ -45,7 +45,9 @@ impl OperationContext { let smartrest_topic = &target.smartrest_publish_topic; match command.status() { - CommandStatus::Executing => Ok(OperationOutcome::Executing), + CommandStatus::Executing => Ok(OperationOutcome::Executing { + extra_messages: vec![], + }), CommandStatus::Successful => { // Send a request to the Downloader to download the file asynchronously from FTS let log_filename = format!("{}-{}", command.payload.log_type, cmd_id); diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/mod.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/mod.rs index 3b13475b422..f2eba4e5614 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/mod.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/mod.rs @@ -148,14 +148,17 @@ impl OperationContext { &entity.smartrest_publish_topic, ) { OperationOutcome::Ignored => UpdateStatus::Ongoing, - OperationOutcome::Executing => { + OperationOutcome::Executing { mut extra_messages } => { let c8y_state_executing_payload = set_operation_executing(c8y_operation); let c8y_state_executing_message = MqttMessage::new(&entity.smartrest_publish_topic, c8y_state_executing_payload); - mqtt_publisher - .send(c8y_state_executing_message) - .await - .unwrap(); + + let mut messages = vec![c8y_state_executing_message]; + messages.append(&mut extra_messages); + + for message in messages { + mqtt_publisher.send(message).await.unwrap(); + } UpdateStatus::Ongoing } @@ -209,7 +212,8 @@ pub(super) enum OperationOutcome { Ignored, /// Update C8y operation state to `EXECUTING`. - Executing, + /// `extra_messages` can be used if an operation requires more than the status update message. + Executing { extra_messages: Vec }, /// Operation is terminated. /// diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/restart.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/restart.rs index b0999dcbe03..6de1ddb38f6 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/restart.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/restart.rs @@ -33,7 +33,9 @@ impl OperationContext { let topic = &target.smartrest_publish_topic; match command.status() { - CommandStatus::Executing => Ok(OperationOutcome::Executing), + CommandStatus::Executing => Ok(OperationOutcome::Executing { + extra_messages: vec![], + }), CommandStatus::Successful => { let smartrest_set_operation = smartrest::smartrest_serializer::succeed_operation_no_payload( diff --git a/crates/extensions/c8y_mapper_ext/src/operations/handlers/software_update.rs b/crates/extensions/c8y_mapper_ext/src/operations/handlers/software_update.rs index 11610f886c4..27c22574a10 100644 --- a/crates/extensions/c8y_mapper_ext/src/operations/handlers/software_update.rs +++ b/crates/extensions/c8y_mapper_ext/src/operations/handlers/software_update.rs @@ -39,7 +39,9 @@ impl OperationContext { // The command has not been processed yet Ok(OperationOutcome::Ignored) } - CommandStatus::Executing => Ok(OperationOutcome::Executing), + CommandStatus::Executing => Ok(OperationOutcome::Executing { + extra_messages: vec![], + }), CommandStatus::Successful => { let smartrest_set_operation = smartrest::smartrest_serializer::succeed_operation_no_payload(