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
use crate::{
accessory::{HapAccessory, HapAccessoryService, Accessory, Information},
service::{HapService, accessory_information::AccessoryInformation, fan_v2},
event::EmitterPtr,
Error,
};
pub type Fanv2 = Accessory<Fanv2Inner>;
#[derive(Default)]
pub struct Fanv2Inner {
id: u64,
pub accessory_information: AccessoryInformation,
pub fan_v2: fan_v2::Fanv2,
}
impl HapAccessory for Fanv2Inner {
fn get_id(&self) -> u64 {
self.id
}
fn set_id(&mut self, id: u64) {
self.id = id;
}
fn get_services(&self) -> Vec<&HapAccessoryService> {
vec![
&self.accessory_information,
&self.fan_v2,
]
}
fn get_mut_services(&mut self) -> Vec<&mut HapAccessoryService> {
vec![
&mut self.accessory_information,
&mut self.fan_v2,
]
}
fn get_mut_information(&mut self) -> &mut AccessoryInformation {
&mut self.accessory_information
}
fn init_iids(&mut self, accessory_id: u64, event_emitter: EmitterPtr) -> Result<(), Error> {
let mut next_iid = 1;
for service in self.get_mut_services() {
service.set_id(next_iid);
next_iid += 1;
for characteristic in service.get_mut_characteristics() {
characteristic.set_id(next_iid)?;
characteristic.set_accessory_id(accessory_id)?;
characteristic.set_event_emitter(Some(event_emitter.clone()))?;
next_iid += 1;
}
}
Ok(())
}
}
pub fn new(information: Information) -> Result<Fanv2, Error> {
let mut fan_v2 = fan_v2::new();
fan_v2.set_primary(true);
Ok(Fanv2::new(Fanv2Inner {
accessory_information: information.to_service()?,
fan_v2,
..Default::default()
}))
}