schnutibox/pkg/run/run.go

135 lines
2.7 KiB
Go
Raw Normal View History

2021-03-30 19:59:26 +02:00
//nolint:wrapcheck
package run
import (
"fmt"
"github.com/fhs/gompd/v2/mpd"
"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"
2021-05-03 14:51:53 +02:00
"go.xsfx.dev/schnutibox/pkg/web"
2021-03-30 19:59:26 +02:00
)
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)
}
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")
}
2021-04-14 11:40:15 +02:00
if err := m.clear(logger); err != nil {
logger.Error().Err(err).Msg("could not clear")
}
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
// 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-05-03 14:51:53 +02:00
// Running web interface. Blocking.
web.Run(cmd, args)
2021-03-30 19:59:26 +02:00
}