57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
|
const logger = require('./logger.js');
|
||
|
const util = require('./util.js');
|
||
|
const commands = require('./commands.js');
|
||
|
const config = require('../config.json').osc;
|
||
|
|
||
|
const CMD = 'oscsend';
|
||
|
|
||
|
async function send(controller, channel, value) {
|
||
|
if (controller == undefined || channel === undefined) {
|
||
|
return;
|
||
|
}
|
||
|
if (value === undefined || isNaN(value) || value < 0) {
|
||
|
value = 0;
|
||
|
} else if (value > 127) {
|
||
|
value = 127;
|
||
|
}
|
||
|
logger.debug('sending value \'' + value + '\' to controller \'' + controller + '\' on channel \'' + channel + '\'...');
|
||
|
let args = [
|
||
|
config.host, config.port,
|
||
|
config.address,
|
||
|
'm',
|
||
|
'00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel)
|
||
|
];
|
||
|
try {
|
||
|
await commands.execute(CMD, args);
|
||
|
} catch (err) {
|
||
|
throw new Error(err);
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
async function sendByRequest(id, args) {
|
||
|
if (args === undefined) {
|
||
|
throw new Error('no arguments defined');
|
||
|
}
|
||
|
const controller = args.get('controller');
|
||
|
if (controller === undefined) {
|
||
|
throw new Error('missing argument \'controller\'');
|
||
|
}
|
||
|
const channel = args.get('channel');
|
||
|
if (channel === undefined) {
|
||
|
throw new Error('missing argument \'channel\'');
|
||
|
} let value = args.get('value');
|
||
|
if (value === undefined) {
|
||
|
throw new Error('missing argument \'value\'');
|
||
|
}
|
||
|
try {
|
||
|
return JSON.stringify({ status: 'ok', controller: controller, channel: channel, value: await send(controller, channel, value) });
|
||
|
} catch (err) {
|
||
|
return JSON.stringify({ status: 'error', error: err });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
send,
|
||
|
sendByRequest
|
||
|
}
|