diff --git a/NOTES.md b/NOTES.md index 793ee3e..559fd24 100644 --- a/NOTES.md +++ b/NOTES.md @@ -29,3 +29,11 @@ - [x] track progress (seek) - [ ] track name + +# foo + +- client: connect websocket +- server: on_connect: send full state +- server: subscribe to changes +- server: on_change: send to client + diff --git a/server.go b/server.go index ae7e321..c9dff82 100644 --- a/server.go +++ b/server.go @@ -134,6 +134,10 @@ func wsServe(c echo.Context) error { if err != nil { c.Logger().Error(err) } + queue, err := mpdConn.PlaylistInfo(-1, -1) + if err != nil { + c.Logger().Error(err) + } jsonStatus, err := json.Marshal(status) if err != nil { c.Logger().Error(err) @@ -142,7 +146,11 @@ func wsServe(c echo.Context) error { if err != nil { c.Logger().Error(err) } - err = websocket.Message.Send(ws, fmt.Sprintf("{\"mpd_status\":%s,\"mpd_current_song\":%s}", string(jsonStatus), string(jsonCurrentSong))) + jsonQueue, err := json.Marshal(queue) + if err != nil { + c.Logger().Error(err) + } + err = websocket.Message.Send(ws, fmt.Sprintf("{\"mpd_status\":%s,\"mpd_current_song\":%s,\"mpd_queue\":%s}", string(jsonStatus), string(jsonCurrentSong), string(jsonQueue))) if err != nil { c.Logger().Error(err) } diff --git a/static/controls.js b/static/controls.js index 67b2610..9748f64 100644 --- a/static/controls.js +++ b/static/controls.js @@ -190,6 +190,54 @@ socket.addEventListener("message", (e) => { } } + // update queue + if ("mpd_queue" in msg && msg.mpd_queue != null) { + const tbody = document.createElement("tbody"); + msg.mpd_queue.forEach(elem => { + const tr = document.createElement("tr"); + const pos = document.createElement("td"); + pos.innerText = elem.Pos; + const artist = document.createElement("td"); + if ("Artist" in elem) { + artist.innerText = elem.Artist; + } + const track = document.createElement("td"); + if ("Title" in elem) { + track.innerText = elem.Title; + } else { + track.innerText = elem.file; + } + const album = document.createElement("td"); + // album.innerText = ""; + const length = document.createElement("td"); + const duration_hours = Math.floor(elem.duration / 3600); + const duration_minutes = Math.floor((elem.duration - duration_hours * 3600) / 60); + const duration_seconds = Math.floor(elem.duration - duration_hours * 3600 - duration_minutes * 60); + length.innerText = `${duration_hours}:${duration_minutes.toString().padStart(2, '0')}:${duration_seconds.toString().padStart(2, '0')}`; + const actions = document.createElement("td"); + const del = document.createElement("button"); + del.innerHTML = "🗑️"; + del.addEventListener("click", e => { + fetch(`${API_URL}/queue_del/${elem.Pos}`).then(r => { + console.log(r.text()); + }) + }); + actions.appendChild(del); + tr.appendChild(pos); + tr.appendChild(artist); + tr.appendChild(track); + tr.appendChild(album); + tr.appendChild(length); + tr.appendChild(actions); + tbody.appendChild(tr); + }); + const currentQueue = document.querySelector("#queue tbody") + if (currentQueue.innerHTML !== tbody.innerHTML) { + console.log("Updating queue") + currentQueue.outerHTML = tbody.outerHTML; + } + } + if ("mpd_error" in msg) { console.error(`MPD Error: ${msg.mpd_error}`) } diff --git a/static/index.html b/static/index.html index de40c89..2387fb9 100644 --- a/static/index.html +++ b/static/index.html @@ -14,14 +14,14 @@ - +
Now playing:
@@ -57,21 +57,23 @@Pos | -Artists | -Track | -Album | -Length | -Actions | +Pos | +Artists | +Track | +Album | +Length | +Actions |
---|