2022-03-11 16:52:37 +01:00
|
|
|
const logger = require('./logger.js');
|
|
|
|
const util = require('./util.js');
|
|
|
|
const commands = require('./commands.js');
|
|
|
|
|
|
|
|
const CMD = 'oscsend';
|
|
|
|
|
|
|
|
async function send(controller, channel, value) {
|
2022-03-25 13:52:41 +01:00
|
|
|
if (controller === undefined || channel === undefined) {
|
2022-03-11 16:52:37 +01:00
|
|
|
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 + '\'...');
|
|
|
|
try {
|
2022-03-25 13:52:41 +01:00
|
|
|
await commands.execute(CMD, [
|
|
|
|
global.config.osc.host, global.config.osc.port,
|
|
|
|
global.config.osc.address,
|
|
|
|
'm',
|
|
|
|
'00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel)
|
|
|
|
]);
|
2022-03-11 16:52:37 +01:00
|
|
|
} 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
|
|
|
|
}
|