Browse Source

Add handlers /volume and /volume/relative

master
Remi Reuvekamp 5 years ago
parent
commit
b560f5ba64
  1. 50
      handlers.go
  2. 2
      main.go

50
handlers.go

@ -22,6 +22,7 @@ var (
ErrInvalidTime = errors.New("invalid time in seconds (time) given or time (time) not specified")
ErrInvalidIndex = errors.New("invalid index (index) given or index (index) not specified")
ErrInvalidAmount = errors.New("invalid amount (amount) given or amount (amount) not specified")
ErrInvalidVolume = errors.New("invalid volume (volume) given or volume (volume) not specified")
ErrLockedModeEnabled = errors.New("locked mode is enabled and locked not phrase (locked) specified")
ErrIncorrectLockedPhrase = errors.New("locked mode is enabled and incorrect locked phrase (locked) specified")
@ -324,6 +325,55 @@ func handlePlayRelative(e *env, w http.ResponseWriter, r *http.Request) (errs ha
return
}
// handleVolume handler requets MPD to set the volume to the given volume.
func handleVolume(e *env, w http.ResponseWriter, r *http.Request) (errs handlerErrors) {
reDialMPD(e)
vol, err := strconv.Atoi(r.FormValue("volume"))
if err != nil || vol > 100 || vol < 0 {
errs.err = ErrInvalidVolume
return
}
err = e.mpdC.Volume(byte(vol), false)
if err != nil {
errs.err = mpdError{err}
}
return
}
// handleVolumeRelative handler requets MPD to set the volume to the given relative
// volume.
func handleVolumeRelative(e *env, w http.ResponseWriter, r *http.Request) (errs handlerErrors) {
reDialMPD(e)
vol, err := strconv.Atoi(r.FormValue("volume"))
if err != nil || vol > 100 || vol < -100 {
errs.err = ErrInvalidVolume
return
}
status, err := e.mpdC.Status()
if err != nil {
errs.err = mpdError{err}
return
}
// Set new volume to zero if it goes below (MPD will start at 100 again
// otherwise).
if int(status.Volume)+vol < 0 {
vol = int(status.Volume) * -1
}
err = e.mpdC.Volume(byte(vol), true)
if err != nil {
errs.err = mpdError{err}
}
return
}
// handleSpeak handler uses speakd to speak information on the current item in the queue.
func handleSpeak(e *env, w http.ResponseWriter, r *http.Request) (errs handlerErrors) {
reDialMPD(e)

2
main.go

@ -82,6 +82,8 @@ func main() {
mux.Handle("/seek/relative", handle(e, checkLocked, handleSeekRelative))
mux.Handle("/play", handle(e, checkLocked, handlePlay))
mux.Handle("/play/relative", handle(e, checkLocked, handlePlayRelative))
mux.Handle("/volume", handle(e, checkLocked, handleVolume))
mux.Handle("/volume/relative", handle(e, checkLocked, handleVolumeRelative))
mux.Handle("/speak", handle(e, checkLocked, handleSpeak))
mux.Handle("/speakc-callback", e.speakc.CallbackHandler())

Loading…
Cancel
Save