icytv-liq/control/index.js

96 lines
2.3 KiB
JavaScript

const path = require('path')
const config = require(path.join(__dirname, 'config.js'))
const Liquidsoap = require(path.join(__dirname, 'liquidsoap.js'))
const Scheduler = require(path.join(__dirname, 'schedule.js'))
const rl = require(path.join(__dirname, 'console.js')).rl
let liq = new Liquidsoap()
liq.start()
let scheduler = new Scheduler(config.calendar)
scheduler.startTimers()
scheduler.runner = function (event) {
liq.queue(event.descriptor)
}
scheduler.reload = function (calendar) {
liq.sendCommand('schedule.reload').catch((e) => console.error('Failed to reload schedule:', e.message))
}
// User input handler
rl.on('line', function (line) {
let argv = line.split(' ')
switch (argv[0]) {
case 'stop':
console.log('Stopping liquidsoap..')
liq.stop()
break
case 'start':
if (liq.running) return
console.log('Starting liquidsoap..')
liq.start()
break
case 'restart':
console.log('Restarting liquidsoap..')
liq.stop()
setTimeout(() => liq.start(), 4000)
break
case 'queue':
let qline = line.substring(6)
liq.queue(qline)
break
case 'skip':
liq.skip()
break
case 'status':
console.log(liq.running ? 'Liquidsoap is running' : 'Liquidsoap is stopped')
break
case 'events':
console.log('Refreshing scheduler..')
scheduler.calendarFetch().catch((e) => console.error('Calendar fetch failed!', e.stack))
break
case 'reload':
console.log('Reloading configuration..')
config.read().then(
() => {
console.log('Configuration reloaded successfully.')
// Restart scheduler
scheduler.stopTimers()
scheduler.startTimers()
},
(e) => console.error('Configuration reload failed:', e.stack)
)
break
default:
console.log('Unknown command.')
}
rl.prompt(true)
})
rl.on('close', function () {
scheduler.stopTimers()
if (liq.running) {
liq.stop()
console.log('Waiting for liquidsoap to exit')
setTimeout(function () {
if (liq.running) {
console.warn('Liquidsoap did not exit in time, forcing..')
liq.proc.kill(9)
}
process.stdout.write('\n')
process.exit(0)
}, 1000)
return
}
console.log('Bye bye!')
process.stdout.write('\n')
process.exit(0)
})