diff --git a/common/lkt.js b/common/lkt.js index b504fe414894a840b450167cbb422901514170ce..f7cedefd0285fc4c28c30cff9b372ff7063fa481 100644 --- a/common/lkt.js +++ b/common/lkt.js @@ -109,6 +109,41 @@ class LktClient { }; return __getResult(client); } + + /* Arguments: + * - command: The command to execute + * Result: + * - Returns the status of the command */ + static __execSimple(command) { + var client = new this(); + var once = false; + var result = {}; + function __getResult(client) { + return new Promise(resolv => { + client.m_socket.on("data", (data) => { + if (!once) { + client.m_socket.write(`${command}\n`); + once = true; + return null; + } else { + client.close(); + result = __mpdStatusToBool(data); + resolv(result); + } + }); + }) + }; + return __getResult(client); + } + + static commandPlay() { return LktClient.__execSimple("play") }; + static commandPlay() { return LktClient.__execSimple("stop") }; + static commandPrev() { return LktClient.__execSimple("previous") }; + static commandNext() { return LktClient.__execSimple("next") }; + static commandShuffle() { return LktClient.__execSimple("shuffle") }; + + static commandQueueAddId(id) { return LktClient.__execSimple(`add id ${id}`); } + static commandQueueDelId(id) { return LktClient.__execSimple(`deleteid ${id}`); } } function __mpdToObject(string) { @@ -124,4 +159,10 @@ function __mpdToObject(string) { return ret; } +function __mpdStatusToBool(string) { + string = string.split("\n"); + string = string[string.length - 1]; + return string.split(/ /) == "OK" ? true : false; +} + module.exports = LktClient; diff --git a/test/test_lkt.js b/test/test_lkt.js index c267bdef7d91f53995bfe7bde1c77edde579a8d1..98a08aca49ecbc453ceda28ab04a905cd0f23261 100644 --- a/test/test_lkt.js +++ b/test/test_lkt.js @@ -6,3 +6,9 @@ lkt .then( (res) => { console.log(res); }) + +lkt + .commandPlay() + .then( (res) => { + console.log(res); + });