diff options
author | PradeepKiruvale <pradeepkumar.kj@softwareag.com> | 2022-07-22 14:31:08 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-22 14:31:08 +0530 |
commit | 7e62504db494d9a2523c3001b2ad6d5fa84ce548 (patch) | |
tree | aa6f73d32f66339cc30ba2de07235c9921abb078 /crates/core/thin_edge_json | |
parent | cf00c1d358f2c9ba67ca5af46fd82fb9f2cf37a6 (diff) |
Extend events API to support child devices (#1243)
* Events for child devices
Extend events APIs to send the event messages from external/child device to device twin in the cloud.
Signed-off-by: Pradeep Kumar K J <pradeepkumar.kj@softwareag.com>
Diffstat (limited to 'crates/core/thin_edge_json')
-rw-r--r-- | crates/core/thin_edge_json/src/event.rs | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/crates/core/thin_edge_json/src/event.rs b/crates/core/thin_edge_json/src/event.rs index 42b1ed35..8e07625f 100644 --- a/crates/core/thin_edge_json/src/event.rs +++ b/crates/core/thin_edge_json/src/event.rs @@ -13,6 +13,7 @@ pub struct ThinEdgeEvent { pub name: String, #[serde(flatten)] pub data: Option<ThinEdgeEventData>, + pub source: Option<String>, } /// In-memory representation of ThinEdge JSON event payload @@ -29,6 +30,7 @@ pub struct ThinEdgeEventData { } pub mod error { + #[derive(thiserror::Error, Debug)] pub enum ThinEdgeJsonDeserializerError { #[error("Unsupported topic: {0}")] @@ -48,7 +50,7 @@ impl ThinEdgeEvent { mqtt_payload: &str, ) -> Result<Self, ThinEdgeJsonDeserializerError> { let topic_split: Vec<&str> = mqtt_topic.split('/').collect(); - if topic_split.len() == 3 { + if topic_split.len() == 3 || topic_split.len() == 4 { let event_name = topic_split[2]; if event_name.is_empty() { return Err(ThinEdgeJsonDeserializerError::EmptyEventName); @@ -60,9 +62,17 @@ impl ThinEdgeEvent { Some(serde_json::from_str(mqtt_payload)?) }; + // The 4th part of the topic name is the event source - if any + let external_source = if topic_split.len() == 4 { + Some(topic_split[3].to_string()) + } else { + None + }; + Ok(Self { name: event_name.into(), data: event_data, + source: external_source, }) } else { Err(ThinEdgeJsonDeserializerError::UnsupportedTopic( @@ -94,6 +104,7 @@ mod tests { time: Some(datetime!(2021-04-23 19:00:00 +05:00)), extras: HashMap::new(), }), + source: None, }; "event parsing" )] @@ -109,6 +120,7 @@ mod tests { time: None, extras: HashMap::new(), }), + source: None, }; "event parsing without timestamp" )] @@ -124,6 +136,7 @@ mod tests { time: Some(datetime!(2021-04-23 19:00:00 +05:00)), extras: HashMap::new(), }), + source: None, }; "event parsing without text" )] @@ -137,9 +150,41 @@ mod tests { time: None, extras: HashMap::new(), }), + source: None, }; "event parsing without text or timestamp" )] + #[test_case( + "tedge/events/click_event/external_source", + json!({ + "text": "Someone clicked", + "time": "2021-04-23T19:00:00+05:00", + }), + ThinEdgeEvent { + name: "click_event".into(), + data: Some(ThinEdgeEventData { + text: Some("Someone clicked".into()), + time: Some(datetime!(2021-04-23 19:00:00 +05:00)), + extras: HashMap::new(), + }), + source: Some("external_source".into()), + }; + "event parsing with external source" + )] + #[test_case( + "tedge/events/click_event/external_source", + json!({}), + ThinEdgeEvent { + name: "click_event".into(), + data: Some(ThinEdgeEventData { + text: None, + time: None, + extras: HashMap::new(), + }), + source: Some("external_source".into()), + }; + "event parsing empty payload with external source" + )] fn parse_thin_edge_event_json( event_topic: &str, event_payload: Value, @@ -159,8 +204,8 @@ mod tests { } #[test] - fn event_translation_more_than_three_topic_levels() { - let result = ThinEdgeEvent::try_from("tedge/events/page/click", "{}"); + fn event_translation_more_than_four_topic_levels() { + let result = ThinEdgeEvent::try_from("tedge/events/page/click/click", "{}"); assert_matches!( result, |