From 106bcb3382a920ed5106146df169bacc2dabc87f Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Tue, 19 Mar 2019 22:10:02 +0100 Subject: [PATCH] albumart --- commands.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 5 +++++ 2 files changed, 58 insertions(+) diff --git a/commands.js b/commands.js index 77fe84e..ce50d3e 100644 --- a/commands.js +++ b/commands.js @@ -23,6 +23,7 @@ class Command extends EventEmitter { _done(data) { this._mpd.off("data", this._dataListener); + this._buffer = null; this.emit("done", data); } @@ -60,7 +61,59 @@ class Welcome extends Command { } } +class AlbumArt extends Command { + constructor(mpd, command) { + super(mpd); + this._size = 0; + this._binary = 0; + this._data = null; + this._lines = []; + + log("--> mpd", command); + mpd.write(command + "\n"); + } + + _processBuffer() { + if (!this._size) { + let line = this._getLine(); + if (!line) { return; } + this._lines.push(line); + this._size = Number(line.split(": ").pop()); + log("size", this._size); + } + + if (!this._binary) { + let line = this._getLine(); + if (!line) { return; } + this._lines.push(line); + this._binary = Number(line.split(": ").pop()); + log("binary", this._binary); + } + + if (!this._data) { + // binary data has this._binary bytes + 1 newline + if (this._buffer.length >= this._binary+1) { + this._data = this._buffer.slice(0, this._binary); + this._buffer = this._buffer.slice(this._binary+1); + this._lines.push([...this._data]); + log("data", this._data.length); + } else { return; } + } + + let line = this._getLine(); + if (!line) { return; } + this._lines.push(line); + this._done(this._lines); + } + +} + exports.create = function(mpd, command) { + if (command.startsWith("albumart")) { + return new AlbumArt(mpd, command); + } else { + return new Normal(mpd, command); + } return new Normal(mpd, command); } diff --git a/index.js b/index.js index aae4e94..194dc4d 100644 --- a/index.js +++ b/index.js @@ -54,6 +54,11 @@ function initConnection(request) { ws.close(); }); + mpd.on("error", () => { + log("mpd connection error"); + ws.close(); + }); + waitForCommand(commands.welcome(mpd)); }