diff options
author | initard <alex.solomes@softwareag.com> | 2021-12-06 17:42:17 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-06 17:42:17 +0000 |
commit | 07f7d72b3c207c7c9e651fee5438b6f89fc97371 (patch) | |
tree | 3f25b4979428466a4aaaaf883958c40b6c9d8d2d /crates/core/c8y_smartrest | |
parent | 64f727c2ecebbc9026dd58622a405ed69f67173f (diff) |
Feature/599/restart device cloud operation (#662)
* cloud restart operation #599
Signed-off-by: initard <solo@softwareag.com>
* operation set to executing c8y #599
Signed-off-by: initard <solo@softwareag.com>
* cargo fmt
Signed-off-by: initard <solo@softwareag.com>
* removing debug hard-coded echo
Signed-off-by: initard <solo@softwareag.com>
* updated handle_restart_opeartion signature in test #599
Signed-off-by: initard <solo@softwareag.com>
* test update for c8y_Restart
Signed-off-by: initard <solo@softwareag.com>
* removed panic on empty restart request #599
Signed-off-by: initard <solo@softwareag.com>
Co-authored-by: initard <solo@softwareag.com>
Diffstat (limited to 'crates/core/c8y_smartrest')
-rw-r--r-- | crates/core/c8y_smartrest/src/error.rs | 3 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_deserializer.rs | 28 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_serializer.rs | 11 |
3 files changed, 40 insertions, 2 deletions
diff --git a/crates/core/c8y_smartrest/src/error.rs b/crates/core/c8y_smartrest/src/error.rs index 03fd5125..4539ac02 100644 --- a/crates/core/c8y_smartrest/src/error.rs +++ b/crates/core/c8y_smartrest/src/error.rs @@ -32,4 +32,7 @@ pub enum SmartRestDeserializerError { parameter: String, hint: String, }, + + #[error("Empty request")] + EmptyRequest, } diff --git a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs index d9868e81..49638301 100644 --- a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs @@ -228,6 +228,27 @@ impl SmartRestLogRequest { } } +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct SmartRestRestartRequest { + pub message_id: String, + pub device: String, +} + +impl SmartRestRestartRequest { + pub fn from_smartrest(smartrest: &str) -> Result<Self, SmartRestDeserializerError> { + let mut rdr = ReaderBuilder::new() + .has_headers(false) + .flexible(true) + .from_reader(smartrest.as_bytes()); + + match rdr.deserialize().next() { + Some(Ok(record)) => Ok(record), + Some(Err(err)) => Err(err)?, + None => Err(SmartRestDeserializerError::EmptyRequest), + } + } +} + type JwtToken = String; #[derive(Debug, Deserialize, PartialEq)] @@ -540,4 +561,11 @@ mod tests { let log = SmartRestLogRequest::from_smartrest(&smartrest); assert!(log.is_ok()); } + + #[test] + fn deserialize_smartrest_restart_request_operation() { + let smartrest = String::from(&format!("510,user")); + let log = SmartRestRestartRequest::from_smartrest(&smartrest); + assert!(log.is_ok()); + } } diff --git a/crates/core/c8y_smartrest/src/smartrest_serializer.rs b/crates/core/c8y_smartrest/src/smartrest_serializer.rs index 6504567d..ce48c1a3 100644 --- a/crates/core/c8y_smartrest/src/smartrest_serializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_serializer.rs @@ -9,6 +9,7 @@ type SmartRest = String; pub enum CumulocitySupportedOperations { C8ySoftwareUpdate, C8yLogFileRequest, + C8yRestartRequest, } impl From<CumulocitySupportedOperations> for &'static str { @@ -16,6 +17,7 @@ impl From<CumulocitySupportedOperations> for &'static str { match op { CumulocitySupportedOperations::C8ySoftwareUpdate => "c8y_SoftwareUpdate", CumulocitySupportedOperations::C8yLogFileRequest => "c8y_LogfileRequest", + CumulocitySupportedOperations::C8yRestartRequest => "c8y_Restart", } } } @@ -59,6 +61,7 @@ impl Default for SmartRestSetSupportedOperations { supported_operations: vec![ CumulocitySupportedOperations::C8ySoftwareUpdate.into(), CumulocitySupportedOperations::C8yLogFileRequest.into(), + CumulocitySupportedOperations::C8yRestartRequest.into(), ], } } @@ -160,7 +163,7 @@ pub struct SmartRestSetOperationToFailed { } impl SmartRestSetOperationToFailed { - fn new(operation: CumulocitySupportedOperations, reason: String) -> Self { + pub fn new(operation: CumulocitySupportedOperations, reason: String) -> Self { Self { message_id: "502", operation: operation.into(), @@ -208,11 +211,15 @@ mod tests { use json_sm::*; #[test] + // NOTE: this test always needs changing when a new operation is added fn serialize_smartrest_supported_operations() { let smartrest = SmartRestSetSupportedOperations::default() .to_smartrest() .unwrap(); - assert_eq!(smartrest, "114,c8y_SoftwareUpdate,c8y_LogfileRequest\n"); + assert_eq!( + smartrest, + "114,c8y_SoftwareUpdate,c8y_LogfileRequest,c8y_Restart\n" + ); } #[test] |