schnutibox/pkg/run/run.go

76 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-03-30 19:59:26 +02:00
package run
import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
2021-08-05 09:43:35 +02:00
"github.com/spf13/viper"
2021-03-30 19:59:26 +02:00
"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-08-05 09:43:35 +02:00
"go.xsfx.dev/schnutibox/pkg/watcher"
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 {
2021-08-05 09:43:35 +02:00
if !viper.GetBool("reader.ignore") {
log.Fatal().Err(err).Msg("could not start RFID reader")
}
log.Warn().Err(err).Msg("could not start RFID reader. ignoring...")
2021-03-30 19:59:26 +02:00
}
2021-05-31 14:44:26 +02:00
// Stating watcher.
2021-08-05 09:43:35 +02:00
watcher.Run()
2021-05-31 14:44:26 +02:00
2021-08-05 09:43:35 +02:00
// nolint:nestif
if !viper.GetBool("reader.ignore") {
go func() {
var id string
2021-03-30 19:59:26 +02:00
2021-08-05 09:43:35 +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-08-05 09:43:35 +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-08-05 09:43:35 +02:00
if err := mpc.Stop(logger); err != nil {
logger.Error().Err(err).Msg("could not stop")
}
2021-03-31 20:14:05 +02:00
2021-08-05 09:43:35 +02:00
if err := mpc.Clear(logger); err != nil {
logger.Error().Err(err).Msg("could not clear")
}
2021-04-14 11:40:15 +02:00
2021-08-05 09:43:35 +02:00
continue
}
2021-03-30 19:59:26 +02:00
2021-08-05 09:43:35 +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-08-05 09:43:35 +02:00
continue
}
2021-03-30 19:59:26 +02:00
2021-08-05 09:43:35 +02:00
// Try to play track.
if err := mpc.Play(logger, id, tracks.Name, tracks.Uris); err != nil {
logger.Error().Err(err).Msg("could not play track")
}
2021-03-31 20:14:05 +02:00
}
2021-08-05 09:43:35 +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
}