-
Notifications
You must be signed in to change notification settings - Fork 43
/
a.go
80 lines (66 loc) · 1.42 KB
/
a.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
package accessory
import (
"github.com/brutella/hap/service"
"encoding/json"
"net/http"
)
type A struct {
Id uint64
Type byte
Info *service.AccessoryInformation
Ss []*service.S
// IdentifyFunc is called when a client
// makes a POST to the /identify endpoint.
IdentifyFunc func(*http.Request)
}
type Info struct {
Name string
SerialNumber string
Manufacturer string
Model string
Firmware string
}
func New(info Info, typ byte) *A {
s := service.NewAccessoryInformation()
s.Name.SetValue("-")
s.Model.SetValue("-")
s.SerialNumber.SetValue("-")
s.Manufacturer.SetValue("-")
s.FirmwareRevision.SetValue("-")
if info.Name != "" {
s.Name.Val = info.Name
}
if info.Model != "" {
s.Model.Val = info.Model
}
if info.SerialNumber != "" {
s.SerialNumber.Val = info.SerialNumber
}
if info.Manufacturer != "" {
s.Manufacturer.Val = info.Manufacturer
}
if info.Firmware != "" {
s.FirmwareRevision.Val = info.Firmware
}
return &A{
Type: typ,
Info: s,
Ss: []*service.S{s.S},
}
}
// Adds a service to the accessory and updates the ids of the service and the corresponding characteristics
func (a *A) AddS(s *service.S) {
a.Ss = append(a.Ss, s)
}
func (a *A) Name() string {
return a.Info.Name.Value()
}
func (a *A) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Id uint64 `json:"aid"`
Ss []*service.S `json:"services"`
}{
Id: a.Id,
Ss: a.Ss,
})
}