2024-02-20 04:57:49 +00:00
|
|
|
import encoder from './encoder.mjs'
|
|
|
|
import { runCommand } from './runner.mjs'
|
2024-02-13 23:54:45 +00:00
|
|
|
|
|
|
|
export default class EncoderRoutes {
|
2024-02-20 04:57:49 +00:00
|
|
|
constructor() {
|
|
|
|
this.core = null
|
|
|
|
}
|
|
|
|
|
|
|
|
registerGlobalIo(io, server) {
|
|
|
|
this.core = server.core
|
|
|
|
encoder.init(server.core, io)
|
2024-02-13 23:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
registerIo(server, ctx) {
|
|
|
|
ctx.socket.safeOn('encoder.status', this.status.bind(this))
|
2024-02-20 04:57:49 +00:00
|
|
|
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))
|
2024-02-13 23:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status(ctx) {
|
2024-02-20 04:57:49 +00:00
|
|
|
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')
|
|
|
|
)
|
2024-02-13 23:54:45 +00:00
|
|
|
}
|
|
|
|
}
|