custom-module: add loadJSON and saveJSON

This commit is contained in:
bordun 2019-06-26 11:42:48 +02:00
parent bf00a58bc7
commit def2560f97
3 changed files with 35 additions and 4 deletions

View file

@ -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

View file

@ -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:

View file

@ -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,