OK, I've got a basic (ugly) prototype Custom Module working. Thank you for the suggestions.
Here it is for the curious:
// Import net and event-stream modules.
var net = nativeRequire('net');
var es = nativeRequire('event-stream')
// Class to create a JSON-RPC TCP client.
function Conn (name, host, port){
this.name = name;
this.host = host;
this.port = port;
var Addr = {
host: this.host,
port: this.port
}
// Create TCP client.
var client = net.createConnection(Addr, function () {
console.log('Connection name : ' + this.name);
console.log('Connection local address : ' + client.localAddress + ":" + client.localPort);
console.log('Connection remote address : ' + client.remoteAddress + ":" + client.remotePort);
})
//Report changes of state and errors.
client.on('end',function () {
console.log(this.name,'Client socket disconnect. ');
});
client.on('timeout', function () {
console.log(this.name,'Client connection timeout. ');
});
client.on('error', function (err) {
console.error(this.name,JSON.stringify(err));
});
return client;
}
Cnotify = function(client, methd, prams) {
var mesg={
jsonrpc:"2.0",
method: methd,
params: prams,
}
client.write(JSON.stringify(mesg)+'\n');
}
Ccall = function (client, Id, methd, prams) {
var mesg={
jsonrpc:"2.0",
id: Id,
method: methd,
params: prams,
}
client.write(JSON.stringify(mesg)+'\n');
}
Osc2rpc = function(data) {
var {address, args, host, port, clientId} = data
dotrpckey = address.replace(/\//g,'.');
rpckey = dotrpckey.substr(1, dotrpckey.length);
rpcvalue = args[0].value;
rpcparam = {
params:[rpckey, rpcvalue],
};
return rpcparam
}
Rpc2osc = function(data) {
for (i = 0; i < data.params.length; i+=2) {
osckey = data.params[i].replace(/\./g,'/');
oscout = data.params[i+1];
receive('localhost', 7000, '/'+osckey, oscout);
}
}
// Create a guitarix client socket.
gxClient = new Conn('guitarix', 'localhost', 7000);
// Receive stream from server and process as newline sperated JSON-RPC objects
gxClient
.pipe(es.split(null, null, {trailing: false}))
.pipe(es.parse())
.on('data', function (data) {
//console.log(data);
if (data.id === "pList") {
pListArray = data.result;
console.log('pListArray populated');
console.log('pListArray size = ' + pListArray.length)
} else {
if (('result' in data) && (typeof data.result === 'object')) {
//console.log(data);
for ( var key in data.result) {
osckey = key.replace(/\./g,'/');
console.log('/' + osckey,' ',data.result[key]);
}
}
if (('params' in data) && !(data.params[0].includes('.v'))) {
console.log(data);
Rpc2osc(data);
}
}
})
module.exports = {
oscOutFilter:function(data){
// Filter outgoing osc messages
rpc = Osc2rpc(data);
Cnotify(gxClient, 'set', rpc.params);
return
},
}
Cnotify(gxClient, 'listen', ["all"]);