From 9aec54e486bf2933228b465175ee9cb3f4591294 Mon Sep 17 00:00:00 2001 From: patrickgilsf Date: Sat, 6 Apr 2024 11:39:20 -0700 Subject: [PATCH] modified incoming data strucutre so that multple packets of data are concatenated and parsed into usable JSON. TCP streams max out at 64KB, so large get functions wouldn't return usable data, just console logging. See https://github.com/patrickgilsf/ide_qsys for similar repo with different intentions --- main.js | 21 ++++++++++------ qrc.js | 77 +++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 32 deletions(-) diff --git a/main.js b/main.js index ce4afcf..9aa9ecd 100644 --- a/main.js +++ b/main.js @@ -1,12 +1,17 @@ var qrc = require('./qrc'); -var i = 0; -qrc.Connect('10.0.0.11') - .then(function() { + +const test = async () => { + qrc.Connect('192.168.42.148') + .then(async function() { console.log('Connected!'); - setInterval(function() { - i = (i+0.01)%1 - qrc.Call('Control.Set', {"Name": "source_level", "Position": i }); - }, 0.1); - }); \ No newline at end of file + qrc.Logon("QSC","5678"); + console.log(await qrc.Component.GetComponents()); + }); +}; + +test(); + + + diff --git a/qrc.js b/qrc.js index 8062dfa..7a00688 100644 --- a/qrc.js +++ b/qrc.js @@ -2,39 +2,68 @@ var net = require('net'); var client = new net.Socket(); var client_connected = false; +client.setEncoding('utf8'); var messageIndex = 0; var responseQueue = {}; +const nt = "\x00" +var rtnBuffer = ""; +let rtn; + +const endOfResponse = (input) => input[input.length - 2] == "}"; // Handle server responses client.on('data', function(data) { - console.log(data.toString()); return; - - // Parse - data = data.toString().slice(0, -1); // remove null - data = JSON.parse(data); // parse JSON - - // Check ID exists - if(!data.id) { console.error('No ID present on JSONRPC message!', data); return; } - - // Get callback - var cb = responseQueue[data.id]; - if(!cb) { console.error('No callback for received message!', data); return; } - - if(data.result) { - cb(data.result, null); // call with no error - } else if(data.error) { - cb(null, data.error) + //log real time data stream to console + // console.log(data.toString()); + + if (endOfResponse(data)) { + if (rtnBuffer != "") { + rtnBuffer += data; + for (let str of rtnBuffer.split(nt)) { + try { + str && JSON.parse(str).result ? rtn = JSON.parse(str) : null; + } catch (e) { + console.log(`error parsing JSON with ${e}, on this string:\n\n${str}`); + } + } + } } else { - cb(null, { - code: -1, - message: 'Invalid response from server.' - }); + rtnBuffer += data + // console.log(rtnBuffer) + } + + // rtn ? console.log(rtn) : null + + if (rtn) { + // console.log(rtn); + //changing "data" to return to copy op's rest of code + data = rtn; + // Check ID exists + if(!data.id) { console.error('No ID present on JSONRPC message!', data); return; } + + // Get callback + var cb = responseQueue[data.id]; + if(!cb) { console.error('No callback for received message!', data); return; } + + if(data.result) { + cb(data.result, null); // call with no error + } else if(data.error) { + cb(null, data.error) + } else { + cb(null, { + code: -1, + message: 'Invalid response from server.' + }); + } } + return rtn; }); + + client.on('close', function() { console.log('Connection closed'); client_connected = false; @@ -70,7 +99,7 @@ function call(method, params) { "id": messageIndex }); - console.log(json); + // console.log(json); // Send request client.write(json+'\0'); @@ -119,7 +148,7 @@ module.exports = { }, }, 'Component': { - 'Get': function(name, controls) { + 'Get': async function(name, controls) { if(typeof controls != 'Array') { controls = [controls]; } // handle single-item requests controls = controls.map(function(n) { return { 'Name': n }; }); return call('Component.Get', { 'Name': name, 'Controls': controls }); @@ -199,7 +228,7 @@ module.exports = { // Zones if(typeof zones != 'Array' && typeof zones != 'object') { console.log(typeof zones); return; } - if(parseInt(zones[0]) != NaN) { options.Zones = zones; options.ZoneTags = []; } else { options.ZoneTags = zones; options.Zones = []; } + if(parsef(zones[0]) != NaN) { options.Zones = zones; options.ZoneTags = []; } else { options.ZoneTags = zones; options.Zones = []; } // Merge options for(i in user_options) {