/
feature.go
232 lines (216 loc) · 4.95 KB
/
feature.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package feature // import "lib.hemtjan.st/feature"
import (
"strconv"
"time"
)
// Info holds information about a feature
type Info struct {
Min int `json:"min,omitempty"`
Max int `json:"max,omitempty"`
Step int `json:"step,omitempty"`
GetTopic string `json:"getTopic,omitempty"`
SetTopic string `json:"setTopic,omitempty"`
}
// Feature represents a feature of a device
type Feature interface {
Name() string
Min() int
Max() int
Step() int
Exists() bool
Set(string) error
OnUpdate() (chan string, error)
OnUpdateFunc(func(string)) error
Update(string) error
OnSet() (chan string, error)
OnSetFunc(func(string)) error
Value() string
UpdateInfo(*Info) []*InfoUpdate
GetTopic() string
SetTopic() string
}
// Transport is the feature's transport
type Transport interface {
SubscribeFeature(string) chan []byte
UpdateFeature(*Info, []byte)
SetFeature(*Info, []byte)
}
type feature struct {
info *Info
name string
transport Transport
value string
updateSub bool
}
// InfoUpdate represents an update to the feature's current
// info
type InfoUpdate struct {
Name string
Old string
New string
}
// New creates a new feature with the specified name and info
// and embeds the transport over which it can be interacted with
func New(name string, info *Info, transport Transport) Feature {
return &feature{
name: name,
info: info,
transport: transport,
}
}
func (f *feature) Name() string { return f.name }
func (f *feature) Min() int { return f.info.Min }
func (f *feature) Max() int { return f.info.Max }
func (f *feature) Step() int { return f.info.Step }
func (f *feature) Exists() bool { return true }
func (f *feature) GetTopic() string { return f.info.GetTopic }
func (f *feature) SetTopic() string { return f.info.SetTopic }
// UpdateInfo updates the Info of the feature
func (f *feature) UpdateInfo(i *Info) (u []*InfoUpdate) {
if i.GetTopic != "" && i.GetTopic != f.info.GetTopic {
u = append(u, &InfoUpdate{"getTopic", f.info.GetTopic, i.GetTopic})
f.info.GetTopic = i.GetTopic
}
if i.SetTopic != "" && i.SetTopic != f.info.SetTopic {
u = append(u, &InfoUpdate{"setTopic", f.info.SetTopic, i.SetTopic})
f.info.SetTopic = i.SetTopic
}
if i.Min != f.info.Min {
u = append(u, &InfoUpdate{"min", strconv.Itoa(f.info.Min), strconv.Itoa(i.Min)})
f.info.Min = i.Min
}
if i.Max != f.info.Max {
u = append(u, &InfoUpdate{"max", strconv.Itoa(f.info.Max), strconv.Itoa(i.Max)})
f.info.Max = i.Max
}
if i.Step != f.info.Step {
u = append(u, &InfoUpdate{"step", strconv.Itoa(f.info.Step), strconv.Itoa(i.Step)})
f.info.Step = i.Step
}
return
}
// Update publishes the new value of the feature
// This is a no-op if the new and current value are the same
func (f *feature) Update(s string) error {
if f.value == s {
return nil
}
f.transport.UpdateFeature(f.info, []byte(s))
f.value = s
return nil
}
// Set updates the value of the feature
func (f *feature) Set(s string) error {
f.transport.SetFeature(f.info, []byte(s))
return nil
}
// OnUpdate returns a channel on which updates
// of the feature's value are published
func (f *feature) OnUpdate() (chan string, error) {
res := f.transport.SubscribeFeature(f.info.GetTopic)
ch := make(chan string, 5)
go func() {
var value string
for {
msg, open := <-res
if !open {
close(ch)
return
}
smsg := string(msg)
if value == smsg {
continue
}
value = smsg
ch <- smsg
}
}()
return ch, nil
}
// OnUpdateFunc calls the provided callback when updates
// of the feature's value are published
func (f *feature) OnUpdateFunc(fn func(val string)) error {
ch, err := f.OnUpdate()
if err != nil {
return err
}
go func() {
for {
val, open := <-ch
if !open {
return
}
fn(val)
}
}()
return nil
}
func (f *feature) Value() string {
if !f.updateSub {
f.updateSub = true
ch := make(chan struct{})
go func(ch chan struct{}) {
defer func() {
if ch != nil {
close(ch)
}
}()
up, err := f.OnUpdate()
if err != nil {
return
}
for {
val, open := <-up
if !open {
f.updateSub = false
return
}
f.value = string(val)
if ch != nil {
close(ch)
ch = nil
}
}
}(ch)
select {
case <-ch:
case <-time.After(1 * time.Second):
}
}
return f.value
}
// OnSet returns a channel on which notifications of a
// feature's intended new value are published
func (f *feature) OnSet() (chan string, error) {
res := f.transport.SubscribeFeature(f.info.SetTopic)
ch := make(chan string, 5)
go func() {
for {
msg, open := <-res
if !open {
close(ch)
return
}
ch <- string(msg)
}
}()
return ch, nil
}
// OnSetFunc calls the provided callback when a
// feature's intended new value is published
func (f *feature) OnSetFunc(fn func(val string)) error {
ch, err := f.OnSet()
if err != nil {
return err
}
go func() {
for {
val, open := <-ch
if !open {
return
}
fn(val)
}
}()
return nil
}