forked from szatmary/sonos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zoneplayer.go
236 lines (209 loc) · 7.48 KB
/
zoneplayer.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
233
234
235
236
package sonos
import (
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
avt "github.com/Coollision/sonos/AVTransport"
clk "github.com/Coollision/sonos/AlarmClock"
con "github.com/Coollision/sonos/ConnectionManager"
dir "github.com/Coollision/sonos/ContentDirectory"
dev "github.com/Coollision/sonos/DeviceProperties"
gmn "github.com/Coollision/sonos/GroupManagement"
rcg "github.com/Coollision/sonos/GroupRenderingControl"
mus "github.com/Coollision/sonos/MusicServices"
ply "github.com/Coollision/sonos/QPlay"
que "github.com/Coollision/sonos/Queue"
ren "github.com/Coollision/sonos/RenderingControl"
sys "github.com/Coollision/sonos/SystemProperties"
vli "github.com/Coollision/sonos/VirtualLineIn"
zgt "github.com/Coollision/sonos/ZoneGroupTopology"
)
type SpecVersion struct {
XMLName xml.Name `xml:"specVersion"`
Major int `xml:"major"`
Minor int `xml:"minor"`
}
type Service struct {
XMLName xml.Name `xml:"service"`
ServiceType string `xml:"serviceType"`
ServiceId string `xml:"serviceId"`
ControlURL string `xml:"controlURL"`
EventSubURL string `xml:"eventSubURL"`
SCPDURL string `xml:"SCPDURL"`
}
type Icon struct {
XMLName xml.Name `xml:"icon"`
Id string `xml:"id"`
Mimetype string `xml:"mimetype"`
Width int `xml:"width"`
Height int `xml:"height"`
Depth int `xml:"depth"`
Url url.URL `xml:"url"`
}
type Device struct {
XMLName xml.Name `xml:"device"`
DeviceType string `xml:"deviceType"`
FriendlyName string `xml:"friendlyName"`
Manufacturer string `xml:"manufacturer"`
ManufacturerURL string `xml:"manufacturerURL"`
ModelNumber string `xml:"modelNumber"`
ModelDescription string `xml:"modelDescription"`
ModelName string `xml:"modelName"`
ModelURL string `xml:"modelURL"`
SoftwareVersion string `xml:"softwareVersion"`
SwGen string `xml:"swGen"`
HardwareVersion string `xml:"hardwareVersion"`
SerialNum string `xml:"serialNum"`
MACAddress string `xml:"MACAddress"`
UDN string `xml:"UDN"`
Icons []Icon `xml:"iconList>icon"`
MinCompatibleVersion string `xml:"minCompatibleVersion"`
LegacyCompatibleVersion string `xml:"legacyCompatibleVersion"`
ApiVersion string `xml:"apiVersion"`
MinApiVersion string `xml:"minApiVersion"`
DisplayVersion string `xml:"displayVersion"`
ExtraVersion string `xml:"extraVersion"`
RoomName string `xml:"roomName"`
DisplayName string `xml:"displayName"`
ZoneType int `xml:"zoneType"`
Feature1 string `xml:"feature1"`
Feature2 string `xml:"feature2"`
Feature3 string `xml:"feature3"`
Seriesid string `xml:"seriesid"`
Variant int `xml:"variant"`
InternalSpeakerSize float32 `xml:"internalSpeakerSize"`
BassExtension float32 `xml:"bassExtension"`
SatGainOffset float32 `xml:"satGainOffset"`
Memory int `xml:"memory"`
Flash int `xml:"flash"`
FlashRepartitioned int `xml:"flashRepartitioned"`
AmpOnTime int `xml:"ampOnTime"`
RetailMode int `xml:"retailMode"`
Services []Service `xml:"serviceList>service"`
Devices []Device `xml:"deviceList>device"`
}
type Root struct {
XMLName xml.Name `xml:"root"`
Xmlns string `xml:"xmlns,attr"`
SpecVersion SpecVersion `xml:"specVersion"`
Device Device `xml:"device"`
}
type ZonePlayer struct {
Root *Root
HttpClient *http.Client
DeviceDescriptionURL *url.URL
// services
AlarmClock *clk.Service
AVTransport *avt.Service
ConnectionManager *con.Service
ContentDirectory *dir.Service
DeviceProperties *dev.Service
GroupManagement *gmn.Service
GroupRenderingControl *rcg.Service
MusicServices *mus.Service
QPlay *ply.Service
Queue *que.Service
RenderingControl *ren.Service
SystemProperties *sys.Service
VirtualLineIn *vli.Service
ZoneGroupTopology *zgt.Service
}
func NewZonePlayer(deviceDescriptionURL *url.URL) (*ZonePlayer, error) {
zp := ZonePlayer{
Root: &Root{},
HttpClient: &http.Client{},
DeviceDescriptionURL: deviceDescriptionURL,
AlarmClock: clk.NewService(deviceDescriptionURL),
AVTransport: avt.NewService(deviceDescriptionURL),
ConnectionManager: con.NewService(deviceDescriptionURL),
ContentDirectory: dir.NewService(deviceDescriptionURL),
DeviceProperties: dev.NewService(deviceDescriptionURL),
GroupManagement: gmn.NewService(deviceDescriptionURL),
GroupRenderingControl: rcg.NewService(deviceDescriptionURL),
MusicServices: mus.NewService(deviceDescriptionURL),
QPlay: ply.NewService(deviceDescriptionURL),
Queue: que.NewService(deviceDescriptionURL),
RenderingControl: ren.NewService(deviceDescriptionURL),
SystemProperties: sys.NewService(deviceDescriptionURL),
VirtualLineIn: vli.NewService(deviceDescriptionURL),
ZoneGroupTopology: zgt.NewService(deviceDescriptionURL),
}
resp, err := zp.HttpClient.Get(zp.DeviceDescriptionURL.String())
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = xml.Unmarshal(body, zp.Root)
if err != nil {
return nil, err
}
return &zp, nil
}
func (z *ZonePlayer) RoomName() string {
return z.Root.Device.RoomName
}
func (z *ZonePlayer) ModelName() string {
return z.Root.Device.ModelName
}
func (z *ZonePlayer) HardwareVersion() string {
return z.Root.Device.HardwareVersion
}
func (z *ZonePlayer) SerialNum() string {
return z.Root.Device.SerialNum
}
func (z *ZonePlayer) IsCoordinator() bool {
zoneGroupState, err := z.GetZoneGroupState()
if err != nil {
return false
}
for _, group := range zoneGroupState.ZoneGroups {
if "uuid:"+group.Coordinator == z.Root.Device.UDN {
return true
}
}
return false
}
// Convience functions
func (z *ZonePlayer) GetZoneGroupState() (*ZoneGroupState, error) {
zoneGroupStateResponse, err := z.ZoneGroupTopology.GetZoneGroupState(z.HttpClient, &zgt.GetZoneGroupStateArgs{})
if err != nil {
return nil, err
}
var zoneGroupState ZoneGroupState
err = xml.Unmarshal([]byte(zoneGroupStateResponse.ZoneGroupState), &zoneGroupState)
if err != nil {
return nil, err
}
return &zoneGroupState, nil
}
func (z *ZonePlayer) GetVolume() (int, error) {
res, err := z.RenderingControl.GetVolume(z.HttpClient, &ren.GetVolumeArgs{Channel: "Master"})
if err != nil {
return 0, err
}
return int(res.CurrentVolume), err
}
func (z *ZonePlayer) SetVolume(desiredVolume int) error {
_, err := z.RenderingControl.SetVolume(z.HttpClient, &ren.SetVolumeArgs{
Channel: "Master",
DesiredVolume: uint16(desiredVolume),
})
return err
}
func (z *ZonePlayer) Play() error {
_, err := z.AVTransport.Play(z.HttpClient, &avt.PlayArgs{
Speed: 1,
})
return err
}
func (z *ZonePlayer) SetAVTransportURI(url string) error {
_, err := z.AVTransport.SetAVTransportURI(z.HttpClient, &avt.SetAVTransportURIArgs{
CurrentURI: url,
})
return err
}