refactor: migrate to OSC min

This commit is contained in:
arc-alex
2025-01-12 13:48:48 +01:00
committed by Carlos Valente
parent bc9345199b
commit f4f266dbd4
8 changed files with 143 additions and 125 deletions
+38 -16
View File
@@ -1,33 +1,51 @@
import { LogOrigin } from 'ontime-types';
import { Server } from 'node-osc';
import { fromBuffer } from 'osc-min';
import * as dgram from 'node:dgram';
import { IAdapter } from './IAdapter.js';
import { logger } from '../classes/Logger.js';
import { integrationPayloadFromPath } from './utils/parse.js';
import { dispatchFromAdapter } from '../api-integration/integration.controller.js';
import { isOntimeCloud } from '../externals.js';
export class OscServer implements IAdapter {
private readonly osc: Server;
class OscServer implements IAdapter {
private udpSocket: dgram.Socket | null = null;
constructor(portIn: number) {
this.osc = new Server(portIn, '0.0.0.0');
this.osc.on('error', (error) => logger.error(LogOrigin.Rx, `OSC IN: ${error}`));
this.osc.on('message', (msg) => {
init(port: number) {
if (isOntimeCloud) {
logger.warning(LogOrigin.Rx, 'OSC: Skip starting server in cloud environment');
}
this.udpSocket?.close();
logger.info(LogOrigin.Rx, `OSC: Starting server on port ${port}`);
this.udpSocket = dgram.createSocket('udp4');
this.udpSocket.on('error', (error) => logger.error(LogOrigin.Rx, `OSC IN: ${error}`));
this.udpSocket.on('message', (buf: ArrayBuffer) => {
// message should look like /ontime/{command}/{params?} {args} where
// ontime: fixed message for app
// command: command to be called
// params: used to create a nested object to patch with
// args: extra data, only used on some API entries
// split message
const [, address, command, ...params] = msg[0].split('/');
const args = msg[1];
/**
* TODO: remove this type casting when mergend in deleration file
* https://github.com/DefinitelyTyped/DefinitelyTyped/pull/71659
*/
const msg = fromBuffer(buf);
if (msg.oscType === 'bundle') {
//TODO: manage bundles
logger.error(LogOrigin.Rx, `OSC IN: We don't take bundles`);
return;
}
// get first part before (ontime)
if (address !== 'ontime') {
const { address, args: oscArgs } = msg;
// split message
const [, ontimeKey, command, ...params] = address.split('/');
const args = oscArgs[0]?.value ?? undefined; //TODO: manage multiple args or mayeb we have no usecase
// get first part (ontime)
if (ontimeKey !== 'ontime') {
logger.error(LogOrigin.Rx, `OSC IN: OSC messages to ontime must start with /ontime/, received: ${msg}`);
return;
}
@@ -50,9 +68,13 @@ export class OscServer implements IAdapter {
logger.error(LogOrigin.Rx, `OSC IN: ${error}`);
}
});
this.udpSocket.bind(port);
}
shutdown() {
this.osc?.close();
logger.info(LogOrigin.Rx, `OSC: Closing server`);
this.udpSocket?.close();
this.udpSocket = null;
}
}
export const oscServer = new OscServer();