summaryrefslogtreecommitdiffstats
path: root/crates/core
diff options
context:
space:
mode:
authorAlbin Suresh <albin.suresh@softwareag.com>2022-03-16 16:59:16 +0530
committerAlbin Suresh <albin.suresh@softwareag.com>2022-03-16 16:59:16 +0530
commit60f0b27047e16b3743eda16a0fa477a29b54575c (patch)
treebc938f77ea86dd10c2f80c83dc01ee693f5a6886 /crates/core
parentd7aefed3626a62d2d54b9e5fd291e5f43a49e9d1 (diff)
Closes #1004 Fix mapping of time field in event JSON to SmartREST
Diffstat (limited to 'crates/core')
-rw-r--r--crates/core/tedge_mapper/src/c8y/converter.rs14
-rw-r--r--crates/core/tedge_mapper/src/c8y/tests.rs9
2 files changed, 14 insertions, 9 deletions
diff --git a/crates/core/tedge_mapper/src/c8y/converter.rs b/crates/core/tedge_mapper/src/c8y/converter.rs
index c5ab7b96..bc61b78b 100644
--- a/crates/core/tedge_mapper/src/c8y/converter.rs
+++ b/crates/core/tedge_mapper/src/c8y/converter.rs
@@ -31,6 +31,7 @@ use std::{
process::Stdio,
};
use thin_edge_json::{alarm::ThinEdgeAlarm, event::ThinEdgeEvent};
+use time::format_description::well_known::Rfc3339;
use tracing::{debug, info, log::error};
use super::{
@@ -158,7 +159,7 @@ where
// If the message doesn't contain any fields other than `text` and `time`, convert to SmartREST
let message = if c8y_event.extras.is_empty() {
- let smartrest_event = Self::serialize_to_smartrest(&c8y_event);
+ let smartrest_event = Self::serialize_to_smartrest(&c8y_event)?;
let smartrest_topic = Topic::new_unchecked(SMARTREST_PUBLISH_TOPIC);
Message::new(&smartrest_topic, smartrest_event)
@@ -180,11 +181,14 @@ where
}
}
- fn serialize_to_smartrest(c8y_event: &C8yCreateEvent) -> String {
- format!(
+ fn serialize_to_smartrest(c8y_event: &C8yCreateEvent) -> Result<String, ConversionError> {
+ Ok(format!(
"{},{},\"{}\",{}",
- CREATE_EVENT_SMARTREST_CODE, c8y_event.event_type, c8y_event.text, c8y_event.time
- )
+ CREATE_EVENT_SMARTREST_CODE,
+ c8y_event.event_type,
+ c8y_event.text,
+ c8y_event.time.format(&Rfc3339)?
+ ))
}
}
diff --git a/crates/core/tedge_mapper/src/c8y/tests.rs b/crates/core/tedge_mapper/src/c8y/tests.rs
index 96ace4a8..bafe2b84 100644
--- a/crates/core/tedge_mapper/src/c8y/tests.rs
+++ b/crates/core/tedge_mapper/src/c8y/tests.rs
@@ -698,7 +698,7 @@ async fn convert_event_with_known_fields_to_c8y_smartrest() -> Result<()> {
);
let event_topic = "tedge/events/click_event";
- let event_payload = r#"{ "text": "Someone clicked" }"#;
+ let event_payload = r#"{ "text": "Someone clicked", "time": "2020-02-02T01:02:03+05:30" }"#;
let event_message = Message::new(&Topic::new_unchecked(event_topic), event_payload);
let converted_events = converter.convert(&event_message).await;
@@ -706,9 +706,10 @@ async fn convert_event_with_known_fields_to_c8y_smartrest() -> Result<()> {
let converted_event = converted_events.get(0).unwrap();
assert_eq!(converted_event.topic.name, "c8y/s/us");
dbg!(converted_event.payload_str()?);
- assert!(converted_event
- .payload_str()?
- .starts_with(r#"400,click_event,"Someone clicked","#));
+ assert_eq!(
+ converted_event.payload_str()?,
+ r#"400,click_event,"Someone clicked",2020-02-02T01:02:03+05:30"#
+ );
Ok(())
}