commit 860ad33a70c60b8c6f04e311fe2a64e2a3adb156 Author: Ondrej Zara Date: Tue Mar 19 20:45:23 2019 +0100 first diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/app/index.html b/app/index.html new file mode 100644 index 0000000..6fe5235 --- /dev/null +++ b/app/index.html @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/js/app.js b/app/js/app.js new file mode 100644 index 0000000..f0db80d --- /dev/null +++ b/app/js/app.js @@ -0,0 +1,10 @@ +import * as mpd from "./mpd.js"; +import * as status from "./status.js"; + +async function init() { + await mpd.init(); + status.init(); + window.mpd = mpd; +} + +init(); diff --git a/app/js/mpd.js b/app/js/mpd.js new file mode 100644 index 0000000..c17777a --- /dev/null +++ b/app/js/mpd.js @@ -0,0 +1,55 @@ +import * as parser from "./parser.js"; + +let ws; +let commandQueue = []; +let pendingResolve; + +function onMessage(e) { + if (pendingResolve) { + pendingResolve(JSON.parse(e.data)); // FIXME tady test na ACK + pendingResolve = null; + } + processQueue(); +} + +function onError(e) { + console.error(e); + ws = null; // fixme +} + +function onClose(e) { + console.warn(e); + ws = null; // fixme +} + +function processQueue() { + if (pendingResolve || commandQueue.length == 0) { return; } + let cmd = commandQueue.shift(); + if (cmd instanceof Array) { cmd = ["command_list_begin", ...cmd, "command_list_end"].join("\n"); } + ws.send(cmd); +} + +export async function command(cmd) { + commandQueue.push(cmd); + processQueue(); + + return new Promise(resolve => pendingResolve = resolve); +} + +export async function getStatus() { + let lines = await command(["status", "currentsong"]); + return parser.linesToStruct(lines); +} + +export async function init() { + return new Promise((resolve, reject) => { + try { + ws = new WebSocket("ws://localhost:8080?server=0:6600"); + } catch (e) { reject(e); } + pendingResolve = resolve; + + ws.addEventListener("error", onError); + ws.addEventListener("message", onMessage); + ws.addEventListener("close", onClose); + }); +} diff --git a/app/js/parser.js b/app/js/parser.js new file mode 100644 index 0000000..35e7660 --- /dev/null +++ b/app/js/parser.js @@ -0,0 +1,11 @@ +export function linesToStruct(lines) { + lines.pop(); // "OK" + let result = {}; + lines.forEach(line => { + let cindex = line.indexOf(":"); + if (cindex == -1) { throw new Error(`Malformed line "${line}"`); } + result[line.substring(0, cindex)] = line.substring(cindex+2); + }); + return result; +} + diff --git a/app/js/status.js b/app/js/status.js new file mode 100644 index 0000000..4342e9e --- /dev/null +++ b/app/js/status.js @@ -0,0 +1,13 @@ +import * as mpd from "./mpd.js"; + +const DELAY = 2000; + +async function tick() { + let data = await mpd.getStatus(); + console.log(data); + setTimeout(tick, DELAY); +} + +export function init() { + tick(); +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..4c06723 --- /dev/null +++ b/index.js @@ -0,0 +1,10 @@ +const static = require("node-static"); +const app = new static.Server("./app"); +const port = 8080; + +let httpServer = require("http").createServer((request, response) => { + request.on("end", () => app.serve(request, response)).resume(); +}); +httpServer.listen(port); + +require("ws2mpd").ws2mpd(httpServer, `http://localhost:${port}`); diff --git a/package.json b/package.json new file mode 100644 index 0000000..6fbb639 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "0", + "version": "1.0.0", + "description": "", + "main": "index.js", + "dependencies": { + "node-static": "^0.7.11", + "ws2mpd": "^1.0.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +}