From 8a23d282eaec7b586f2949c98a19cdce2683881d Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Mon, 5 Oct 2020 14:25:47 +0200 Subject: [PATCH] Some other commands --- common/lkt.js | 41 +++++++++++++++++++++++++++++++++++++++++ test/test_lkt.js | 6 ++++++ 2 files changed, 47 insertions(+) diff --git a/common/lkt.js b/common/lkt.js index b504fe4..f7cedef 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 c267bde..98a08ac 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); + }); -- GitLab