Code cleanup
Much better organization and ease-of-use now that the proof of concept is established. Still an awful lot to do here, but I'm getting the hang of the whole node.js thing. Also moved the actual test functionality out to a test script so the module itself doesn't execute anything.
This commit is contained in:
204
index.js
204
index.js
@ -1,15 +1,69 @@
|
|||||||
var dgram = require('dgram');
|
var dgram = require('dgram');
|
||||||
var net = require('net');
|
var net = require('net');
|
||||||
|
const SmartBuffer = require('smart-buffer').SmartBuffer;
|
||||||
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
var server = dgram.createSocket("udp4");
|
class SLMessage extends SmartBuffer {
|
||||||
server.bind( function() {
|
constructor(senderId, messageId) {
|
||||||
server.setBroadcast(true)
|
super();
|
||||||
server.setMulticastTTL(128);
|
this.writeUInt16LE(senderId);
|
||||||
broadcastNew();
|
this.writeUInt16LE(messageId);
|
||||||
|
|
||||||
|
this._wroteSize = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
toBuffer() {
|
||||||
|
if (this._wroteSize === false) {
|
||||||
|
this.insertInt32LE(this.length - 4, 4);
|
||||||
|
this._wroteSize = true;
|
||||||
|
} else {
|
||||||
|
this.writeInt32LE(this.length - 8, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.toBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
writeSLString(str) {
|
||||||
|
this.writeInt32LE(str.length);
|
||||||
|
this.writeString(str);
|
||||||
|
this.skip(4 - (str.length % 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
writeSLBuffer(buf) {
|
||||||
|
this.writeInt32LE(buf.length);
|
||||||
|
this.writeBuffer(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
skip(num) {
|
||||||
|
if (num > 0) {
|
||||||
|
this.writeBuffer(Buffer.alloc(num));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FindUnits extends EventEmitter {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.finder = dgram.createSocket('udp4');
|
||||||
|
var _this = this;
|
||||||
|
this.finder.on('message', function (message, remote) {
|
||||||
|
_this.foundServer(message, remote);
|
||||||
|
}).on('close', function() {
|
||||||
|
console.log('finder closed');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
server.on('message', function (message, remote) {
|
search() {
|
||||||
console.log('Got a response.');
|
var _this = this;
|
||||||
|
this.finder.bind(function() {
|
||||||
|
_this.finder.setBroadcast(true);
|
||||||
|
_this.finder.setMulticastTTL(128);
|
||||||
|
_this.sendServerBroadcast();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
foundServer(message, remote) {
|
||||||
|
console.log('Found something');
|
||||||
var server = {
|
var server = {
|
||||||
address: remote.address,
|
address: remote.address,
|
||||||
type: message.readInt32LE(0),
|
type: message.readInt32LE(0),
|
||||||
@ -21,88 +75,112 @@ server.on('message', function (message, remote) {
|
|||||||
|
|
||||||
console.log(' type: ' + server.type + ', host: ' + server.address + ':' + server.port + ', identified as ' + server.gatewayName);
|
console.log(' type: ' + server.type + ', host: ' + server.address + ':' + server.port + ', identified as ' + server.gatewayName);
|
||||||
if (server.type === 2) {
|
if (server.type === 2) {
|
||||||
connectTo(server);
|
this.emit('serverFound', server);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
function broadcastNew() {
|
sendServerBroadcast() {
|
||||||
var message = new Uint8Array(8);
|
var message = new Uint8Array(8);
|
||||||
message[0] = 1;
|
message[0] = 1;
|
||||||
server.send(message, 0, message.length, 1444, "255.255.255.255");
|
this.finder.send(message, 0, message.length, 1444, "255.255.255.255");
|
||||||
console.log("Looking for ScreenLogic hosts...");
|
console.log("Looking for ScreenLogic hosts...");
|
||||||
}
|
}
|
||||||
|
|
||||||
function connectTo(server) {
|
close() {
|
||||||
|
this.finder.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class UnitConnection extends EventEmitter {
|
||||||
|
constructor(server) {
|
||||||
|
super();
|
||||||
|
this.server = server;
|
||||||
|
|
||||||
|
this.client = new net.Socket();
|
||||||
|
var _this = this;
|
||||||
|
this.client.on('data', function(msg) {
|
||||||
|
_this.onClientMessage(msg);
|
||||||
|
}).on('close', function(had_error) {
|
||||||
|
console.log('unit connection closed');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
this.client.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
connect() {
|
||||||
console.log("connecting...");
|
console.log("connecting...");
|
||||||
var client = new net.Socket();
|
var _this = this;
|
||||||
client.connect(server.port, server.address, function() {
|
this.client.connect(this.server.port, this.server.address, function() {
|
||||||
|
_this.onConnected();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onConnected() {
|
||||||
console.log('connected');
|
console.log('connected');
|
||||||
|
|
||||||
console.log('sending connection string...');
|
console.log('sending init message...');
|
||||||
var buf = Buffer.from('CONNECTSERVERHOST\r\n\r\n');
|
this.client.write('CONNECTSERVERHOST\r\n\r\n');
|
||||||
client.write(buf);
|
|
||||||
|
|
||||||
console.log('sending challenge string...');
|
console.log('sending challenge message...');
|
||||||
buf = Buffer.alloc(8);
|
var buf = new SLMessage(0, 14);
|
||||||
buf.writeUInt16LE(14, 2);
|
this.client.write(buf.toBuffer());
|
||||||
client.write(buf);
|
|
||||||
|
|
||||||
console.log('sending login string...');
|
|
||||||
buf = Buffer.alloc(72);
|
|
||||||
buf.writeUInt16LE(0);
|
|
||||||
buf.writeUInt16LE(27, 2);
|
|
||||||
buf.writeInt32LE(64, 4); // length
|
|
||||||
buf.writeInt32LE(348, 8); // schema
|
|
||||||
buf.writeInt32LE(0, 12); // connection type
|
|
||||||
var name = 'ScreenLogicConnect library';
|
|
||||||
buf.writeInt32LE(name.length, 16);
|
|
||||||
buf.write(name, 20);
|
|
||||||
var pos = 20 + name.length;
|
|
||||||
for (var i = 0; i < 4 - (name.length % 4); i++) {
|
|
||||||
buf.writeUInt8(0, 20 + name.length + i);
|
|
||||||
pos = pos + 1;
|
|
||||||
}
|
}
|
||||||
buf.writeInt32LE(16, pos);
|
|
||||||
pos += 4;
|
|
||||||
pos += 16;
|
|
||||||
buf.writeInt32LE(2, pos);
|
|
||||||
client.write(buf);
|
|
||||||
|
|
||||||
|
login() {
|
||||||
|
console.log('sending login message...');
|
||||||
|
var buf = new SLMessage(0, 27);
|
||||||
|
buf.writeInt32LE(348); // schema
|
||||||
|
buf.writeInt32LE(0); // connection type
|
||||||
|
buf.writeSLString('ScreenLogicConnect library'); // version
|
||||||
|
buf.writeSLBuffer(Buffer.alloc(16)); // encoded password. empty/unused for local connections
|
||||||
|
buf.writeInt32LE(2); // procID
|
||||||
|
this.client.write(buf.toBuffer());
|
||||||
|
}
|
||||||
|
|
||||||
|
getPoolStatus() {
|
||||||
console.log('sending pool status query...');
|
console.log('sending pool status query...');
|
||||||
buf = Buffer.alloc(12);
|
var buf = new SLMessage(0, 12526);
|
||||||
buf.writeUInt16LE(12526, 2);
|
buf.writeInt32LE(0);
|
||||||
buf.writeInt32LE(4, 4);
|
this.client.write(buf.toBuffer());
|
||||||
client.write(buf);
|
}
|
||||||
|
|
||||||
|
parsePoolStatus(msg) {
|
||||||
|
// todo: actually parse the message
|
||||||
|
this.emit('poolStatus', {});
|
||||||
|
}
|
||||||
|
|
||||||
|
getControllerConfig() {
|
||||||
console.log('sending controller config query...');
|
console.log('sending controller config query...');
|
||||||
buf = Buffer.alloc(16);
|
var buf = new SLMessage(0, 12532);
|
||||||
buf.writeUInt16LE(12532, 2);
|
buf.writeInt32LE(0);
|
||||||
buf.writeInt32LE(8, 4);
|
buf.writeInt32LE(0);
|
||||||
client.write(buf);
|
this.client.write(buf.toBuffer());
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(function() {
|
parseControllerConfig(msg) {
|
||||||
console.log('destroying');
|
// todo: actually parse the message
|
||||||
client.destroy();
|
this.emit('controllerConfig', {});
|
||||||
}, 3000);
|
}
|
||||||
});
|
|
||||||
|
|
||||||
client.on('close', function() {
|
onClientMessage(msg) {
|
||||||
console.log('closed');
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('data', function(msg) {
|
|
||||||
console.log('received message of length ' + msg.length);
|
console.log('received message of length ' + msg.length);
|
||||||
var msgType = msg.readInt16LE(2);
|
var msgType = msg.readInt16LE(2);
|
||||||
if (msgType === 15) {
|
if (msgType === 15) {
|
||||||
console.log(" it's a challenge response");
|
console.log(" it's a challenge response");
|
||||||
|
this.login();
|
||||||
} else if (msgType === 28) {
|
} else if (msgType === 28) {
|
||||||
console.log(" it's a login response");
|
console.log(" it's a login response");
|
||||||
|
this.emit('loggedIn');
|
||||||
} else if (msgType === 12527) {
|
} else if (msgType === 12527) {
|
||||||
console.log(" it's pool status");
|
console.log(" it's pool status");
|
||||||
|
this.parsePoolStatus(msg);
|
||||||
} else if (msgType === 12533) {
|
} else if (msgType === 12533) {
|
||||||
console.log(" it's controller configuration");
|
console.log(" it's controller configuration");
|
||||||
|
this.parseControllerConfig(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* debug print full buffer contents:
|
/* debug print full buffer contents:
|
||||||
@ -110,3 +188,9 @@ for (const value of buf.values()) {
|
|||||||
console.log(value.toString(16));
|
console.log(value.toString(16));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
FindUnits,
|
||||||
|
SLMessage,
|
||||||
|
UnitConnection
|
||||||
|
}
|
||||||
|
18
test.js
Executable file
18
test.js
Executable file
@ -0,0 +1,18 @@
|
|||||||
|
const ScreenLogic = require('./index');
|
||||||
|
|
||||||
|
var finder = new ScreenLogic.FindUnits();
|
||||||
|
finder.on('serverFound', function(server) {
|
||||||
|
var client = new ScreenLogic.UnitConnection(server);
|
||||||
|
client.on('loggedIn', function() {
|
||||||
|
this.getPoolStatus();
|
||||||
|
this.getControllerConfig();
|
||||||
|
}).on('poolStatus', function(status) {
|
||||||
|
}).on('controllerConfig', function(config) {
|
||||||
|
client.close();
|
||||||
|
finder.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
client.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
finder.search();
|
Reference in New Issue
Block a user