This commit is contained in:
Ondrej Zara 2019-03-19 20:45:23 +01:00
commit 860ad33a70
8 changed files with 120 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

4
app/index.html Normal file
View file

@ -0,0 +1,4 @@
<!doctype html>
<html>
<script type="module" src="js/app.js"></script>
</html>

10
app/js/app.js Normal file
View file

@ -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();

55
app/js/mpd.js Normal file
View file

@ -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);
});
}

11
app/js/parser.js Normal file
View file

@ -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;
}

13
app/js/status.js Normal file
View file

@ -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();
}

10
index.js Normal file
View file

@ -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}`);

16
package.json Normal file
View file

@ -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"
}