-
Notifications
You must be signed in to change notification settings - Fork 189
/
accessory.go
140 lines (114 loc) · 2.99 KB
/
accessory.go
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
package accessory
import (
"github.com/brutella/hc/service"
)
type Info struct {
Name string
SerialNumber string
Manufacturer string
Model string
FirmwareRevision string
ID uint64
}
// Accessory is a HomeKit accessory.
//
// An accessory contains services, which themselves contain characteristics.
// Every accessory has the "accessory info" service by default which consists
// of characteristics to identify the accessory: name, model, manufacturer,...
type Accessory struct {
ID uint64 `json:"aid"`
Services []*service.Service `json:"services"`
Type AccessoryType `json:"-"`
Info *service.AccessoryInformation `json:"-"`
idCount uint64
onIdentify func()
}
// New returns an accessory which implements model.Accessory.
func New(info Info, typ AccessoryType) *Accessory {
svc := service.NewAccessoryInformation()
if name := info.Name; len(name) > 0 {
svc.Name.SetValue(name)
} else {
svc.Name.SetValue("undefined")
}
if serial := info.SerialNumber; len(serial) > 0 {
svc.SerialNumber.SetValue(serial)
} else {
svc.SerialNumber.SetValue("undefined")
}
if manufacturer := info.Manufacturer; len(manufacturer) > 0 {
svc.Manufacturer.SetValue(manufacturer)
} else {
svc.Manufacturer.SetValue("undefined")
}
if model := info.Model; len(model) > 0 {
svc.Model.SetValue(model)
} else {
svc.Model.SetValue("undefined")
}
if version := info.FirmwareRevision; len(version) > 0 {
svc.FirmwareRevision.SetValue(version)
} else {
svc.FirmwareRevision.SetValue("undefined")
}
var id uint64 = 0
if info.ID > id {
id = info.ID
}
acc := &Accessory{
idCount: 1,
ID: id,
Info: svc,
Type: typ,
}
acc.AddService(acc.Info.Service)
svc.Identify.OnValueRemoteUpdate(func(value bool) {
acc.Identify()
})
return acc
}
func (a *Accessory) GetServices() []*service.Service {
result := make([]*service.Service, 0)
for _, s := range a.Services {
result = append(result, s)
}
return result
}
func (a *Accessory) OnIdentify(fn func()) {
a.onIdentify = fn
}
func (a *Accessory) Identify() {
if a.onIdentify != nil {
a.onIdentify()
}
}
// Adds a service to the accessory and updates the ids of the service and the corresponding characteristics
func (a *Accessory) AddService(s *service.Service) {
a.Services = append(a.Services, s)
}
// UpdateIDs updates the service and characteirstic ids.
func (a *Accessory) UpdateIDs() {
for _, s := range a.Services {
s.ID = a.idCount
a.idCount++
for _, c := range s.Characteristics {
c.ID = a.idCount
a.idCount++
}
}
}
// Equal returns true when receiver has the same services and id as the argument.
func (a *Accessory) Equal(other interface{}) bool {
if accessory, ok := other.(*Accessory); ok == true {
if len(a.Services) != len(accessory.Services) {
return false
}
for i, s := range a.Services {
if s.Equal(accessory.Services[i]) == false {
return false
}
}
return a.ID == accessory.ID
}
return false
}