forked from szatmary/sonos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
zoneplayer.go
525 lines (456 loc) · 13.4 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package sonos
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
avt "github.com/caglar10ur/sonos/services/AVTransport"
clk "github.com/caglar10ur/sonos/services/AlarmClock"
ain "github.com/caglar10ur/sonos/services/AudioIn"
con "github.com/caglar10ur/sonos/services/ConnectionManager"
dir "github.com/caglar10ur/sonos/services/ContentDirectory"
dev "github.com/caglar10ur/sonos/services/DeviceProperties"
gmn "github.com/caglar10ur/sonos/services/GroupManagement"
rcg "github.com/caglar10ur/sonos/services/GroupRenderingControl"
mus "github.com/caglar10ur/sonos/services/MusicServices"
ply "github.com/caglar10ur/sonos/services/QPlay"
que "github.com/caglar10ur/sonos/services/Queue"
ren "github.com/caglar10ur/sonos/services/RenderingControl"
sys "github.com/caglar10ur/sonos/services/SystemProperties"
vli "github.com/caglar10ur/sonos/services/VirtualLineIn"
zgt "github.com/caglar10ur/sonos/services/ZoneGroupTopology"
)
type SonosService interface {
ControlEndpoint() *url.URL
EventEndpoint() *url.URL
ParseEvent([]byte) []interface{}
}
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 ZonePlayerOption func(*ZonePlayer)
func WithClient(c *http.Client) ZonePlayerOption {
return func(z *ZonePlayer) {
z.client = c
}
}
func WithLocation(u *url.URL) ZonePlayerOption {
return func(z *ZonePlayer) {
z.location = u
}
}
func FromEndpoint(endpoint string) (*url.URL, error) {
return url.Parse(fmt.Sprintf("http://%s:1400/xml/device_description.xml", endpoint))
}
type ZonePlayer struct {
Root *Root
client *http.Client
// A URL that can be queried for device capabilities
location *url.URL
*Services
}
type Services struct {
// services
AlarmClock *clk.Service
AudioIn *ain.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
}
// NewZonePlayer returns a new ZonePlayer instance.
func NewZonePlayer(opts ...ZonePlayerOption) (*ZonePlayer, error) {
zp := &ZonePlayer{
Root: &Root{},
client: &http.Client{
Timeout: 10 * time.Second,
},
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated *ZonePlayer as the argument
opt(zp)
}
if zp.location == nil {
return nil, fmt.Errorf("empty location")
}
resp, err := zp.client.Get(zp.location.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
}
zp.Services = &Services{
AlarmClock: clk.NewService(
clk.WithLocation(zp.location),
clk.WithClient(zp.client),
),
AudioIn: ain.NewService(
ain.WithLocation(zp.location),
ain.WithClient(zp.client),
),
AVTransport: avt.NewService(
avt.WithLocation(zp.location),
avt.WithClient(zp.client),
),
ConnectionManager: con.NewService(
con.WithLocation(zp.location),
con.WithClient(zp.client),
),
ContentDirectory: dir.NewService(
dir.WithLocation(zp.location),
dir.WithClient(zp.client),
),
DeviceProperties: dev.NewService(
dev.WithLocation(zp.location),
dev.WithClient(zp.client),
),
GroupManagement: gmn.NewService(
gmn.WithLocation(zp.location),
gmn.WithClient(zp.client),
),
GroupRenderingControl: rcg.NewService(
rcg.WithLocation(zp.location),
rcg.WithClient(zp.client),
),
MusicServices: mus.NewService(
mus.WithLocation(zp.location),
mus.WithClient(zp.client),
),
QPlay: ply.NewService(
ply.WithLocation(zp.location),
ply.WithClient(zp.client),
),
Queue: que.NewService(
que.WithLocation(zp.location),
que.WithClient(zp.client),
),
RenderingControl: ren.NewService(
ren.WithLocation(zp.location),
ren.WithClient(zp.client),
),
SystemProperties: sys.NewService(
sys.WithLocation(zp.location),
sys.WithClient(zp.client),
),
VirtualLineIn: vli.NewService(
vli.WithLocation(zp.location),
vli.WithClient(zp.client),
),
ZoneGroupTopology: zgt.NewService(
zgt.WithLocation(zp.location),
zgt.WithClient(zp.client),
),
}
return zp, nil
}
// Client returns the underlying http client.
func (z *ZonePlayer) Client() *http.Client {
return z.client
}
func (z *ZonePlayer) Location() *url.URL {
return z.location
}
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) SerialNumber() 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
}
func (z *ZonePlayer) GetZoneGroupState() (*ZoneGroupState, error) {
zoneGroupStateResponse, err := z.ZoneGroupTopology.GetZoneGroupState(&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(&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(&ren.SetVolumeArgs{
Channel: "Master",
DesiredVolume: uint16(desiredVolume),
})
return err
}
func (z *ZonePlayer) Play() error {
_, err := z.AVTransport.Play(&avt.PlayArgs{
Speed: "1",
})
return err
}
func (z *ZonePlayer) Stop() error {
_, err := z.AVTransport.Stop(&avt.StopArgs{})
return err
}
func (z *ZonePlayer) SetAVTransportURI(url string) error {
_, err := z.AVTransport.SetAVTransportURI(&avt.SetAVTransportURIArgs{
CurrentURI: url,
})
return err
}
func (zp *ZonePlayer) Event(evt interface{}, fn EventHandlerFunc) {
switch e := evt.(type) {
/*
AlarmClock
type TimeZone string
type TimeServer string
type TimeGeneration uint32
type AlarmListVersion string
type DailyIndexRefreshTime string
type TimeFormat string
type DateFormat string
AudioIn
type AudioInputName string
type Icon string
type LineInConnected bool
type LeftLineInLevel int32
type RightLineInLevel int32
type Playing bool
✅ AVTransport
✅ type LastChange string
ConnectionManage
type SourceProtocolInfo string
type SinkProtocolInfo string
type CurrentConnectionIDs string
ContentDirectory
type SystemUpdateID uint32
type ContainerUpdateIDs string
type ShareIndexInProgress bool
type ShareIndexLastError string
type UserRadioUpdateID string
type SavedQueuesUpdateID string
type ShareListUpdateID string
type RecentlyPlayedUpdateID string
type Browseable bool
type RadioFavoritesUpdateID uint32
type RadioLocationUpdateID uint32
type FavoritesUpdateID string
type FavoritePresetsUpdateID string
DevicePropoerties
type SettingsReplicationState string
type ZoneName string
type Icon string
type Configuration string
type Invisible bool
type IsZoneBridge bool
type AirPlayEnabled bool
type SupportsAudioIn bool
type SupportsAudioClip bool
type IsIdle bool
type MoreInfo string
type ChannelMapSet string
type HTSatChanMapSet string
type HTFreq uint32
type HTBondedZoneCommitState uint32
type Orientation int32
type LastChangedPlayState string
type RoomCalibrationState int32
type AvailableRoomCalibration string
type TVConfigurationError bool
type HdmiCecAvailable bool
type WirelessMode uint32
type WirelessLeafOnly bool
type HasConfiguredSSID bool
type ChannelFreq uint32
type BehindWifiExtender uint32
type WifiEnabled bool
type EthLink bool
type ConfigMode string
type SecureRegState uint32
type VoiceConfigState uint32
type MicEnabled uint32
GroupManagement
type GroupCoordinatorIsLocal bool
type LocalGroupUUID string
type VirtualLineInGroupID string
type ResetVolumeAfter bool
type VolumeAVTransportURI string
GroupRenderingControl
type GroupMute bool
type GroupVolume uint16
type GroupVolumeChangeable bool
MusicServices
type ServiceListVersion string
✅ Queue
✅ type LastChange string
✅ RenderingControl
✅ type LastChange string
SystemProperties
type CustomerID string
type UpdateID uint32
type UpdateIDX uint32
type VoiceUpdateID uint32
type ThirdPartyHash string
VirtualLinein
type CurrentTrackMetaData string
ZoneGroupTopology
✅ type AvailableSoftwareUpdate string
✅ type ZoneGroupState string
type ThirdPartyMediaServersX string
type AlarmRunSequence string
type MuseHouseholdId string
type ZoneGroupName string
type ZoneGroupID string
type ZonePlayerUUIDsInGroup string
type AreasUpdateID string
type SourceAreasUpdateID string
type NetsettingsUpdateID string
*/
case avt.LastChange:
var levt AVTransportLastChange
err := xml.Unmarshal([]byte(e), &levt)
if err != nil {
fmt.Printf("Unmarshal failure: %s", err)
return
}
fn(levt)
case ren.LastChange:
var levt RenderingControlLastChange
err := xml.Unmarshal([]byte(e), &levt)
if err != nil {
fmt.Printf("Unmarshal failure: %s", err)
return
}
fn(levt)
case que.LastChange:
var levt QueueLastChange
err := xml.Unmarshal([]byte(e), &levt)
if err != nil {
fmt.Printf("Unmarshal failure: %s", err)
return
}
fn(levt)
case zgt.AvailableSoftwareUpdate:
var levt ZoneGroupTopologyAvailableSoftwareUpdate
err := xml.Unmarshal([]byte(e), &levt)
if err != nil {
fmt.Printf("Unmarshal failure: %s", err)
return
}
fn(levt)
case zgt.ZoneGroupState:
var levt ZoneGroupTopologyZoneGroupState
err := xml.Unmarshal([]byte(e), &levt)
if err != nil {
fmt.Printf("Unmarshal failure: %s", err)
return
}
fn(levt)
default:
fmt.Printf("Unhandeld event %T: %s\n", e, e)
}
}