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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#![allow(clippy::needless_update)]
use crate::{resource, util};
use derive_setters::Setters;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct Sensor {
#[serde(skip)]
pub id: String,
pub name: String,
#[serde(rename = "type")]
pub type_name: String,
#[serde(rename = "modelid")]
pub model_id: String,
#[serde(rename = "uniqueid")]
pub unique_id: Option<String>,
#[serde(rename = "manufacturername")]
pub manufacturer_name: Option<String>,
#[serde(rename = "productname")]
pub product_name: Option<String>,
#[serde(rename = "diversityid")]
pub diversity_id: Option<String>,
#[serde(rename = "swversion")]
pub software_version: Option<String>,
pub state: State,
pub config: Config,
pub recycle: Option<bool>,
}
impl Sensor {
pub(crate) fn with_id(self, id: String) -> Self {
Self { id, ..self }
}
}
impl resource::Resource for Sensor {}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct State {
pub presence: Option<bool>,
pub flag: Option<bool>,
#[serde(
rename = "lastupdated",
deserialize_with = "util::deserialize_option_date_time"
)]
pub last_updated: Option<chrono::NaiveDateTime>,
#[serde(rename = "buttonevent")]
pub button_event: Option<u32>,
pub temperature: Option<i32>,
#[serde(rename = "lightlevel")]
pub light_level: Option<u32>,
pub dark: Option<bool>,
pub daylight: Option<bool>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct Config {
pub on: bool,
pub reachable: Option<bool>,
pub battery: Option<u8>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct AttributeModifier {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
impl AttributeModifier {
pub fn new() -> Self {
Self::default()
}
}
impl resource::Modifier for AttributeModifier {
type Id = String;
fn url_suffix(id: Self::Id) -> String {
format!("sensors/{}", id)
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct StateModifier {
#[serde(skip_serializing_if = "Option::is_none")]
pub presence: Option<bool>,
}
impl StateModifier {
pub fn new() -> Self {
Self::default()
}
}
impl resource::Modifier for StateModifier {
type Id = String;
fn url_suffix(id: Self::Id) -> String {
format!("sensors/{}/state", id)
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct ConfigModifier {
#[serde(skip_serializing_if = "Option::is_none")]
pub on: Option<bool>,
}
impl ConfigModifier {
pub fn new() -> Self {
Self::default()
}
}
impl resource::Modifier for ConfigModifier {
type Id = String;
fn url_suffix(id: Self::Id) -> String {
format!("sensors/{}/config", id)
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct Scanner {
#[serde(skip_serializing_if = "Option::is_none", rename = "deviceid")]
pub device_ids: Option<Vec<String>>,
}
impl Scanner {
pub fn new() -> Self {
Self::default()
}
}
impl resource::Scanner for Scanner {
fn url_suffix() -> String {
"sensors".to_owned()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn serialize_attribute_modifier() {
let modifier = AttributeModifier::new();
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({});
assert_eq!(modifier_json, expected_json);
let modifier = AttributeModifier {
name: Some("test".into()),
};
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({"name": "test"});
assert_eq!(modifier_json, expected_json);
}
#[test]
fn serialize_state_modifier() {
let modifier = StateModifier::new();
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({});
assert_eq!(modifier_json, expected_json);
let modifier = StateModifier {
presence: Some(true),
};
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({"presence": true});
assert_eq!(modifier_json, expected_json);
}
#[test]
fn serialize_config_modifier() {
let modifier = ConfigModifier::new();
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({});
assert_eq!(modifier_json, expected_json);
let modifier = ConfigModifier { on: Some(true) };
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({"on": true});
assert_eq!(modifier_json, expected_json);
}
#[test]
fn serialize_scanner() {
let scanner = Scanner::new();
let scanner_json = serde_json::to_value(scanner).unwrap();
let expected_json = json!({});
assert_eq!(scanner_json, expected_json);
let scanner = Scanner {
device_ids: Some(vec!["1".into()]),
};
let scanner_json = serde_json::to_value(scanner).unwrap();
let expected_json = json!({
"deviceid": ["1"]
});
assert_eq!(scanner_json, expected_json);
}
}