80 lines
1.8 KiB
JavaScript
80 lines
1.8 KiB
JavaScript
// Liquidsoap class
|
|
const path = require('path')
|
|
const net = require('net')
|
|
|
|
const spawn = require('child_process').spawn
|
|
|
|
const config = require(path.join(__dirname, 'config.js'))
|
|
const rootpath = path.join(__dirname, '..')
|
|
const csl = require(path.join(__dirname, 'console.js'))
|
|
|
|
function printProcessOutput (fn) {
|
|
return function (data) {
|
|
let str = data.toString().trim()
|
|
csl.rl.output.write('\x1b[2K\r')
|
|
fn.call(null, str)
|
|
csl.rl && csl.rl.prompt(true)
|
|
}
|
|
}
|
|
|
|
class Liquidsoap {
|
|
constructor () {
|
|
this.running = false
|
|
}
|
|
|
|
start () {
|
|
let pd = path.parse(config.liquidsoap.entry)
|
|
let proc = this.proc = spawn('liquidsoap', [pd.base], {cwd: path.join(rootpath, pd.dir)})
|
|
this.running = true
|
|
|
|
proc.stdout.on('data', printProcessOutput(csl.realConsoleLog))
|
|
proc.stderr.on('data', printProcessOutput(csl.realConsoleError))
|
|
|
|
proc.on('close', () => {
|
|
this.running = false
|
|
})
|
|
}
|
|
|
|
stop () {
|
|
if (this.running && this.proc) {
|
|
this.proc.kill()
|
|
}
|
|
}
|
|
|
|
async sendCommand (msg) {
|
|
if (!this.running) throw new Error('Process is not running.')
|
|
let client = net.connect(config.control.port, config.control.host)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
client.once('connect', function () {
|
|
let split = msg.split('\n')
|
|
for (let i in split) {
|
|
client.write(split[i] + '\r\n')
|
|
}
|
|
|
|
client.write('quit\r\n')
|
|
client.end()
|
|
|
|
resolve()
|
|
})
|
|
})
|
|
}
|
|
|
|
queue (item) {
|
|
if (typeof item !== 'object') item = [item]
|
|
|
|
let q = []
|
|
for (let i in item) {
|
|
q.push('queue.push smart:' + item[i])
|
|
}
|
|
|
|
this.sendCommand(q.join('\n')).catch((e) => console.error('Failed to queue:', e.message))
|
|
}
|
|
|
|
skip () {
|
|
this.sendCommand('skip').catch((e) => console.error('Failed to skip:', e.message))
|
|
}
|
|
}
|
|
|
|
module.exports = Liquidsoap
|