This commit is contained in:
cv
2021-06-10 22:46:44 +02:00
parent 441a67e09b
commit 780c355a10
3 changed files with 115 additions and 17 deletions
+15 -2
View File
@@ -10,6 +10,7 @@ const adapter = new JSONFile(file);
export const db = new Low(adapter);
// dependencies
import { Client } from 'node-osc';
import express from 'express';
import http from 'http';
import cors from 'cors';
@@ -74,8 +75,21 @@ app.use((err, req, res, next) => {
// create HTTP server
const server = http.createServer(app);
// Start OSC Client
let oscClient = null;
export const startOSCClient = (overrideConfig = null) => {
// Setup IP
const ipOut = overrideConfig?.ip || config.osc.ipOut;
// Setup default port
const oscOutPort = overrideConfig?.port || config.osc.portOut;
oscClient = new Client(ipOut, oscOutPort);
console.log(`OSC Client sending to ${ipOut}:${oscOutPort}`);
};
startOSCClient();
// init timer
global.timer = new EventTimer(server, config);
global.timer = new EventTimer(server, oscClient, config);
global.timer.setupWithEventList(data.events);
// Start server
@@ -85,5 +99,4 @@ server.listen(port, () =>
// Start OSC server
import { initiateOSC } from './controllers/OscController.js';
initiateOSC(config.osc);
+98 -15
View File
@@ -15,6 +15,10 @@ export class EventTimer extends Timer {
// Socket IO Object
io = null;
// OSC Client
oscClient = null;
_numClients = 0;
_interval = null;
@@ -57,7 +61,7 @@ export class EventTimer extends Timer {
numEvents = null;
_eventlist = null;
constructor(httpServer, config) {
constructor(httpServer, oscClient, config) {
// call super constructor
super();
@@ -79,11 +83,80 @@ export class EventTimer extends Timer {
// listen to new connections
this._listenToConnections();
// set oscClient
this.updateOSCClient(oscClient);
}
updateOSCClient(oscClient) {
this.oscClient = oscClient;
}
sendOSC(event) {
if (this.oscClient == null) return;
const add = '/ontime';
const play = 'play';
const pause = 'pause';
const stop = 'stop';
const prev = 'prev';
const next = 'next';
const reload = 'reload';
switch (event) {
case 'time':
// Send Timetag Message
this.oscClient.send(add, this.timeTag, (err) => {
if (err) console.error(err);
});
break;
case 'play':
// Play Message
this.oscClient.send(add, play, (err) => {
if (err) console.error(err);
});
break;
case 'pause':
// Pause Message
this.oscClient.send(add, pause, (err) => {
if (err) console.error(err);
});
break;
case 'stop':
// Stop Message
this.oscClient.send(add, stop, (err) => {
if (err) console.error(err);
});
break;
case 'prev':
this.oscClient.send(add, prev, (err) => {
if (err) console.error(err);
});
break;
case 'next':
this.oscClient.send(add, next, (err) => {
if (err) console.error(err);
});
break;
case 'reload':
this.oscClient.send(add, reload, (err) => {
if (err) console.error(err);
});
break;
default:
break;
}
}
// send current timer
broadcastTimer() {
// through websockets
this.io.emit('timer', this.getObject());
// through OSC
this.sendOSC('time');
}
// broadcast state
@@ -134,20 +207,6 @@ export class EventTimer extends Timer {
}
}
start() {
// if there is nothing selected, no nothing
if (this.selectedEventId == null) return;
super.start();
this.broadcastState();
}
pause() {
// if there is nothing selected, no nothing
if (this.selectedEventId == null) return;
super.pause();
this.broadcastState();
}
_setterManager(action, payload) {
switch (action) {
/*******************************************/
@@ -771,19 +830,31 @@ export class EventTimer extends Timer {
}
start() {
// if there is nothing selected, no nothing
if (this.selectedEventId == null) return;
// call super
super.start();
// broadcast current state
this.broadcastState();
// send OSC
this.sendOSC('play');
}
pause() {
// if there is nothing selected, no nothing
if (this.selectedEventId == null) return;
// call super
super.pause();
// broadcast current state
this.broadcastState();
// send OSC
this.sendOSC('pause');
}
stop() {
@@ -792,6 +863,9 @@ export class EventTimer extends Timer {
// broadcast current state
this.broadcastState();
// send OSC
this.sendOSC('stop');
}
increment(amount) {
@@ -885,6 +959,9 @@ export class EventTimer extends Timer {
return;
}
// send OSC
this.sendOSC('prev');
// change playstate
this.pause();
@@ -905,6 +982,9 @@ export class EventTimer extends Timer {
return;
}
// send OSC
this.sendOSC('next');
// change playstate
this.pause();
@@ -932,6 +1012,9 @@ export class EventTimer extends Timer {
// reset playstate
this.pause();
// send OSC
this.sendOSC('reload');
// reload data
this.loadEvent(this.selectedEventIndex);
}
+2
View File
@@ -11,5 +11,7 @@ export const config = {
},
osc: {
port: 8888,
ipOut: '127.0.0.1',
portOut: 9999,
},
};