From 860ad33a70c60b8c6f04e311fe2a64e2a3adb156 Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Tue, 19 Mar 2019 20:45:23 +0100 Subject: [PATCH] first --- .gitignore | 1 + app/index.html | 4 ++++ app/js/app.js | 10 +++++++++ app/js/mpd.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ app/js/parser.js | 11 ++++++++++ app/js/status.js | 13 ++++++++++++ index.js | 10 +++++++++ package.json | 16 ++++++++++++++ 8 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 app/index.html create mode 100644 app/js/app.js create mode 100644 app/js/mpd.js create mode 100644 app/js/parser.js create mode 100644 app/js/status.js create mode 100644 index.js create mode 100644 package.json 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" +}