schnutibox/pkg/run/run.go

67 lines
1.4 KiB
Go
Raw Normal View History

2021-03-30 19:59:26 +02:00
package run
import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"go.xsfx.dev/schnutibox/internal/config"
2021-05-31 14:44:26 +02:00
"go.xsfx.dev/schnutibox/pkg/mpc"
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
)
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-05-31 14:44:26 +02:00
// Stating watcher.
mpc.Watcher()
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
// 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-05-31 14:44:26 +02:00
if err := mpc.Stop(logger); err != nil {
2021-03-31 20:14:05 +02:00
logger.Error().Err(err).Msg("could not stop")
}
2021-05-31 14:44:26 +02:00
if err := mpc.Clear(logger); err != nil {
2021-04-14 11:40:15 +02:00
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.
2021-05-31 14:44:26 +02:00
if err := mpc.Play(logger, id, tracks.Name, tracks.Uris); err != nil {
2021-03-31 20:14:05 +02:00
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
}