diff --git a/README.md b/README.md index e5e7c2b..1d0b1eb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1 @@ # schnutibox - -## TODO - -- raspi configuration script diff --git a/assets/web/files/schnutibox.js b/assets/web/files/schnutibox.js index 91e81d8..07aa5a8 100644 --- a/assets/web/files/schnutibox.js +++ b/assets/web/files/schnutibox.js @@ -1,6 +1,6 @@ if(typeof(EventSource) !== "undefined") { - var source = new EventSource("/log"); - source.onmessage = function(event) { + var logSource = new EventSource("/log"); + logSource.onmessage = function(event) { var j = JSON.parse(event.data); /* eslint-disable no-prototype-builtins */ if (j.hasOwnProperty("message")) { @@ -11,6 +11,15 @@ if(typeof(EventSource) !== "undefined") { document.getElementById("log").innerHTML = "Sorry, your browser does not support server-sent events..."; } +if(typeof(EventSource) !== "undefined") { + var currentsongSource = new EventSource("/currentsong"); + currentsongSource.onmessage = function(event){ + document.getElementById("currentsong").innerHTML = event.data + }; +} else { + document.getElementById("currentsong").innerHTML = "Sorry, your browser does not support server-sent events..."; +}; + function handleSubmit(event, url) { event.preventDefault() diff --git a/assets/web/templates/index.html.tmpl b/assets/web/templates/index.html.tmpl index cb4ce50..dd02930 100644 --- a/assets/web/templates/index.html.tmpl +++ b/assets/web/templates/index.html.tmpl @@ -11,6 +11,9 @@

schnutibox

+

Currently playing

+
+

Timer

Takes only seconds. Example: 600s
diff --git a/pkg/currentsong/currentsong.go b/pkg/currentsong/currentsong.go new file mode 100644 index 0000000..f6119a5 --- /dev/null +++ b/pkg/currentsong/currentsong.go @@ -0,0 +1,56 @@ +package currentsong + +import ( + "fmt" + "net/http" + + "go.xsfx.dev/logginghandler" +) + +var recvs = make(map[chan string]struct{}) // nolint:gochecknoglobals + +// Write writes current track to the receivers. +func Write(track string) { + for k := range recvs { + k <- track + } +} + +func Handler(w http.ResponseWriter, r *http.Request) { + logger := logginghandler.Logger(r) + logger.Debug().Msg("got a new receiver") + + flusher, ok := w.(http.Flusher) + if !ok { + logger.Error().Msg("streaming unsupported") + http.Error(w, "streaming unsupported", http.StatusInternalServerError) + + return + } + + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Connection", "keep-alive") + // TODO: has to be something else! + w.Header().Set("Access-Control-Allow-Origin", "*") + + cChan := make(chan string) + + recvs[cChan] = struct{}{} + + for { + select { + case e := <-cChan: + // Send event to client. + fmt.Fprintf(w, "data: %s\n\n", e) + + // Send it right now and not buffering it. + flusher.Flush() + case <-r.Context().Done(): + close(cChan) + delete(recvs, cChan) + + return + } + } +} diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index 6bf0091..c6f6339 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -1,10 +1,12 @@ package watcher import ( + "fmt" "time" "github.com/rs/zerolog/log" "go.xsfx.dev/schnutibox/internal/metrics" + "go.xsfx.dev/schnutibox/pkg/currentsong" "go.xsfx.dev/schnutibox/pkg/mpc" "go.xsfx.dev/schnutibox/pkg/timer" ) @@ -51,6 +53,20 @@ func Run() { return } + currentSong, err := m.CurrentSong() + if err != nil { + log.Error().Err(err).Msg("could not get current song") + metrics.BoxErrors.Inc() + + return + } + + if len(currentSong) != 0 { + currentsong.Write(fmt.Sprintf("%s - %s", currentSong["Artist"], currentSong["Track"])) + } else { + currentsong.Write("") + } + // Sets the metrics. metrics.Set(uris, s["state"]) }() diff --git a/pkg/web/web.go b/pkg/web/web.go index edd5aa9..cbaeb82 100644 --- a/pkg/web/web.go +++ b/pkg/web/web.go @@ -17,6 +17,7 @@ import ( assets "go.xsfx.dev/schnutibox/assets/web" "go.xsfx.dev/schnutibox/internal/config" api "go.xsfx.dev/schnutibox/pkg/api/v1" + "go.xsfx.dev/schnutibox/pkg/currentsong" "go.xsfx.dev/schnutibox/pkg/sselog" "go.xsfx.dev/schnutibox/pkg/timer" "golang.org/x/net/http2" @@ -96,6 +97,8 @@ func (t TimerServer) Get(ctx context.Context, req *api.TimerEmpty) (*api.Timer, return timer.T.Req, nil } +func currentSong(w http.ResponseWriter, r *http.Request) {} + func gw(s *grpc.Server, conn string) *runtime.ServeMux { ctx := context.Background() gopts := []grpc.DialOption{grpc.WithInsecure()} @@ -133,6 +136,7 @@ func Run(command *cobra.Command, args []string) { mux.Handle("/", http.HandlerFunc(root)) mux.Handle("/log", http.HandlerFunc(sselog.LogHandler)) + mux.Handle("/currentsong", http.HandlerFunc(currentsong.Handler)) mux.Handle( "/static/",