Sélectionner une révision Git
Bifurcation depuis
Alexandre MORIGNOT / PlayBot
Le projet source a une visibilité limitée.
-
Alexandre Morignot a rédigéAlexandre Morignot a rédigé
lkt.js 4,93 Kio
const logger = require.main.require('./common/logger.js');
const net = require('net');
class LktClient {
/***************************************
* Not static methods, do not use them *
***************************************/
/* The constructor
* - host: String
* - port: String
*
* Private members
* - m_socket: net.socket
*
* Node: You should not use that directly, prefere the use of static
* methods */
constructor(port = 6600) {
this.m_online = false;
this.m_port = port;
this.m_socket = new net.Socket();
this.m_closed = true;
const sockopt = {
port: this.m_port,
host: "localhost",
readable: true,
writable: true
};
logger.debug("Creating the lektor client");
this.m_socket.setTimeout(3000);
this.m_socket.setEncoding('utf8');
this.m_socket.on("timeout", () => {
logger.error(`Got timeout while connecting to localhost:${this.m_port}`);
this.m_socket.end();
});
this.m_socket.on("ready", () => {
logger.debug(`Ready to use socker with localhost:${this.m_port}`);
this.m_online = true;
});
this.m_socket.on("end", () => {
logger.info(`Disconnected from server localhost:${this.m_port}`);
this.m_online = false;
});
this.m_socket.on('error', (err) => {
logger.error(`${err}`);
if (this.m_online) {
this.m_socket.destroy();
this.m_online = false;
}
});
this.m_socket.on("close", () => {
logger.info(`Socket localhost:${this.m_port} closed`);
this.m_online = false;
});
this.m_socket.connect(sockopt, () => {
logger.info(`Socket connected to localhost:${this.m_port}`);
this.m_online = true;
});
return this;
}
/* Close the client.
* Note: Prefere using the static methods. */
close() {
if (this.m_online) {
logger.debug("Requesting socket shutdown");
this.m_socket.destroy();
} else {
logger.debug("Socket already closed");
}
this.m_closed = true;
}
/*****************************************
* Static methods, to be used by clients *
*****************************************/
/* The status command */
static commandStatus() {
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("status\n");
once = true;
return null;
} else {
client.close();
result = __mpdToObject(data);
resolv(result);
}
});
})
};
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) {
var ret = {};
string
.split("\n")
.forEach( (line) => {
if (line.length <= 1)
return;
var couple = line.split(/: (.+)/).slice(0, 2);
ret[couple[0]] = couple[1];
});
return ret;
}
function __mpdStatusToBool(string) {
string = string.split("\n");
string = string[string.length - 1];
return string.split(/ /) == "OK" ? true : false;
}
module.exports = LktClient;