Skip to content

Commit

Permalink
fixup! feat: convert device profile request to tedge json format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruadhri17 committed Jul 31, 2024
1 parent 41a8f23 commit b2ca6c5
Show file tree
Hide file tree
Showing 2 changed files with 543 additions and 182 deletions.
44 changes: 44 additions & 0 deletions crates/extensions/c8y_mapper_ext/src/operations/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::sync::Arc;

use c8y_api::json_c8y_deserializer::C8yDeviceProfile;
use c8y_api::json_c8y_deserializer::C8yDownloadConfigFile;
use c8y_api::json_c8y_deserializer::C8yFirmware;
use c8y_api::json_c8y_deserializer::C8yLogfileRequest;
Expand All @@ -13,6 +14,7 @@ use tedge_api::commands::ConfigUpdateCmdPayload;
use tedge_api::commands::FirmwareUpdateCmdPayload;
use tedge_api::commands::LogMetadata;
use tedge_api::commands::LogUploadCmdPayload;
use tedge_api::device_profile::DeviceProfileCmdPayload;
use tedge_api::entity_store::EntityExternalId;
use tedge_api::entity_store::EntityMetadata;
use tedge_api::mqtt_topics::Channel;
Expand Down Expand Up @@ -309,4 +311,46 @@ impl CumulocityConverter {

Ok(messages)
}

/// Convert c8y_DeviceProfile JSON over MQTT operation to ThinEdge device_profile command.
pub fn convert_device_profile_request(
&self,
device_xid: String,
cmd_id: String,
device_profile_request: C8yDeviceProfile,
profile_name: String,
) -> Result<Vec<MqttMessage>, CumulocityMapperError> {
let entity_xid: EntityExternalId = device_xid.into();

let target = self.entity_store.try_get_by_external_id(&entity_xid)?;

let channel = Channel::Command {
operation: OperationType::DeviceProfile,
cmd_id,
};
let topic = self.mqtt_schema.topic_for(&target.topic_id, &channel);

let mut request = DeviceProfileCmdPayload {
status: CommandStatus::Init,
name: profile_name,
operations: Vec::new(),
};

if let Some(firmware) = device_profile_request.firmware {
request.add_firmware(firmware.into());
}

if let Some(software) = device_profile_request.software {
request.add_software(software.try_into()?);
}

for config in device_profile_request.configuration {
request.add_config(config.into());
}

// Command messages must be retained
Ok(vec![
MqttMessage::new(&topic, request.to_json()).with_retain()
])
}
}
Loading

0 comments on commit b2ca6c5

Please sign in to comment.