1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Fundamentum device representation
//!

/// Fundamentum Device
#[derive(Debug, Clone)]
pub struct Device {
    /// Unique identifier of a device used by the client to recognize his devices.
    serial: String,
    /// Identifier of the project to which the device is provisioned into
    project_id: u64,
    /// Identifier of the region to which the device is provisioned into
    region_id: u64,
    /// Identifier of the registry to which the device is provisioned into
    registry_id: u64,
}

impl Device {
    /// Create a new [`Device`]
    pub fn new<S: Into<String>>(
        serial: S,
        project_id: u64,
        region_id: u64,
        registry_id: u64,
    ) -> Self {
        Self {
            serial: serial.into(),
            project_id,
            region_id,
            registry_id,
        }
    }

    /// Returns the MQTT client id of this [`Device`].
    #[must_use]
    pub fn client_id(&self) -> String {
        format!(
            "projects/{}/regions/{}/registries/{}/devices/{}",
            self.project_id, self.region_id, self.registry_id, self.serial
        )
    }

    /// Returns a reference to the serial of this [`Device`].
    #[must_use]
    pub fn serial(&self) -> &str {
        self.serial.as_ref()
    }

    /// Returns the project id of this [`Device`].
    #[must_use]
    pub const fn project_id(&self) -> u64 {
        self.project_id
    }

    /// Returns the region id of this [`Device`].
    #[must_use]
    pub const fn region_id(&self) -> u64 {
        self.region_id
    }

    /// Returns the registry id of this [`Device`].
    #[must_use]
    pub const fn registry_id(&self) -> u64 {
        self.registry_id
    }
}