-
Notifications
You must be signed in to change notification settings - Fork 1
/
hemtjanst.go
156 lines (139 loc) · 3.39 KB
/
hemtjanst.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sladdlos
import (
"context"
"hemtjan.st/sladdlos/tradfri"
"lib.hemtjan.st/device"
"strconv"
"strings"
"sync"
)
type HemtjanstClient struct {
sync.RWMutex
Id string
Announce bool
SkipGroup bool
SkipBulb bool
transport device.Transport
tree *tradfri.Tree
devices map[string]*HemtjanstDevice
groups map[int]*tradfri.Group
accessories map[int]*tradfri.Accessory
newDevChan chan *tradfri.Accessory
newGroupChan chan *tradfri.Group
}
func NewHemtjanstClient(tree *tradfri.Tree, transport device.Transport, id string) *HemtjanstClient {
h := &HemtjanstClient{
tree: tree,
transport: transport,
Id: id,
SkipBulb: false,
SkipGroup: false,
devices: map[string]*HemtjanstDevice{},
newDevChan: make(chan *tradfri.Accessory),
newGroupChan: make(chan *tradfri.Group),
groups: map[int]*tradfri.Group{},
accessories: map[int]*tradfri.Accessory{},
}
tree.AddCallback(h)
return h
}
func topicFor(a tradfri.Instance, t ...string) string {
return strings.Join(t, "/") + "-" + strconv.Itoa(a.GetInstanceID())
}
func (h *HemtjanstClient) Start(ctx context.Context) {
for {
select {
case d, op := <-h.newDevChan:
if !op {
return
}
go func(d *tradfri.Accessory) {
h.Lock()
defer h.Unlock()
if _, ok := h.accessories[d.GetInstanceID()]; !ok {
h.accessories[d.GetInstanceID()] = d
}
h.ensureDevices()
}(d)
case g, op := <-h.newGroupChan:
if !op {
return
}
go func(g *tradfri.Group) {
h.Lock()
defer h.Unlock()
if _, ok := h.groups[g.GetInstanceID()]; !ok {
h.groups[g.GetInstanceID()] = g
}
h.ensureDevices()
}(g)
case <-ctx.Done():
return
}
}
}
func (h *HemtjanstClient) ensureDevices() {
ownerGroup := map[int]int{}
for _, grp := range h.groups {
topic := topicFor(grp, "light", "grp")
if _, ok := h.devices[topic]; ok {
continue
}
dev := NewHemtjanstGroup(h, topic, grp)
h.devices[topic] = dev
if grp.Members != nil {
for _, member := range grp.Members {
ownerGroup[member] = grp.GetInstanceID()
}
}
}
for _, accessory := range h.accessories {
var topic string
if accessory.IsLight() {
topic = topicFor(accessory, "light", "bulb")
} else if accessory.IsPlug() {
topic = topicFor(accessory, "outlet", "plug")
} else if accessory.IsBlind() {
topic = topicFor(accessory, "windowCovering", "blind")
} else if accessory.IsRemote() {
topic = topicFor(accessory, "remote", "remote")
} else {
topic = topicFor(accessory, "unknown", "unknown")
}
if _, ok := h.devices[topic]; ok {
continue
}
var ownerDev *HemtjanstDevice
var owner *tradfri.Group
if grpId, ok := ownerGroup[accessory.GetInstanceID()]; ok {
if owner, ok = h.groups[grpId]; !ok {
continue
}
} else {
// Wait until we have the group
continue
}
ownerTopic := topicFor(owner, "light", "grp")
var ok bool
if ownerDev, ok = h.devices[ownerTopic]; !ok {
continue
}
dev := NewHemtjanstAccessory(h, topic, accessory, ownerDev)
h.devices[topic] = dev
if ownerDev != nil {
ownerDev.AddMember(dev)
}
}
}
func (h *HemtjanstClient) OnNewAccessory(d *tradfri.Accessory) {
go func() {
h.newDevChan <- d
}()
}
func (h *HemtjanstClient) OnNewGroup(g *tradfri.Group) {
go func() {
h.newGroupChan <- g
}()
}
func (h *HemtjanstClient) OnNewScene(g *tradfri.Group, s *tradfri.Scene) {
}