From d453f5306ed196c9f2d6878e3cdbaa7b1bc088e6 Mon Sep 17 00:00:00 2001 From: bordun Date: Wed, 26 Jun 2019 11:42:48 +0200 Subject: [PATCH] custom-module: add loadJSON and saveJSON --- CHANGELOG.md | 4 +++- resources/docs/docs/custom-module.md | 2 ++ src/server/osc/index.js | 33 +++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2165eadc..3b3d1e2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ - sliders: `default` and `spring` properties are now dynamic - server - images urls are now resolved relatively to the session path directory by default (fallback to absolute path resolution if not found) - +- custom module + - added `loadJSON` and `saveJSON` functions + ## 0.46.2 - improved canvas rendering speed on chrome v69 and above diff --git a/resources/docs/docs/custom-module.md b/resources/docs/docs/custom-module.md index d20aa7fc..669f43fe 100644 --- a/resources/docs/docs/custom-module.md +++ b/resources/docs/docs/custom-module.md @@ -63,6 +63,8 @@ The module is executed in a restricted context, only a few globals are available - `settings.read`: `function(name)`, see [settings.js](https://github.com/jean-emmanuel/open-stage-control/blob/master/src/server/settings.js#L55-L103) for available options - `app`: an [event emitter](https://nodejs.org/api/events.html#events_class_eventemitter) for monitoring the events sent by the different clients. Event names can be found in [callbacks.js](https://github.com/jean-emmanuel/open-stage-control/blob/master/src/server/callbacks.js), callbacks are called with 2 arguments: `data` (object) and `client` (object: `{address, id}`) - `options`: `array` containing the extra options passed to `--custom-module` after the filename +- `loadJSON`: `function(path)` loads a json file (path is relative to the custom module location) +- `saveJSON`: `function(path, object)` save an object/array to a json file (path is relative to the custom module location) `sendOsc` and `receiveOsc` expect arguments formatted as follow: diff --git a/src/server/osc/index.js b/src/server/osc/index.js index 6dbb1d59..5e760430 100644 --- a/src/server/osc/index.js +++ b/src/server/osc/index.js @@ -1,4 +1,5 @@ -var ipc = require('../server').ipc, +var path = require('path'), + ipc = require('../server').ipc, settings = require('../settings'), tcpInPort = settings.read('tcpInPort'), debug = settings.read('debug'), @@ -26,8 +27,8 @@ class OscServer { if (!customModule[0].includes('.js')) { var index = customModule.findIndex(x => typeof x === 'string' && x.includes('.js')) if (index !== undefined) { - var path = customModule.slice(0, index + 1).join(' ') - customModule.splice(0, index + 1, path) + var _path = customModule.slice(0, index + 1).join(' ') + customModule.splice(0, index + 1, _path) } } @@ -57,6 +58,32 @@ class OscServer { } this.receiveOsc({host, port, address, args:args.map(x=>this.parseArg(x))}) }, + loadJSON: (url)=>{ + if (url.split('.').pop() === 'json') { + try { + url = path.resolve(path.dirname(customModule[0]), url) + return JSON.parse(fs.readFileSync(url, 'utf8')) + } catch(e) { + console.error('ERROR: could not load json file from ' + url) + console.error(e.message) + } + } else { + console.error('ERROR: unauthorized file type for loadJSON') + } + }, + saveJSON: (url, data)=>{ + if (url.split('.').pop() === 'json') { + url = path.resolve(path.dirname(customModule[0]), url) + try { + return fs.writeFileSync(url, JSON.stringify(data, null, ' ')) + } catch(e) { + console.error('ERROR: could not save json file to ' + url) + console.error(e.message) + } + } else { + console.error('ERROR: unauthorized file type for saveJSON') + } + }, app: this.customModuleEventEmitter, setTimeout, clearTimeout, setInterval, clearInterval,