Skip to content

Commit

Permalink
feat: convert device profile request to tedge json format
Browse files Browse the repository at this point in the history
Signed-off-by: Krzysztof Piotrowski <[email protected]>
  • Loading branch information
Ruadhri17 committed Jul 25, 2024
1 parent b9a3261 commit 7c3536a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
35 changes: 34 additions & 1 deletion crates/core/tedge_api/src/device_profile_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use serde::Deserialize;
use serde::Serialize;

/// Command for device profile
pub type DeviceProfileCmd = Command<DeviceProfileCmdPayload>;
pub type DeviceProfileCommand = Command<DeviceProfileCmdPayload>;

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DeviceProfileCmdPayload {
#[serde(flatten)]
pub status: CommandStatus,
pub name: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub operations: Vec<DeviceProfileOperation>,
}

Expand Down Expand Up @@ -53,3 +54,35 @@ pub enum OperationPayload {
Software(SoftwareInfo),
Config(ConfigInfo),
}

impl DeviceProfileCmdPayload {
pub fn add_firmware(&mut self, firmware: FirmwareInfo) {
let firmware_operation = DeviceProfileOperation {
operation: OperationType::FirmwareUpdate,
skip: false,
payload: OperationPayload::Firmware(firmware),
};

self.operations.push(firmware_operation);
}

pub fn add_software(&mut self, software: SoftwareInfo) {
let software_operation = DeviceProfileOperation {
operation: OperationType::SoftwareUpdate,
skip: false,
payload: OperationPayload::Software(software),
};

self.operations.push(software_operation);
}

pub fn add_config(&mut self, config: ConfigInfo) {
let config_operation = DeviceProfileOperation {
operation: OperationType::ConfigUpdate,
skip: false,
payload: OperationPayload::Config(config),
};

self.operations.push(config_operation);
}
}
13 changes: 12 additions & 1 deletion crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,19 @@ impl CumulocityConverter {
}
C8yDeviceControlOperation::DeviceProfile(request) => {
if self.config.capabilities.device_profile {
vec![]
if let Some(profile_name) = extras.get("profileName") {
self.convert_device_profile_request(
device_xid,
cmd_id,
request,
profile_name.to_string(),
)?
} else {
error!("Received a c8y_DeviceProfile without a profile name");
vec![]
}
} else {
warn!("Received a c8y_DeviceProfile operation, however, device_profile feature is disabled");
vec![]
}
}
Expand Down
48 changes: 48 additions & 0 deletions crates/extensions/c8y_mapper_ext/src/operations/device_profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::converter::CumulocityConverter;
use crate::error::CumulocityMapperError;
use c8y_api::json_c8y_deserializer::C8yDeviceProfile;
use tedge_api::device_profile_command::DeviceProfileCmdPayload;
use tedge_api::entity_store::EntityExternalId;
use tedge_api::mqtt_topics::Channel;
use tedge_api::mqtt_topics::OperationType;
use tedge_api::CommandStatus;
use tedge_api::Jsonify;
use tedge_mqtt_ext::MqttMessage;

impl CumulocityConverter {
/// 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(),
};

request.add_firmware(device_profile_request.get_firmware_info());
request.add_software(device_profile_request.get_software_info());
for config in device_profile_request.get_config_info() {
request.add_config(config);
}

// Command messages must be retained
Ok(vec![
MqttMessage::new(&topic, request.to_json()).with_retain()
])
}
}
1 change: 1 addition & 0 deletions crates/extensions/c8y_mapper_ext/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use tracing::error;

pub mod config_snapshot;
pub mod config_update;
pub mod device_profile;
mod error;
pub mod firmware_update;
pub mod log_upload;
Expand Down

0 comments on commit 7c3536a

Please sign in to comment.