storing mpd playlist tracks for metrics and not the ones from the config

This commit is contained in:
Marvin Steadfast 2021-05-31 08:48:28 +02:00
parent b7418991e1
commit 3de8271fb1
3 changed files with 48 additions and 31 deletions

View File

@ -1,4 +1,4 @@
//nolint:gochecknoglobals,golint,stylecheck //nolint:gochecknoglobals
package web package web
import ( import (
@ -12,14 +12,17 @@ import (
//go:embed files //go:embed files
var files embed.FS var files embed.FS
// Files is the sub directed http.FileSystem for files.
var Files = sub(files, "files") var Files = sub(files, "files")
// Templates stores the templates.
//go:embed templates //go:embed templates
var Templates embed.FS var Templates embed.FS
//go:embed swagger-ui //go:embed swagger-ui
var swaggerUI embed.FS var swaggerUI embed.FS
// SwaggerUI is the sub directed http.FileSystem for the swagger-ui.
var SwaggerUI = sub(swaggerUI, "swagger-ui") var SwaggerUI = sub(swaggerUI, "swagger-ui")
func sub(f embed.FS, dir string) http.FileSystem { func sub(f embed.FS, dir string) http.FileSystem {

View File

@ -65,11 +65,43 @@ func (m *mpc) play(logger zerolog.Logger, rfid string, name string, uris []strin
} }
} }
metrics.NewPlay(rfid, name, uris) // Getting playlist uris from MPD server.
// This is needed to identify the right metric to use.
mpdURIS, err := m.playlistURIS()
if err != nil {
metrics.BoxErrors.Inc()
return err
}
metrics.NewPlay(rfid, name, mpdURIS)
return m.conn.Play(-1) return m.conn.Play(-1)
} }
// playlistURIS extracts uris from MPD playlist.
func (m *mpc) playlistURIS() ([]string, error) {
// Check if we can connect to MPD server.
if err := m.conn.Ping(); err != nil {
return nil, fmt.Errorf("could not ping MPD server: %w", err)
}
attrs, err := m.conn.PlaylistInfo(-1, -1)
if err != nil {
return nil, fmt.Errorf("could not get playlist: %w", err)
}
// Stores the tracklist it got from the MPD server.
uris := []string{}
// Builds uri list.
for _, a := range attrs {
uris = append(uris, a["file"])
}
return uris, nil
}
func (m *mpc) watch() { func (m *mpc) watch() {
log.Debug().Msg("starting watch") log.Debug().Msg("starting watch")
@ -79,31 +111,14 @@ func (m *mpc) watch() {
for { for {
<-ticker.C <-ticker.C
// Check if we can connect to MPD server. uris, err := m.playlistURIS()
if err := m.conn.Ping(); err != nil {
log.Error().Err(err).Msg("could not ping MPD server")
metrics.BoxErrors.Inc()
continue
}
// Getting playlist info.
attrs, err := m.conn.PlaylistInfo(-1, -1)
if err != nil { if err != nil {
log.Error().Err(err).Msg("could not get playlist info") log.Error().Err(err).Msg("could not get playlist uris")
metrics.BoxErrors.Inc() metrics.BoxErrors.Inc()
continue continue
} }
// Stores the tracklist it got from the MPD server.
uris := []string{}
// Builds uri list.
for _, a := range attrs {
uris = append(uris, a["file"])
}
// Gettings MPD state. // Gettings MPD state.
s, err := m.conn.Status() s, err := m.conn.Status()
if err != nil { if err != nil {
@ -129,16 +144,6 @@ func Run(cmd *cobra.Command, args []string) {
log.Fatal().Err(err).Msg("could not start RFID reader") log.Fatal().Err(err).Msg("could not start RFID reader")
} }
// 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 {
log.Fatal().Err(err).Msg("could not connect to MPD server")
}
m := newMpc(c)
m.watch()
go func() { go func() {
var id string var id string
@ -148,6 +153,14 @@ func Run(cmd *cobra.Command, args []string) {
logger := log.With().Str("id", id).Logger() logger := log.With().Str("id", id).Logger()
logger.Info().Msg("received id") logger.Info().Msg("received id")
// 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 {
log.Fatal().Err(err).Msg("could not connect to MPD server")
}
m := newMpc(c)
// Check of stop tag was detected. // Check of stop tag was detected.
if id == config.Cfg.Meta.Stop { if id == config.Cfg.Meta.Stop {
logger.Info().Msg("stopping") logger.Info().Msg("stopping")

View File

@ -10,6 +10,7 @@ import (
"go.xsfx.dev/logginghandler" "go.xsfx.dev/logginghandler"
) )
// Log is the global sse logger struct.
var Log *SSELog var Log *SSELog
type SSELog struct { type SSELog struct {