From 47285f4dea3b94d41b6b85834f16292c3dc49c78 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 11 Oct 2016 15:31:35 +0200 Subject: [PATCH] main: change main osc module to support boolean args properly, add vmodule deps to disable unsupported serialport require() --- README.md | 2 +- app/main/osc.js | 94 ++++++++++++++++++------------------- app/package.json | 3 +- app/resources/docs/index.md | 2 +- 4 files changed, 50 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 2b286368..e66fdfc3 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ It relies on the use of several libraries : - [Node.js](https://nodejs.org/) - [Electron](http://electron.atom.io/) -- [node-osc](https://github.com/TheAlphaNerd/node-osc) +- [osc.js](https://github.com/colinbdclark/osc.js) - [socket.io](http://socket.io) - [browserify](http://browserify.org) - [express](http://expressjs.com) diff --git a/app/main/osc.js b/app/main/osc.js index 4e29c5ed..15197a72 100644 --- a/app/main/osc.js +++ b/app/main/osc.js @@ -1,67 +1,65 @@ -var osc = require('node-osc'), +// destroy serialport module as it's currently unsupported and would throw when required by osc +require('vmodule')('serialport', {}, {global: true}) + +var osc = require('osc'), ipc = require('./server').ipc, settings = require('./settings'), oscInPort = settings.read('oscInPort'), debug = settings.read('debug') -if (oscInPort) { - var oscServer = new osc.Server(oscInPort, '127.0.0.1') +var oscServer = new osc.UDPPort({ + localAddress: "0.0.0.0", + localPort: oscInPort +}); - oscServer.on('message', function (msg, rinfo) { - var data = {path:msg.shift(),args:msg} - if (data.args.length==1) data.args = data.args[0] - ipc.send('receiveOsc',data) - if (debug) console.log('OSC received: ',data, 'From : ' + rinfo.address + ':' + rinfo.port) - }) +oscServer.on('message', function (msg, bundle, info) { + var data = {path: msg.address, args: msg.args} + if (data.args.length==1) data.args = data.args[0] + ipc.send('receiveOsc',data) + if (debug) console.log('OSC received: ',data, 'From : ' + info.address + ':' + info.port) +}) + +oscServer.open() + + +var parseArg = function(arg,precision){ + if (arg==null) return null + switch (typeof arg) { + case 'number': + return {type: precision == 0 ? 'i' : 'f', value: arg} + case 'boolean': + return {type: arg ? 'T' : 'F', value: arg} + case 'object': + return {type: arg.type, value: arg.value} + default: + return {type: 'string', value:String(arg)} + } } module.exports = { send: function(host,port,path,args,precision) { - var client = new osc.Client(host, port) + var message = [] - client._sock.bind({exclusive:false,reuseAddr:true,port:oscInPort},function(){ + if (typeof args=='object' && args!==null) { + for (i in args) { + var arg = parseArg(args[i],precision) + if (arg!=null) message.push(arg) + } + } else if (args!==null){ + var arg = parseArg(args[i],precision) + if (arg!=null) message.push(arg) + } - - var message = new osc.Message(path) - - var numberType = precision==0?'integer':'float' - - if (typeof args=='object' && args!==null) { - for (i in args) { - var arg = args[i] - if (typeof arg == 'number' && arg!==null && typeof arg != 'object') { - message.append({type:numberType,value:arg}) - } else if (typeof arg == "boolean") { - message.append({type:"boolean",value:arg}) - } else if (arg!==null && typeof arg != 'object'){ - message.append({type:'string',value:String(arg)}) - } else if (arg!==null && typeof arg == 'object' && arg.type && arg.value!=undefined) { - message.append({type:arg.type,value:arg.value}) - } - } - } else if (args!==null){ - if (typeof args == 'number') { - message.append({type:numberType,value:args}) - } else if (typeof args == "boolean") { - message.append({type:"boolean",value:args}) - } else if (typeof args == 'object' && arg.type && arg.value!=undefined) { - message.append({type:args.type,value:args.value}) - } else { - message.append({type:'string',value:String(args)}) - } - } - - client.send(message, function () { - client.kill() - }) - - if (debug) console.log('OSC sent: ',{path:path,args:message.args}, 'To : ' + host + ':' + port) - - }) + oscServer.send({ + address: path, + args: message + }, host, port); + + if (debug) console.log('OSC sent: ',{path:path,args:message}, 'To : ' + host + ':' + port) } } diff --git a/app/package.json b/app/package.json index 40aa0ba2..b309b106 100644 --- a/app/package.json +++ b/app/package.json @@ -9,8 +9,9 @@ "email": "jeannot@ammd.net" }, "dependencies": { + "vmodule":"1.0.1", "electron-localshortcut":"0.6.1", - "node-osc": "2.0.3", + "osc": "2.0.3", "yargs": "5.0.0", "socket.io": "1.4.8", "socket.io-client": "1.4.8", diff --git a/app/resources/docs/index.md b/app/resources/docs/index.md index 48350dd8..8d26a29e 100644 --- a/app/resources/docs/index.md +++ b/app/resources/docs/index.md @@ -35,7 +35,7 @@ It relies on the use of several libraries : - [Node.js](https://nodejs.org/) - [Electron](http://electron.atom.io/) -- [node-osc](https://github.com/TheAlphaNerd/node-osc) +- [osc.js](https://github.com/colinbdclark/osc.js) - [socket.io](http://socket.io) - [browserify](http://browserify.org) - [express](http://expressjs.com)