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
use serde::ser::{Serialize, SerializeStruct, Serializer};
use crate::{
accessory::{AccessoryInformation, HapAccessory},
service::{
accessory_information::AccessoryInformationService,
speaker::SpeakerService,
television::TelevisionService,
HapService,
},
HapType,
Result,
};
#[derive(Default)]
pub struct TelevisionAccessory {
id: u64,
pub accessory_information: AccessoryInformationService,
pub television: TelevisionService,
pub speaker: SpeakerService,
}
impl TelevisionAccessory {
pub fn new(id: u64, information: AccessoryInformation) -> Result<Self> {
let accessory_information = information.to_service(1, id)?;
let television_id = accessory_information.get_characteristics().len() as u64;
let mut television = TelevisionService::new(1 + television_id + 1, id);
television.set_primary(true);
let speaker_id = television_id + television.get_characteristics().len() as u64;
let mut speaker = SpeakerService::new(1 + speaker_id + 1, id);
speaker.set_primary(true);
Ok(Self {
id,
accessory_information,
television,
speaker,
})
}
}
impl HapAccessory for TelevisionAccessory {
fn get_id(&self) -> u64 { self.id }
fn set_id(&mut self, id: u64) { self.id = id; }
fn get_service(&self, hap_type: HapType) -> Option<&dyn HapService> {
for service in self.get_services() {
if service.get_type() == hap_type {
return Some(service);
}
}
None
}
fn get_mut_service(&mut self, hap_type: HapType) -> Option<&mut dyn HapService> {
for service in self.get_mut_services() {
if service.get_type() == hap_type {
return Some(service);
}
}
None
}
fn get_services(&self) -> Vec<&dyn HapService> {
vec![&self.accessory_information, &self.television, &self.speaker]
}
fn get_mut_services(&mut self) -> Vec<&mut dyn HapService> {
vec![&mut self.accessory_information, &mut self.television, &mut self.speaker]
}
}
impl Serialize for TelevisionAccessory {
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
let mut state = serializer.serialize_struct("HapAccessory", 2)?;
state.serialize_field("aid", &self.get_id())?;
state.serialize_field("services", &self.get_services())?;
state.end()
}
}