forked from llun/soundtouch-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnow_playing.go
172 lines (151 loc) · 4.12 KB
/
now_playing.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
package soundtouch
import (
"encoding/xml"
)
// PlayStatus is a string type
type PlayStatus string
// All Playing states of soundtouch speaker
const (
PlayState = "PLAY_STATE"
PauseState = "PAUSE_STATE"
BufferingState = "BUFFERING_STATE"
InvalidPlayStatus = "INVALID_PLAY_STATUS"
StopState = "STOP"
Standby = "STANDBY"
)
// ALLSTATES contains all soundtouch state constants
var ALLSTATES = []string{
PlayState,
PauseState,
BufferingState,
InvalidPlayStatus,
StopState,
Standby,
}
// Source is a string type
type Source string
// All Sources of a soundtouch speaker
const (
Slave = "SLAVE_SOURCE"
InternetRadio = "INTERNET_RADIO"
LocalInternetRadio = "LOCAL_INTERNET_RADIO"
Pandora = "PANDORA"
TuneIn = "TUNEIN"
Airplay = "AIRPLAY"
StoredMusic = "STORED_MUSIC"
Aux = "AUX"
Bluetooth = "BLUETOOTH"
Product = "PRODUCT"
OffSource = "OFF_SOURCE"
CurratedRadio = "CURRATED_RADIO"
UPDATE = "UPDATE"
Deezer = "DEEZER"
Spotify = "SPOTIFY"
Alexa = "ALEXA"
IHeart = "IHEART"
)
// ALLSOURCES contains all soundtouch sources
var ALLSOURCES = []string{
Slave,
InternetRadio,
LocalInternetRadio,
Pandora,
TuneIn,
Airplay,
StoredMusic,
Aux,
Bluetooth,
Product,
OffSource,
CurratedRadio,
UPDATE,
Deezer,
Spotify,
IHeart,
}
// All StreamTypes
const (
RadioStreaming = "RADIO_STREAMING"
TrackOnDemand = "TRACK_ONDEMAND"
RadioTracks = "RADIO_TRACKS" // like Alexa
)
// ALLSTREAMS contains all soundtouch streamtypes
var ALLSTREAMS = []string{
RadioStreaming, TrackOnDemand,
}
// NowPlaying defines the now_playing message to/from soundtouch system
type NowPlaying struct {
PlayStatus PlayStatus `xml:"playStatus" json:",omitempty"`
Source string `xml:"source,attr" json:",omitempty"`
SourceAccount string `xml:"sourceAccount,attr" json:",omitempty"`
DeviceID string `xml:"deviceID,attr" json:",omitempty"`
Content ContentItem `xml:"ContentItem" json:",omitempty"`
Track string `xml:"track" json:",omitempty"`
Artist string `xml:"artist" json:",omitempty"`
Album string `xml:"album" json:",omitempty"`
TrackID string `xml:"trackID" json:",omitempty"`
Art string `xml:"art" json:",omitempty"`
StreamType string `xml:"streamType" json:",omitempty"`
Raw []byte `json:"-"`
}
// NowPlaying sends the now_playing command to the soundtouch system
func (s *Speaker) NowPlaying() (NowPlaying, error) {
body, err := s.GetData("now_playing")
if err != nil {
return NowPlaying{}, err
}
nowPlaying := NowPlaying{
Raw: body,
}
err = xml.Unmarshal(body, &nowPlaying)
if err != nil {
return nowPlaying, err
}
return nowPlaying, nil
}
// IsAlive returns true in case the soundtouch system is in PlayState
func (s *Speaker) IsAlive() bool {
np, err := s.NowPlaying()
if err != nil {
return false
}
var isAlive bool
isAlive = np.PlayStatus != Standby
if isAlive {
isAlive = np.PlayStatus == PlayState
}
return isAlive
}
// IsPoweredOn returns true in case the soundtouch system is not in standby but powered
func (s *Speaker) IsPoweredOn() bool {
np, err := s.NowPlaying()
if err != nil {
return false
}
return np.Source != Standby
}
// PowerOn switches the soundtouch system on. True on success. Returns false in case the system was already powered
func (s *Speaker) PowerOn() bool {
if !s.IsPoweredOn() {
s.PressKey(POWER)
return true
}
return false
}
// PowerOnWithVolume switches the soundtouch system on and set's a specific volume. True on success. Returns false in case the system was already powered with no volume set.
func (s *Speaker) PowerOnWithVolume(vol int) bool {
if !s.IsPoweredOn() {
s.PressKey(POWER)
s.SetVolume(vol)
return true
}
return false
}
// PowerOff powers off the soundtouch systems. True on success. Returns false in case it was already powered off.
func (s *Speaker) PowerOff() bool {
if s.IsPoweredOn() {
s.PressKey(POWER)
return true
}
return false
}