From 0631dec3d32a40f5bf7160a23109d1d8a1bc41ad Mon Sep 17 00:00:00 2001 From: cv <34649812+cpvalente@users.noreply.github.com> Date: Wed, 5 May 2021 20:00:05 +0200 Subject: [PATCH] feat: OSC --- server/app.js | 6 ++- server/classes/EventTimer.js | 19 +++++--- server/classes/Timer.js | 17 ++++++-- server/config.json | 3 ++ server/controllers/OscController.js | 68 +++++++++++++++++++++++++++++ server/package.json | 1 + server/yarn.lock | 19 ++++++++ 7 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 server/controllers/OscController.js diff --git a/server/app.js b/server/app.js index 11eddd13d..41ab02d90 100644 --- a/server/app.js +++ b/server/app.js @@ -72,4 +72,8 @@ global.timer = new EventTimer(server, config); timer.setupWithEventList(eventlist); // Start server -server.listen(port, () => console.log(`Listening on port ${port}`)); +server.listen(port, () => console.log(`HTTP Server is listening on port ${port}`)); + +// Start OSC server +const initiateOSC = require('./controllers/OscController.js').initiateOSC; +initiateOSC(config.osc); diff --git a/server/classes/EventTimer.js b/server/classes/EventTimer.js index 92ac96ab5..e68539a7d 100644 --- a/server/classes/EventTimer.js +++ b/server/classes/EventTimer.js @@ -437,6 +437,7 @@ class EventTimer extends Timer { if (end < start) end += this.DAYMS; // time stuff changes on wheter we keep the running clock + if (type === 'load') { this._resetTimers(); @@ -586,14 +587,14 @@ class EventTimer extends Timer { current = ${this.current} duration = ${this.duration} - Options - ------------------------------ - showNegative = ${this.showNegative} - Events ------------------------------ - numEvents = ${this.numEvents} - selectedEvent = ${this.selectedEvent} + numEvents = ${this.numEvents} + selectedEvent = ${this.selectedEvent} + selectedEventId = ${this.selectedEventId} + nextEventId = ${this.nextEventId} + selectedPublicEventId = ${this.selectedPublicEventId} + nextPublicEventId = ${this.nextPublicEventId} Private Titles ------------------------------ @@ -617,7 +618,6 @@ class EventTimer extends Timer { Subtitle Next = ${this.titlesPublic.subtitleNext} Presenter Next = ${this.titlesPublic.presenterNext} - Messages ------------------------------ presenter text = ${this.presenter.text} @@ -630,10 +630,15 @@ class EventTimer extends Timer { Private ------------------------------ finishAt = ${this._finishAt} + finished = ${this._finishedAt} startedAt = ${this._startedAt} pausedAt = ${this._pausedAt} pausedInterval = ${this._pausedInterval} pausedTotal = ${this._pausedTotal} + + Socket + ------------------------------ + numClients = ${this._numClients} `; } diff --git a/server/classes/Timer.js b/server/classes/Timer.js index 387436c64..6fd83e5e3 100644 --- a/server/classes/Timer.js +++ b/server/classes/Timer.js @@ -54,12 +54,20 @@ class Timer { this.current = this._startedAt + this.duration + this._pausedTotal - now; - if (this.current <= 0 && this._finishedAt == null) - this._finishedAt = now; break; case 'pause': // update paused time this._pausedInterval = now - this._pausedAt; + + if (this._startedAt != null) { + // update current timer + this.current = + this._startedAt + + this.duration + + this._pausedTotal + + this._pausedInterval - + now; + } break; case 'stop': // nothing here yet @@ -68,6 +76,9 @@ class Timer { console.error('Timer: no playstate on update call', this.state); break; } + + // Cleanup + if (this.current <= 0 && this._finishedAt == null) this._finishedAt = now; } // helpers @@ -193,7 +204,7 @@ class Timer { if (amount < 0 && Math.abs(amount) > this.current) { // if we will make the clock negative - this._finishedAt = this._getCurrentTime(); + if (this._finishedAt == null) this._finishedAt = this._getCurrentTime(); } else if (this.current < 0 && this.current + amount > 0) { // clock will go from negative to positive this._finishedAt = null; diff --git a/server/config.json b/server/config.json index 8287c70dc..40d18e4a8 100644 --- a/server/config.json +++ b/server/config.json @@ -8,5 +8,8 @@ "database": { "filename": "db.json", "tablename": "events" + }, + "osc": { + "port": 8888 } } \ No newline at end of file diff --git a/server/controllers/OscController.js b/server/controllers/OscController.js new file mode 100644 index 000000000..d10b1c2c4 --- /dev/null +++ b/server/controllers/OscController.js @@ -0,0 +1,68 @@ +const osc = require('node-osc'); + +const initiateOSC = (config) => { + const oscServer = new osc.Server(config.port, '0.0.0.0', () => { + console.log(`OSC Server is listening on port ${config.port}`); + }); + + oscServer.on('message', function (msg) { + // message should look like /ontime/{path}/{args} where + // ontime: fixed message for app + // path: command to be called + // args: extra data, only used on some of the API entries (delay, goto) + console.log('OSC received', msg); + + // split message + const [, address, path] = msg[0].split('/'); + const args = msg[1]; + + // get first part before (ontime) + if (address !== 'ontime') return; + + // get second part (command) + switch (path) { + case 'start': + case 'play': + console.log('calling play'); + global.timer.start(); + break; + case 'pause': + console.log('calling pause'); + global.timer.pause(); + break; + case 'prev': + console.log('calling prev'); + global.timer.previous(); + break; + case 'next': + console.log('calling next'); + global.timer.next(); + break; + case 'unload': + console.log('calling unload'); + global.timer.unload(); + break; + case 'reload': + console.log('calling reload'); + global.timer.reload(); + break; + case 'roll': + console.log('calling roll'); + break; + case 'delay': + console.log('calling delay with', args); + let t = parseInt(args); + if (!isNaN(t)) global.timer.increment(t * 1000 * 60); + break; + case 'goto': + console.log('calling goto with', args); + break; + + default: + console.log('Error: not recognised'); + break; + } + }); +}; + +module.exports = { initiateOSC }; diff --git a/server/package.json b/server/package.json index 94b6b299c..45025a56a 100644 --- a/server/package.json +++ b/server/package.json @@ -8,6 +8,7 @@ "express-session": "~1.17.1", "lowdb": "^1.0.0", "nanoid": "^3.1.22", + "node-osc": "^6.0.1", "passport": "~0.4.1", "passport-local": "~1.0.0", "socket.io": "^4.0.0" diff --git a/server/yarn.lock b/server/yarn.lock index 7c5a3fd23..4b7da8610 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -45,6 +45,11 @@ base64id@2.0.0, base64id@~2.0.0: resolved "https://registry.yarnpkg.com/base64id/-/base64id-2.0.0.tgz#2770ac6bc47d312af97a8bf9a634342e0cd25cb6" integrity sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog== +binpack@~0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/binpack/-/binpack-0.1.0.tgz#bd3d0974c3f2a0446e17df4f60b55a72a205a97e" + integrity sha1-vT0JdMPyoERuF99PYLVacqIFqX4= + body-parser@1.19.0, body-parser@~1.19.0: version "1.19.0" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" @@ -375,6 +380,13 @@ negotiator@0.6.2: resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb" integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw== +node-osc@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/node-osc/-/node-osc-6.0.1.tgz#6f2c39996c64460435b678dc91c128d3a9711031" + integrity sha512-UZBQGH4vVTw3Ub5/xcnhlrhL6bpaYz3hhNjO4eup4tknyP5qOi2GVVnEmEn/qaTgTvs7SVyytj3OHg9JWRD4Aw== + dependencies: + osc-min "^1.1.1" + object-assign@^4: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" @@ -392,6 +404,13 @@ on-headers@~1.0.2: resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f" integrity sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA== +osc-min@^1.1.1: + version "1.1.2" + resolved "https://registry.yarnpkg.com/osc-min/-/osc-min-1.1.2.tgz#3dcaff10f804c39b7080cf4c10fd7a4103cd9037" + integrity sha512-8DbiO8ME85R75stgNVCZtHxB9MNBBNcyy+isNBXrsFeinXGjwNAauvKVmGlfRas5VJWC/mhzIx7spR2gFvWxvg== + dependencies: + binpack "~0" + parseurl@~1.3.3: version "1.3.3" resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"