-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
46 lines (40 loc) · 891 Bytes
/
example.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
package golightmyroom
import (
"context"
"sync"
)
func main() {
lights, err := multipleLights("mac address 1", "mac address 2")
if err != nil {
panic(err)
}
runMultiple(lights, func(light Light) {
light.SetBrightness(0.5)
if temperatureControlled, ok := light.(TemperatureControl); ok {
temperatureControlled.SetTemperature(TemperatureWarmWhite)
}
})
}
func multipleLights(macAddresses ...string) (lights []Light, err error) {
for _, macAddress := range macAddresses {
var light Light
light, err = ConnectToBluetoothWithContext(macAddress, context.Background())
if err != nil {
return nil, err
}
lights = append(lights, light)
}
return lights, nil
}
func runMultiple(lights []Light, f func(light Light)) {
var wg sync.WaitGroup
for _, light := range lights {
wg.Add(1)
light := light
go func() {
f(light)
wg.Done()
}()
}
wg.Wait()
}