import encoder from './encoder.mjs' import { runCommand } from './runner.mjs' export default class EncoderRoutes { constructor() { this.core = null } registerGlobalIo(io, server) { this.core = server.core encoder.init(server.core, io) } registerIo(server, ctx) { ctx.socket.safeOn('encoder.status', this.status.bind(this)) ctx.socket.safeOn('encoder.stop', this.stop.bind(this)) ctx.socket.safeOn('encoder.start', this.start.bind(this)) ctx.socket.safeOn('encoder.run', this.run.bind(this)) ctx.socket.safeOn('encoder.settings', this.settings.bind(this)) } status(ctx) { ctx.socket.emit('encoder.status', encoder.status()) } stop(ctx) { encoder.safeStop() } start(ctx) { encoder.safeStart() } settings(ctx, data) { this.core.db.data.encoder_settings = { device: data.device, format_code: data.format_code, command: data.command, } this.core.db.write() } run(ctx, data) { return runCommand(data.command, data.arguments, (msg, source) => { encoder.log('info', msg.replace(/\n/g, ''), 'RUN') }) .then( code => encoder.log('info', 'Program returned successfully', 'RUN'), err => encoder.log('error', err.message, 'RUN') ) } }