Skip to content

Commit

Permalink
feat: add deserializer for c8y_DeviceProfile json object
Browse files Browse the repository at this point in the history
Signed-off-by: Krzysztof Piotrowski <[email protected]>
  • Loading branch information
Ruadhri17 committed Jul 26, 2024
1 parent 8e6b2de commit 8d4542b
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions crates/core/c8y_api/src/json_c8y_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use download::DownloadInfo;
use mqtt_channel::Topic;
use serde::Deserialize;
use std::collections::HashMap;
use tedge_api::commands::ConfigInfo;
use tedge_api::commands::FirmwareInfo;
use tedge_api::commands::SoftwareInfo;
use tedge_api::commands::SoftwareModuleAction;
use tedge_api::commands::SoftwareModuleItem;
use tedge_api::commands::SoftwareRequestResponseSoftwareList;
use tedge_api::mqtt_topics::EntityTopicId;
use tedge_api::SoftwareModule;
use tedge_api::SoftwareModuleUpdate;
Expand Down Expand Up @@ -34,6 +40,7 @@ pub enum C8yDeviceControlOperation {
UploadConfigFile(C8yUploadConfigFile),
DownloadConfigFile(C8yDownloadConfigFile),
Firmware(C8yFirmware),
DeviceProfile(C8yDeviceProfile),
Custom,
}

Expand Down Expand Up @@ -61,6 +68,10 @@ impl C8yDeviceControlOperation {
)?)
} else if let Some(value) = hashmap.get("c8y_Firmware") {
C8yDeviceControlOperation::Firmware(C8yFirmware::from_json_value(value.clone())?)
} else if let Some(value) = hashmap.get("c8y_DeviceProfile") {
C8yDeviceControlOperation::DeviceProfile(C8yDeviceProfile::from_json_value(
value.clone(),
)?)
} else {
C8yDeviceControlOperation::Custom
};
Expand Down Expand Up @@ -413,6 +424,77 @@ pub struct C8yFirmware {
pub url: String,
}

#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct C8yDeviceProfile {
pub firmware: C8yFirmware,
pub software: C8ySoftwareUpdate,
pub configuration: Vec<C8yDownloadConfigFile>,
}

impl C8yDeviceProfile {
pub fn get_firmware_info(&self) -> FirmwareInfo {
FirmwareInfo {
name: Some(self.firmware.name.clone()),
version: Some(self.firmware.version.clone()),
remote_url: Some(self.firmware.url.clone()),
}
}

pub fn try_get_software_info(&self) -> Result<SoftwareInfo, C8yJsonOverMqttDeserializerError> {
let mut software_info = SoftwareInfo {
update_list: Vec::new(),
};

for module in self.software.modules() {
let plugin_type = module
.get_module_version_and_type()
.1
.unwrap_or_else(SoftwareModule::default_type);

let item = SoftwareModuleItem {
name: module.name.clone(),
version: module.get_module_version_and_type().0,
url: module.get_url(),
action: match module.action.clone().try_into()? {
C8ySoftwareUpdateAction::Install => Some(SoftwareModuleAction::Install),
C8ySoftwareUpdateAction::Delete => Some(SoftwareModuleAction::Remove),
},
reason: None,
};

if let Some(list) = software_info
.update_list
.iter_mut()
.find(|list| list.plugin_type == plugin_type)
{
list.modules.push(item);
} else {
software_info
.update_list
.push(SoftwareRequestResponseSoftwareList {
plugin_type,
modules: vec![item],
});
}
}

Ok(software_info)
}

pub fn get_config_info(&self) -> Vec<ConfigInfo> {
let mut config_info = Vec::new();
for config in &self.configuration {
config_info.push(ConfigInfo {
config_type: config.config_type.clone(),
remote_url: Some(config.url.clone()),
})
}

config_info
}
}

pub trait C8yDeviceControlOperationHelper {
fn from_json_value(value: serde_json::Value) -> Result<Self, serde_json::Error>
where
Expand All @@ -434,6 +516,8 @@ impl C8yDeviceControlOperationHelper for C8yDownloadConfigFile {}

impl C8yDeviceControlOperationHelper for C8yFirmware {}

impl C8yDeviceControlOperationHelper for C8yDeviceProfile {}

#[derive(thiserror::Error, Debug)]
pub enum C8yJsonOverMqttDeserializerError {
#[error("Parameter {parameter} is not recognized. {hint}")]
Expand Down

0 comments on commit 8d4542b

Please sign in to comment.