-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessory.go
120 lines (100 loc) · 3.57 KB
/
accessory.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
package hksoundtouch
import (
"fmt"
"net"
"time"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/log"
"github.com/brutella/hc/service"
ba "github.com/llun/hkbridge/accessories"
"github.com/llun/soundtouch-golang"
)
const BASE_TYPE = "00074"
type SoundTouch struct {
*accessory.Accessory
Speaker *service.Speaker
Volume *characteristic.Volume
AUX *Button
Preset1 *Button
Preset2 *Button
Preset3 *Button
Preset4 *Button
Preset5 *Button
Preset6 *Button
IP *IP
SoundTouch *soundtouch.Speaker
power bool
nowPlaying soundtouch.NowPlaying
worker *ba.Worker
pollingCh <-chan time.Time
}
func Lookup(iface *net.Interface, worker *ba.Worker) []*SoundTouch {
var services []*SoundTouch
speakerCh := soundtouch.Lookup(iface)
for speaker := range speakerCh {
info, err := speaker.Info()
if err != nil {
log.Debug.Fatal(err)
}
services = append(services, NewSoundTouch(speaker, info.DeviceID, info.Type, worker))
}
return services
}
func NewSoundTouch(speaker *soundtouch.Speaker, serial, model string, worker *ba.Worker) *SoundTouch {
info := accessory.Info{
Name: "SoundTouch",
Manufacturer: "Bose",
SerialNumber: serial,
Model: model,
}
acc := SoundTouch{
worker: worker,
}
acc.Accessory = accessory.New(info, accessory.TypeOther)
acc.SoundTouch = speaker
acc.Speaker = acc.createSpeakerService()
acc.AddService(acc.Speaker.Service)
go acc.PollingState()
return &acc
}
func (s *SoundTouch) PollingState() {
s.worker.AddAction(NewGetNowPlaying(s, s.SoundTouch))
s.worker.AddAction(NewGetVolume(s, s.SoundTouch))
for range s.pollingCh {
s.worker.AddAction(NewGetNowPlaying(s, s.SoundTouch))
s.worker.AddAction(NewGetVolume(s, s.SoundTouch))
}
}
func (s *SoundTouch) createSpeakerService() *service.Speaker {
speaker := service.NewSpeaker()
s.Volume = s.createVolume(s.SoundTouch)
speaker.AddCharacteristic(s.Volume.Characteristic)
s.IP = s.createIP(s.SoundTouch)
speaker.AddCharacteristic(s.IP.Characteristic)
s.Preset1 = s.createButton(fmt.Sprintf("%v%v", 1, BASE_TYPE), "Preset1", soundtouch.PRESET_1, s.SoundTouch)
speaker.AddCharacteristic(s.Preset1.Characteristic)
s.Preset2 = s.createButton(fmt.Sprintf("%v%v", 2, BASE_TYPE), "Preset2", soundtouch.PRESET_2, s.SoundTouch)
speaker.AddCharacteristic(s.Preset2.Characteristic)
s.Preset3 = s.createButton(fmt.Sprintf("%v%v", 3, BASE_TYPE), "Preset3", soundtouch.PRESET_3, s.SoundTouch)
speaker.AddCharacteristic(s.Preset3.Characteristic)
s.Preset4 = s.createButton(fmt.Sprintf("%v%v", 4, BASE_TYPE), "Preset4", soundtouch.PRESET_4, s.SoundTouch)
speaker.AddCharacteristic(s.Preset4.Characteristic)
s.Preset5 = s.createButton(fmt.Sprintf("%v%v", 5, BASE_TYPE), "Preset5", soundtouch.PRESET_5, s.SoundTouch)
speaker.AddCharacteristic(s.Preset5.Characteristic)
s.Preset6 = s.createButton(fmt.Sprintf("%v%v", 6, BASE_TYPE), "Preset6", soundtouch.PRESET_6, s.SoundTouch)
speaker.AddCharacteristic(s.Preset6.Characteristic)
s.AUX = s.createButton(fmt.Sprintf("%v%v", 20, BASE_TYPE), "AUX", soundtouch.AUX, s.SoundTouch)
speaker.AddCharacteristic(s.AUX.Characteristic)
s.setupMute(speaker.Mute, s.SoundTouch)
return speaker
}
func AllAccessories(config ba.AccessoryConfig, iface *net.Interface, worker *ba.Worker) []*accessory.Accessory {
speakers := Lookup(iface, worker)
soundtouchAccessories := make([]*accessory.Accessory, len(speakers))
for idx, speaker := range speakers {
soundtouchAccessories[idx] = speaker.Accessory
}
log.Info.Println("Soundtouchs, %v", soundtouchAccessories)
return soundtouchAccessories
}