fundamentum_sdk_mqtt/models/
telemetry.rs1use bytes::Bytes;
4use serde::{Deserialize, Serialize};
5
6use crate::{Device, Error, PublishOptions, Publishable};
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
10pub struct Telemetry<S, P>
11where
12 S: Into<String> + Clone + Send,
13 P: Into<Bytes> + Clone + Send,
14{
15 sub_topic: Option<S>,
16 payload: P,
17}
18
19impl<S, P> Telemetry<S, P>
20where
21 S: Into<String> + Clone + Send,
22 P: Into<Bytes> + Clone + Send,
23{
24 #[must_use]
26 pub const fn new(sub_topic: Option<S>, payload: P) -> Self {
27 Self { sub_topic, payload }
28 }
29}
30
31impl<S, P> Publishable for Telemetry<S, P>
32where
33 S: Into<String> + Clone + Send,
34 P: Into<Bytes> + Clone + Send,
35{
36 type Error = Error;
37
38 fn topic(&self, device: &Device) -> impl Into<String> + Send {
39 format!(
40 "registries/{}/devices/{}/events{}",
41 device.registry_id(),
42 device.serial(),
43 self.sub_topic
44 .as_ref()
45 .map_or(String::new(), |s| format!("/{}", s.clone().into()))
46 )
47 }
48
49 fn payload(&self) -> Result<impl Into<bytes::Bytes> + Send, Error> {
50 Ok(self.payload.clone())
51 }
52
53 fn publish_overrides(&self) -> PublishOptions {
54 PublishOptions::default()
55 }
56}