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
use accessory::{HapAccessory, HapAccessoryService, Accessory, Information};
use service::{HapService, accessory_information::AccessoryInformation, temperature_sensor};
use event::EmitterPtr;
use Error;
pub type TemperatureSensor = Accessory<TemperatureSensorInner>;
#[derive(Default)]
pub struct TemperatureSensorInner {
id: u64,
pub accessory_information: AccessoryInformation,
pub temperature_sensor: temperature_sensor::TemperatureSensor,
}
impl HapAccessory for TemperatureSensorInner {
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.temperature_sensor,
]
}
fn get_mut_services(&mut self) -> Vec<&mut HapAccessoryService> {
vec![
&mut self.accessory_information,
&mut self.temperature_sensor,
]
}
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<TemperatureSensor, Error> {
let mut temperature_sensor = temperature_sensor::new();
temperature_sensor.set_primary(true);
Ok(TemperatureSensor::new(TemperatureSensorInner {
accessory_information: information.to_service()?,
temperature_sensor,
..Default::default()
}))
}