2023-12-10 21:33:54 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MPD API calls
|
|
|
|
|
2023-12-29 16:55:06 +01:00
|
|
|
func updateDb(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-29 16:55:06 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
jobId, err := conn.Update("")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-29 16:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, strconv.Itoa(jobId))
|
|
|
|
}
|
|
|
|
|
2023-12-10 21:33:54 +01:00
|
|
|
func previousTrack(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Previous()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextTrack(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Next()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopPlayback(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Stop()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func resumePlayback(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
status, err := conn.Status()
|
2023-12-10 21:33:54 +01:00
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
if status["state"] == "stop" {
|
|
|
|
err := conn.Play(-1)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = conn.Pause(false)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func pausePlayback(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Pause(true)
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func seek(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
seconds, err := strconv.Atoi(c.Param("seconds"))
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if seconds < 0 {
|
|
|
|
return c.String(http.StatusBadRequest, "seconds must be positive integer")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.SeekCur(time.Duration(seconds)*time.Second, false)
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleRepeat(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
status, err := conn.Status()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
if status["repeat"] == "1" {
|
|
|
|
err = conn.Repeat(false)
|
|
|
|
} else {
|
|
|
|
err = conn.Repeat(true)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleRandom(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
status, err := conn.Status()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
2023-12-30 15:02:13 +01:00
|
|
|
if status["random"] == "1" {
|
2023-12-10 21:33:54 +01:00
|
|
|
err = conn.Random(false)
|
|
|
|
} else {
|
|
|
|
err = conn.Random(true)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setVolume(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
level, err := strconv.Atoi(c.Param("level"))
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if level > 100 || level < 0 {
|
|
|
|
return c.String(http.StatusBadRequest, "Volume must be between 0 and 100")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.SetVolume(level)
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
}
|