alarm_dot_com/models/
system.rs1use serde::{Deserialize, Serialize};
4
5use super::jsonapi::Resource;
6use crate::error::{AlarmError, Result};
7
8#[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#[derive(Debug, Clone)]
24pub struct System {
25 pub id: String,
26 pub name: String,
27 pub partition_ids: Vec<String>,
29 pub sensor_ids: Vec<String>,
31 pub lock_ids: Vec<String>,
33 pub garage_door_ids: Vec<String>,
35 pub light_ids: Vec<String>,
37 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}