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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use crate::{resource, util};
use chrono::{NaiveDateTime, NaiveTime};
use derive_setters::Setters;
use serde::{Deserialize, Deserializer, Serialize};
use serde_repr::Deserialize_repr;
use std::{collections::HashMap, net::IpAddr};
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct Config {
pub name: String,
#[serde(rename = "swupdate2")]
pub software_update: SoftwareUpdate,
#[serde(rename = "swversion")]
pub software_version: String,
#[serde(rename = "apiversion")]
pub api_version: String,
#[serde(rename = "linkbutton")]
pub link_button: bool,
#[serde(rename = "ipaddress")]
pub ip_address: IpAddr,
#[serde(rename = "mac")]
pub mac_address: String,
pub netmask: IpAddr,
pub gateway: IpAddr,
pub dhcp: bool,
#[serde(rename = "portalservices")]
pub portal_services: bool,
#[serde(rename = "portalconnection")]
pub portal_connection: ServiceStatus,
#[serde(rename = "portalstate")]
pub portal_state: PortalState,
#[serde(rename = "internetservices")]
pub internet_services: InternetServices,
#[serde(rename = "UTC")]
pub current_time: NaiveDateTime,
#[serde(
rename = "localtime",
deserialize_with = "util::deserialize_option_date_time"
)]
pub local_time: Option<NaiveDateTime>,
#[serde(deserialize_with = "util::deserialize_option_string")]
pub timezone: Option<String>,
#[serde(rename = "zigbeechannel")]
pub zigbee_channel: u8,
#[serde(rename = "modelid")]
pub model_id: String,
#[serde(rename = "bridgeid")]
pub bridge_id: String,
#[serde(rename = "factorynew")]
pub factory_new: bool,
#[serde(rename = "replacesbridgeid")]
pub replaces_bridge_id: Option<String>,
#[serde(rename = "datastoreversion")]
pub datastore_version: String,
#[serde(rename = "starterkitid")]
pub starterkit_id: String,
pub backup: Backup,
#[serde(deserialize_with = "deserialize_whitelist")]
pub whitelist: Vec<User>,
}
impl resource::Resource for Config {}
fn deserialize_whitelist<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Vec<User>, D::Error> {
let map: HashMap<String, User> = Deserialize::deserialize(deserializer)?;
Ok(map.into_iter().map(|(id, user)| user.with_id(id)).collect())
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct SoftwareUpdate {
pub state: SoftwareUpdateState,
#[serde(rename = "checkforupdate")]
pub check: bool,
#[serde(rename = "autoinstall")]
pub auto_install: SoftwareUpdateAutoInstall,
#[serde(rename = "lastchange")]
pub last_change: Option<NaiveDateTime>,
#[serde(rename = "lastinstall")]
pub last_install: Option<NaiveDateTime>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SoftwareUpdateState {
Unkown,
NoUpdates,
Transferring,
AnyReadyToInstall,
AllReadyToInstall,
Installing,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct SoftwareUpdateAutoInstall {
pub on: bool,
#[serde(
rename = "updatetime",
deserialize_with = "util::deserialize_option_time"
)]
pub update_time: Option<NaiveTime>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct PortalState {
pub signedon: bool,
pub incoming: bool,
pub outgoing: bool,
pub communication: ServiceStatus,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct InternetServices {
pub internet: ServiceStatus,
#[serde(rename = "remoteaccess")]
pub remote_access: ServiceStatus,
pub time: ServiceStatus,
#[serde(rename = "swupdate")]
pub software_update: ServiceStatus,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ServiceStatus {
Connected,
Disconnected,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct Backup {
pub status: BackupStatus,
#[serde(rename = "errorcode")]
pub error: BackupError,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub enum BackupStatus {
#[serde(rename = "idle")]
Idle,
#[serde(rename = "startmigration")]
StartMigration,
#[serde(rename = "fileready_disabled")]
FilereadyDisabled,
#[serde(rename = "prepare_restore")]
PrepareRestore,
#[serde(rename = "restoring")]
Restoring,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize_repr)]
#[repr(u8)]
pub enum BackupError {
None = 0,
ExportFailed = 1,
ImportFailed = 2,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct User {
#[serde(skip)]
pub id: String,
pub name: String,
#[serde(rename = "last use date")]
pub last_use_date: NaiveDateTime,
#[serde(rename = "create date")]
pub create_date: NaiveDateTime,
}
impl User {
fn with_id(self, id: String) -> Self {
Self { id, ..self }
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct Modifier {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "ipaddress")]
pub ip_address: Option<IpAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub netmask: Option<IpAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gateway: Option<IpAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dhcp: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", rename = "proxyport")]
pub proxy_port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none", rename = "proxyaddress")]
pub proxy_address: Option<IpAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub linkbutton: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub touchlink: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", rename = "zigbeechannel")]
pub zigbee_channel: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none", rename = "UTC")]
pub current_time: Option<NaiveDateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timezone: Option<String>,
}
impl Modifier {
pub fn new() -> Self {
Self::default()
}
}
impl resource::Modifier for Modifier {
type Id = ();
fn url_suffix(_id: Self::Id) -> String {
"config".to_owned()
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
use serde_json::json;
use std::net::Ipv4Addr;
#[test]
fn serialize_modifier() {
let modifier = Modifier::new();
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({});
assert_eq!(modifier_json, expected_json);
let modifier = Modifier {
name: Some("test".into()),
ip_address: Some(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2))),
netmask: Some(IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0))),
gateway: Some(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1))),
dhcp: Some(true),
proxy_port: Some(0),
proxy_address: Some(IpAddr::V4(Ipv4Addr::new(192, 168, 2, 1))),
linkbutton: Some(false),
touchlink: Some(false),
zigbee_channel: Some(1),
current_time: Some(NaiveDateTime::new(
NaiveDate::from_ymd(2020, 1, 1),
NaiveTime::from_hms(0, 0, 0),
)),
timezone: Some("Europe/Berlin".into()),
};
let modifier_json = serde_json::to_value(modifier).unwrap();
let expected_json = json!({
"name": "test",
"ipaddress": "192.168.1.2",
"netmask": "255.255.255.0",
"gateway": "192.168.1.1",
"dhcp": true,
"proxyport": 0,
"proxyaddress": "192.168.2.1",
"linkbutton": false,
"touchlink": false,
"zigbeechannel": 1,
"UTC": "2020-01-01T00:00:00",
"timezone": "Europe/Berlin"
});
assert_eq!(modifier_json, expected_json);
}
}