Skip to main content

alarm_dot_com/models/
system.rs

1//! System model.
2
3use serde::{Deserialize, Serialize};
4
5use super::jsonapi::Resource;
6use crate::error::{AlarmError, Result};
7
8/// System attributes from the API.
9#[derive(Debug, Clone, Deserialize, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct SystemAttributes {
12    #[serde(default)]
13    pub description: Option<String>,
14    #[serde(default)]
15    pub has_snapshots: Option<bool>,
16    #[serde(default)]
17    pub supports_secure_arming: Option<bool>,
18    #[serde(default)]
19    pub remain_logged_in: Option<bool>,
20}
21
22/// An Alarm.com system.
23#[derive(Debug, Clone)]
24pub struct System {
25    pub id: String,
26    pub name: String,
27    /// IDs of partitions in this system.
28    pub partition_ids: Vec<String>,
29    /// IDs of sensors in this system.
30    pub sensor_ids: Vec<String>,
31    /// IDs of locks in this system.
32    pub lock_ids: Vec<String>,
33    /// IDs of garage doors in this system.
34    pub garage_door_ids: Vec<String>,
35    /// IDs of lights in this system.
36    pub light_ids: Vec<String>,
37    /// IDs of thermostats in this system.
38    pub thermostat_ids: Vec<String>,
39    pub attributes: SystemAttributes,
40}
41
42impl System {
43    pub fn from_resource(resource: &Resource) -> Result<Self> {
44        let attrs: SystemAttributes =
45            serde_json::from_value(resource.attributes.clone()).map_err(|e| {
46                AlarmError::UnexpectedResponse(format!("failed to parse system attributes: {e}"))
47            })?;
48
49        let name = attrs
50            .description
51            .clone()
52            .unwrap_or_else(|| format!("System {}", resource.id));
53
54        Ok(System {
55            id: resource.id.clone(),
56            name,
57            partition_ids: resource.related_ids("partitions"),
58            sensor_ids: resource.related_ids("sensors"),
59            lock_ids: resource.related_ids("locks"),
60            garage_door_ids: resource.related_ids("garageDoors"),
61            light_ids: resource.related_ids("lights"),
62            thermostat_ids: resource.related_ids("thermostats"),
63            attributes: attrs,
64        })
65    }
66}