-
Notifications
You must be signed in to change notification settings - Fork 43
/
s.go
75 lines (61 loc) · 1.23 KB
/
s.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
package service
import (
"github.com/brutella/hap/characteristic"
"encoding/json"
)
type S struct {
Id uint64
Type string
Hidden bool
Primary bool
Linked []*S
Cs []*characteristic.C
}
// New returns a new service.
func New(typ string) *S {
return &S{
Type: typ,
Cs: []*characteristic.C{},
Linked: []*S{},
}
}
func (s *S) AddC(c *characteristic.C) {
s.Cs = append(s.Cs, c)
}
func (s *S) AddS(other *S) {
s.Linked = append(s.Linked, other)
}
func (s *S) C(typ string) *characteristic.C {
for _, c := range s.Cs {
if c.Type == typ {
return c
}
}
return nil
}
func (s *S) MarshalJSON() ([]byte, error) {
linked := []uint64{}
for _, s := range s.Linked {
linked = append(linked, s.Id)
}
d := struct {
Id uint64 `json:"iid"`
Type string `json:"type"`
Cs []*characteristic.C `json:"characteristics"`
Hidden *bool `json:"hidden,omitempty"`
Primary *bool `json:"primary,omitempty"`
Linked []uint64 `json:"linked,omitempty"`
}{
Id: s.Id,
Type: s.Type,
Cs: s.Cs,
Linked: linked,
}
if s.Hidden {
d.Hidden = &s.Hidden
}
if s.Primary {
d.Primary = &s.Primary
}
return json.Marshal(&d)
}