fundamentum_sdk_mqtt/models/
device_command_update.rs1use bytes::Bytes;
5use serde::{Deserialize, Serialize};
6
7use super::CommandStatus;
8use crate::{Device, Error, PublishOptions, Publishable};
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
12pub struct DeviceCommandUpdate {
13 #[serde(skip)]
15 id: u64,
16 status: CommandStatus,
18 message: String,
20}
21
22impl DeviceCommandUpdate {
23 pub fn new<S: Into<String>>(id: u64, status: CommandStatus, message: S) -> Self {
30 Self {
31 id,
32 status,
33 message: message.into(),
34 }
35 }
36
37 #[must_use]
39 pub const fn status(&self) -> &CommandStatus {
40 &self.status
41 }
42
43 #[must_use]
45 #[expect(clippy::missing_const_for_fn, reason = "False positive")]
46 pub fn message(&self) -> &str {
47 &self.message
48 }
49}
50
51impl Publishable for DeviceCommandUpdate {
52 type Error = Error;
53
54 fn topic(&self, device: &Device) -> impl Into<String> + Send {
55 format!(
56 "registries/{}/devices/{}/commands/{}",
57 device.registry_id(),
58 device.serial(),
59 self.id
60 )
61 }
62
63 fn payload(&self) -> Result<impl Into<Bytes> + Send, Error> {
64 Ok(serde_json::to_vec(self)?)
65 }
66
67 fn publish_overrides(&self) -> PublishOptions {
68 PublishOptions::default()
69 }
70}