summaryrefslogtreecommitdiffstats
path: root/crates/core
diff options
context:
space:
mode:
authorinitard <solo@softwareag.com>2022-06-24 12:14:58 +0100
committerinitard <alex.solomes@softwareag.com>2022-07-20 09:17:48 +0100
commit589081115f943a1275464682a027b97d20e2d5f0 (patch)
tree0fdb36a64fb4079f5921a28432e1702f411519f4 /crates/core
parentae947b3e64d0cb5684c171b9e15c522d16749d48 (diff)
publishing supported log types if c8y bridge is up
Signed-off-by: initard <solo@softwareag.com>
Diffstat (limited to 'crates/core')
-rw-r--r--crates/core/c8y_api/src/lib.rs1
-rw-r--r--crates/core/c8y_api/src/utils.rs36
2 files changed, 37 insertions, 0 deletions
diff --git a/crates/core/c8y_api/src/lib.rs b/crates/core/c8y_api/src/lib.rs
index 7eb6352c..64cd7a32 100644
--- a/crates/core/c8y_api/src/lib.rs
+++ b/crates/core/c8y_api/src/lib.rs
@@ -1,2 +1,3 @@
pub mod http_proxy;
pub mod json_c8y;
+pub mod utils;
diff --git a/crates/core/c8y_api/src/utils.rs b/crates/core/c8y_api/src/utils.rs
new file mode 100644
index 00000000..89ce4b18
--- /dev/null
+++ b/crates/core/c8y_api/src/utils.rs
@@ -0,0 +1,36 @@
+pub mod bridge {
+
+ use mqtt_channel::Message;
+
+ pub const C8Y_BRIDGE_HEALTH_TOPIC: &str = "tedge/health/mosquitto-c8y-bridge";
+ const C8Y_BRIDGE_UP_PAYLOAD: &str = "1";
+
+ pub fn is_c8y_bridge_up(message: &Message) -> bool {
+ match message.payload_str() {
+ Ok(payload) => {
+ message.topic.name == C8Y_BRIDGE_HEALTH_TOPIC && payload == C8Y_BRIDGE_UP_PAYLOAD
+ }
+ Err(_err) => false,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use mqtt_channel::{Message, Topic};
+ use test_case::test_case;
+
+ use crate::utils::bridge::{is_c8y_bridge_up, C8Y_BRIDGE_HEALTH_TOPIC};
+
+ #[test_case(C8Y_BRIDGE_HEALTH_TOPIC, "1", true)]
+ #[test_case(C8Y_BRIDGE_HEALTH_TOPIC, "0", false)]
+ #[test_case("tedge/not/health/topic", "1", false)]
+ #[test_case("tedge/not/health/topic", "0", false)]
+ fn test_bridge_is_up(topic: &str, payload: &str, expected: bool) {
+ let topic = Topic::new(topic).unwrap();
+ let message = Message::new(&topic, payload);
+
+ let actual = is_c8y_bridge_up(&message);
+ assert_eq!(actual, expected);
+ }
+}