2021-03-30 19:59:26 +02:00
|
|
|
//nolint:wrapcheck
|
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-31 20:14:05 +02:00
|
|
|
"net/http"
|
2021-03-30 19:59:26 +02:00
|
|
|
|
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
2021-03-31 20:14:05 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-03-30 19:59:26 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"go.xsfx.dev/schnutibox/internal/config"
|
2021-03-31 20:14:05 +02:00
|
|
|
"go.xsfx.dev/schnutibox/internal/metrics"
|
2021-03-30 19:59:26 +02:00
|
|
|
"go.xsfx.dev/schnutibox/pkg/rfid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mpc struct {
|
|
|
|
conn *mpd.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMpc(conn *mpd.Client) *mpc {
|
|
|
|
return &mpc{conn}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mpc) stop(logger zerolog.Logger) error {
|
|
|
|
logger.Info().Msg("trying to stop playback")
|
|
|
|
|
|
|
|
return m.conn.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mpc) clear(logger zerolog.Logger) error {
|
|
|
|
logger.Info().Msg("trying to clear playlist")
|
|
|
|
|
|
|
|
return m.conn.Clear()
|
|
|
|
}
|
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
func (m *mpc) play(logger zerolog.Logger, rfid string, name string, uris []string) error {
|
2021-03-30 19:59:26 +02:00
|
|
|
logger.Info().Msg("trying to add tracks")
|
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
// Metric labels.
|
|
|
|
mLabels := []string{rfid, name}
|
|
|
|
|
2021-03-30 19:59:26 +02:00
|
|
|
// Stop playing track.
|
|
|
|
if err := m.stop(logger); err != nil {
|
2021-03-31 20:14:05 +02:00
|
|
|
metrics.BoxErrors.Inc()
|
|
|
|
|
2021-03-30 19:59:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear playlist.
|
|
|
|
if err := m.clear(logger); err != nil {
|
2021-03-31 20:14:05 +02:00
|
|
|
metrics.BoxErrors.Inc()
|
|
|
|
|
2021-03-30 19:59:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding every single uri to playlist
|
|
|
|
for _, i := range uris {
|
|
|
|
logger.Debug().Str("uri", i).Msg("add track")
|
|
|
|
|
|
|
|
if err := m.conn.Add(i); err != nil {
|
2021-03-31 20:14:05 +02:00
|
|
|
metrics.BoxErrors.Inc()
|
|
|
|
|
2021-03-30 19:59:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
metrics.TracksPlayed.WithLabelValues(mLabels...).Inc()
|
|
|
|
|
2021-03-30 19:59:26 +02:00
|
|
|
return m.conn.Play(-1)
|
|
|
|
}
|
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
//nolint:funlen
|
2021-03-30 19:59:26 +02:00
|
|
|
func Run(cmd *cobra.Command, args []string) {
|
|
|
|
log.Info().Msg("starting the RFID reader")
|
|
|
|
|
|
|
|
idChan := make(chan string)
|
|
|
|
r := rfid.NewRFID(config.Cfg, idChan)
|
|
|
|
|
|
|
|
if err := r.Run(); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("could not start RFID reader")
|
|
|
|
}
|
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
go func() {
|
|
|
|
var id string
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
for {
|
|
|
|
// Wating for a scanned tag.
|
|
|
|
id = <-idChan
|
|
|
|
logger := log.With().Str("id", id).Logger()
|
|
|
|
logger.Info().Msg("received id")
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
// Create MPD connection on every received event.
|
|
|
|
c, err := mpd.Dial("tcp", fmt.Sprintf("%s:%d", config.Cfg.MPD.Hostname, config.Cfg.MPD.Port))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error().Err(err).Msg("could not connect to MPD server")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
m := newMpc(c)
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
// Check of stop tag was detected.
|
|
|
|
if id == config.Cfg.Meta.Stop {
|
|
|
|
logger.Info().Msg("stopping")
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
if err := m.stop(logger); err != nil {
|
|
|
|
logger.Error().Err(err).Msg("could not stop")
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
2021-03-30 19:59:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
// Check if there is a track for the ID.
|
|
|
|
tracks, ok := config.Cfg.Tracks[id]
|
|
|
|
if !ok {
|
|
|
|
logger.Error().Msg("could not find track for ID")
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
// Try to play track.
|
|
|
|
if err := m.play(logger, id, tracks.Name, tracks.URIS); err != nil {
|
|
|
|
logger.Error().Err(err).Msg("could not play track")
|
|
|
|
}
|
2021-03-30 19:59:26 +02:00
|
|
|
}
|
2021-03-31 20:14:05 +02:00
|
|
|
}()
|
2021-03-30 19:59:26 +02:00
|
|
|
|
2021-03-31 20:14:05 +02:00
|
|
|
l := fmt.Sprintf("%s:%d", config.Cfg.Box.Hostname, config.Cfg.Box.Port)
|
|
|
|
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
|
|
|
log.Info().Msgf("serving on %s...", l)
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(l, nil); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("")
|
2021-03-30 19:59:26 +02:00
|
|
|
}
|
|
|
|
}
|