fundamentum_sdk_mqtt/models/
device_command_update.rs

1//! `DeviceCommandUpdate` module
2//!
3
4use bytes::Bytes;
5use serde::{Deserialize, Serialize};
6
7use super::CommandStatus;
8use crate::{Device, Error, PublishOptions, Publishable};
9
10/// `DeviceCommandUpdate`
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
12pub struct DeviceCommandUpdate {
13    /// Unique ID of the command
14    #[serde(skip)]
15    id: u64,
16    /// Command's status
17    status: CommandStatus,
18    /// Status change description
19    message: String,
20}
21
22impl DeviceCommandUpdate {
23    /// Create a new `DeviceCommandUpdate`
24    ///
25    /// # Params
26    ///
27    /// * `status`: The command's status
28    /// * `message`: The command's status description message
29    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    /// Get the status of the command update.
38    #[must_use]
39    pub const fn status(&self) -> &CommandStatus {
40        &self.status
41    }
42
43    /// Get the message of the command update.
44    #[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}