Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • 639842ca45f1455bdc61e761c3f721846974fa8b
  • master par défaut protégée
  • dev-deurstann-3
  • dev-deurstann-2
  • dev-kubat
  • dev-deurstann
  • dev-sting
7 résultats

lkt.js

Blame
  • lkt.js 6,98 Kio
    const logger = require.main.require('./common/logger.js');
    const net = require('net');
    const { finished } = require('stream');
    const { cli } = require('winston/lib/winston/config');
    
    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('lkt', 'Creating the lektor client');
            this.m_socket.setTimeout(3000);
            this.m_socket.setEncoding('utf8');
            this.m_socket.on('timeout', () => {
                logger.error(
                    'lkt',
                    `Got timeout while connecting to localhost:${this.m_port}`
                );
                this.m_socket.end();
            });
    
            this.m_socket.on('ready', () => {
                logger.debug(
                    'lkt',
                    `Ready to use socker with localhost:${this.m_port}`
                );
                this.m_online = true;
            });
    
            this.m_socket.on('end', () => {
                logger.info(
                    'lkt',
                    `Disconnected from server localhost:${this.m_port}`
                );
                this.m_online = false;
            });
    
            this.m_socket.on('error', err => {
                logger.error('lkt', `${err}`);
                if (this.m_online) {
                    this.m_socket.destroy();
                    this.m_online = false;
                }
            });
    
            this.m_socket.on('close', () => {
                logger.info('lkt', `Socket localhost:${this.m_port} closed`);
                this.m_online = false;
            });
    
            this.m_socket.connect(sockopt, () => {
                logger.info('lkt', `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('lkt', 'Requesting socket shutdown');
                this.m_socket.destroy();
            } else {
                logger.debug('lkt', '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.setTimeout(0);
                    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 idleActualisation() {
            var client = new this();
            client.m_socket.setTimeout(0);
            function __getResult(client) {
                return new Promise((resolve, reject) => {
                    client.m_socket.on('data', data => {
                        if (String(data).includes('playlist')) {
                            LktClient.setQueueUpdated(true);
                        }
                        client.m_socket.write(`idle\n`);
                        return null;
                    });
                });
            }
            return __getResult(client);
        }
    
        static commandPlay() {
            var status = LktClient.commandStatus();
            return status.then(LktClient.changePlayStatus, LktClient.errorStatus);
        }
        static changePlayStatus(status) {
            switch (status.state) {
                case 'play':
                    return LktClient.__execSimple('pause 1');
                    break;
                case 'pause':
                    return LktClient.__execSimple('pause 0');
                    break;
                case 'stop':
                    return LktClient.__execSimple('play');
                    break;
                default:
                    logger.info('Unknown play state' + status.state);
            }
        }
        static commandStop() {
            //return LktClient.idleActualisation();
            return LktClient.__execSimple('stop');
        }
        static commandPrev() {
            return LktClient.__execSimple('previous');
        }
        static commandNext() {
            return LktClient.__execSimple('next');
        }
        static commandShuffle() {
            return LktClient.__execSimple('shuffle');
        }
        static commandClear() {
            return LktClient.__execSimple('clear');
        }
        static commandMove(from, to) {
            var request = 'move ' + from + ' ' + to;
            logger.info(request);
            return LktClient.__execSimple('move ' + from + ' ' + to);
        }
    
        static commandQueueAddId(id) {
            return LktClient.__execSimple(`add id ${id}`);
        }
        static commandQueueDelId(id) {
            return LktClient.__execSimple(`deleteid ${id}`);
        }
        static errorStatus(error) {
            logger.error('Unable to access lektor status:' + error);
        }
    
        static queue_updated = false;
    
        static setQueueUpdated(state) {
            this.queue_updated = state;
        }
    
        static isQueueUpdated() {
            return this.queue_updated;
        }
    }
    
    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;