Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
@@ -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);
});
qrc.Logon("QSC","5678");
console.log(await qrc.Component.GetComponents());
});
};

test();



77 changes: 53 additions & 24 deletions qrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +99,7 @@ function call(method, params) {
"id": messageIndex
});

console.log(json);
// console.log(json);

// Send request
client.write(json+'\0');
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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) {
Expand Down