Removed debug logging

Added verification of broadcast response message size
Added clearer handling of message types

I'm sure there's a better way to do debug logging, but I don't really feel like adding a dependency on another module just for that.
This commit is contained in:
Parnic
2018-03-30 20:35:24 -05:00
parent ce41d6b009
commit 0bbae0446b
8 changed files with 114 additions and 57 deletions

View File

@ -11,7 +11,7 @@ class FindUnits extends EventEmitter {
this.finder.on('message', function (message, remote) { this.finder.on('message', function (message, remote) {
_this.foundServer(message, remote); _this.foundServer(message, remote);
}).on('close', function() { }).on('close', function() {
console.log('finder closed'); //console.log('finder closed');
}); });
} }
@ -25,7 +25,8 @@ class FindUnits extends EventEmitter {
} }
foundServer(message, remote) { foundServer(message, remote) {
console.log('Found something'); //console.log('Found something');
if (message.length >= 40) {
var server = { var server = {
address: remote.address, address: remote.address,
type: message.readInt32LE(0), type: message.readInt32LE(0),
@ -35,17 +36,18 @@ class FindUnits extends EventEmitter {
gatewayName: message.toString('utf8', 12, 28) gatewayName: message.toString('utf8', 12, 28)
}; };
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) {
this.emit('serverFound', server); this.emit('serverFound', server);
} }
} }
}
sendServerBroadcast() { sendServerBroadcast() {
var message = new Uint8Array(8); var message = new Uint8Array(8);
message[0] = 1; message[0] = 1;
this.finder.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...");
} }
close() { close() {
@ -63,7 +65,7 @@ class UnitConnection extends EventEmitter {
this.client.on('data', function(msg) { this.client.on('data', function(msg) {
_this.onClientMessage(msg); _this.onClientMessage(msg);
}).on('close', function(had_error) { }).on('close', function(had_error) {
console.log('unit connection closed'); //console.log('unit connection closed');
}); });
} }
@ -72,7 +74,7 @@ class UnitConnection extends EventEmitter {
} }
connect() { connect() {
console.log("connecting..."); //console.log("connecting...");
var _this = this; var _this = this;
this.client.connect(this.server.port, this.server.address, function() { this.client.connect(this.server.port, this.server.address, function() {
_this.onConnected(); _this.onConnected();
@ -80,78 +82,91 @@ class UnitConnection extends EventEmitter {
} }
onConnected() { onConnected() {
console.log('connected'); //console.log('connected');
console.log('sending init message...'); //console.log('sending init message...');
this.client.write('CONNECTSERVERHOST\r\n\r\n'); this.client.write('CONNECTSERVERHOST\r\n\r\n');
console.log('sending challenge message...'); //console.log('sending challenge message...');
this.client.write(new messages.SLChallengeMessage().toBuffer()); this.client.write(new messages.SLChallengeMessage().toBuffer());
} }
login() { login() {
console.log('sending login message...'); //console.log('sending login message...');
this.client.write(new messages.SLLoginMessage().toBuffer()); this.client.write(new messages.SLLoginMessage().toBuffer());
} }
getPoolStatus() { getPoolStatus() {
console.log('sending pool status query...'); //console.log('sending pool status query...');
this.client.write(new messages.SLPoolStatusMessage().toBuffer()); this.client.write(new messages.SLPoolStatusMessage().toBuffer());
} }
getControllerConfig() { getControllerConfig() {
console.log('sending controller config query...'); //console.log('sending controller config query...');
this.client.write(new messages.SLControllerConfigMessage().toBuffer()); this.client.write(new messages.SLControllerConfigMessage().toBuffer());
} }
getChemicalData() { getChemicalData() {
console.log('sending chemical data query...'); //console.log('sending chemical data query...');
this.client.write(new messages.SLChemDataMessage().toBuffer()); this.client.write(new messages.SLChemDataMessage().toBuffer());
} }
getSaltCellConfig() { getSaltCellConfig() {
console.log('sending salt cell config query...'); //console.log('sending salt cell config query...');
this.client.write(new messages.SLSaltCellConfigMessage().toBuffer()); this.client.write(new messages.SLSaltCellConfigMessage().toBuffer());
} }
getVersion() { getVersion() {
console.log('sending version query...'); //console.log('sending version query...');
this.client.write(new messages.SLVersionMessage().toBuffer()); this.client.write(new messages.SLVersionMessage().toBuffer());
} }
onClientMessage(msg) { onClientMessage(msg) {
console.log('received message of length ' + msg.length); //console.log('received message of length ' + msg.length);
if (msg.length < 4) {
return;
}
var msgType = msg.readInt16LE(2); var msgType = msg.readInt16LE(2);
if (msgType === 15) { switch (msgType) {
console.log(" it's a challenge response"); case messages.SLChallengeMessage.getResponseId():
//console.log(" it's a challenge response");
this.login(); this.login();
} else if (msgType === 28) { break;
console.log(" it's a login response"); case messages.SLLoginMessage.getResponseId():
//console.log(" it's a login response");
this.emit('loggedIn'); this.emit('loggedIn');
} else if (msgType === 12527) { break;
console.log(" it's pool status"); case messages.SLPoolStatusMessage.getResponseId():
//console.log(" it's pool status");
this.emit('poolStatus', new messages.SLPoolStatusMessage(msg)); this.emit('poolStatus', new messages.SLPoolStatusMessage(msg));
} else if (msgType === 12533) { break;
console.log(" it's controller configuration"); case messages.SLControllerConfigMessage.getResponseId():
//console.log(" it's controller configuration");
this.emit('controllerConfig', new messages.SLControllerConfigMessage(msg)); this.emit('controllerConfig', new messages.SLControllerConfigMessage(msg));
} else if (msgType === 12593) { break;
console.log(" it's chem data"); case messages.SLChemDataMessage.getResponseId():
//console.log(" it's chem data");
this.emit('chemicalData', new messages.SLChemDataMessage(msg)); this.emit('chemicalData', new messages.SLChemDataMessage(msg));
} else if (msgType === 12573) { break;
console.log(" it's salt cell config"); case messages.SLSaltCellConfigMessage.getResponseId():
//console.log(" it's salt cell config");
this.emit('saltCellConfig', new messages.SLSaltCellConfigMessage(msg)); this.emit('saltCellConfig', new messages.SLSaltCellConfigMessage(msg));
} else if (msgType === 8121) { break;
console.log(" it's version"); case messages.SLVersionMessage.getResponseId():
//console.log(" it's version");
this.emit('version', new messages.SLVersionMessage(msg)); this.emit('version', new messages.SLVersionMessage(msg));
} else { break;
console.log(" it's unknown. type: " + msgType); default:
//console.log(" it's unknown. type: " + msgType);
break;
} }
} }
} }
/* debug print full buffer contents: /* debug print full buffer contents:
for (const value of buf.values()) { for (const value of buf.values()) {
console.log(value.toString(16)); //console.log(value.toString(16));
} }
*/ */

View File

@ -1,7 +1,13 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 14;
exports.SLChallengeMessage = class SLChallengeMessage extends SLMessage { exports.SLChallengeMessage = class SLChallengeMessage extends SLMessage {
constructor() { constructor() {
super(0, 14); super(0, MSG_ID);
}
static getResponseId() {
return MSG_ID + 1;
} }
} }

View File

@ -1,8 +1,10 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12592;
exports.SLChemDataMessage = class SLChemDataMessage extends SLMessage { exports.SLChemDataMessage = class SLChemDataMessage extends SLMessage {
constructor(buf) { constructor(buf) {
super(0, 12592); super(0, MSG_ID);
if (!buf) { if (!buf) {
this.writeInt32LE(0); // controller index this.writeInt32LE(0); // controller index
} else { } else {
@ -45,4 +47,8 @@ exports.SLChemDataMessage = class SLChemDataMessage extends SLMessage {
this.error = (salt & 128) !== 0; this.error = (salt & 128) !== 0;
} }
} }
static getResponseId() {
return MSG_ID + 1;
}
} }

View File

@ -1,8 +1,10 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12532;
exports.SLControllerConfigMessage = class SLControllerConfigMessage extends SLMessage { exports.SLControllerConfigMessage = class SLControllerConfigMessage extends SLMessage {
constructor(buf) { constructor(buf) {
super(0, 12532); super(0, MSG_ID);
if (!buf) { if (!buf) {
this.writeInt32LE(0); this.writeInt32LE(0);
this.writeInt32LE(0); this.writeInt32LE(0);
@ -74,4 +76,8 @@ exports.SLControllerConfigMessage = class SLControllerConfigMessage extends SLMe
this.interfaceTabFlags = this.readInt32LE(); this.interfaceTabFlags = this.readInt32LE();
this.showAlarms = this.readInt32LE(); this.showAlarms = this.readInt32LE();
} }
static getResponseId() {
return MSG_ID + 1;
}
} }

View File

@ -1,12 +1,18 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 27;
exports.SLLoginMessage = class SLLoginMessage extends SLMessage { exports.SLLoginMessage = class SLLoginMessage extends SLMessage {
constructor() { constructor() {
super(0, 27); super(0, MSG_ID);
this.writeInt32LE(348); // schema this.writeInt32LE(348); // schema
this.writeInt32LE(0); // connection type this.writeInt32LE(0); // connection type
this.writeSLString('node-screenlogic'); // version this.writeSLString('node-screenlogic'); // version
this.writeSLBuffer(Buffer.alloc(16)); // encoded password. empty/unused for local connections this.writeSLBuffer(Buffer.alloc(16)); // encoded password. empty/unused for local connections
this.writeInt32LE(2); // procID this.writeInt32LE(2); // procID
} }
static getResponseId() {
return MSG_ID + 1;
}
} }

View File

@ -1,11 +1,13 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12526;
const SPA_CIRCUIT_ID = 500; const SPA_CIRCUIT_ID = 500;
const POOL_CIRCUIT_ID = 505; const POOL_CIRCUIT_ID = 505;
exports.SLPoolStatusMessage = class SLPoolStatusMessage extends SLMessage { exports.SLPoolStatusMessage = class SLPoolStatusMessage extends SLMessage {
constructor(buf) { constructor(buf) {
super(0, 12526); super(0, MSG_ID);
if (!buf) { if (!buf) {
this.writeInt32LE(0); this.writeInt32LE(0);
} else { } else {
@ -98,4 +100,8 @@ exports.SLPoolStatusMessage = class SLPoolStatusMessage extends SLMessage {
} }
} }
} }
static getResponseId() {
return MSG_ID + 1;
}
} }

View File

@ -1,8 +1,10 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12572;
exports.SLSaltCellConfigMessage = class SLSaltCellConfigMessage extends SLMessage { exports.SLSaltCellConfigMessage = class SLSaltCellConfigMessage extends SLMessage {
constructor(buf) { constructor(buf) {
super(0, 12572); super(0, MSG_ID);
if (!buf) { if (!buf) {
this.writeInt32LE(0); // controller index this.writeInt32LE(0); // controller index
} else { } else {
@ -24,4 +26,8 @@ exports.SLSaltCellConfigMessage = class SLSaltCellConfigMessage extends SLMessag
this.flags = this.readInt32LE(); this.flags = this.readInt32LE();
this.superChlorTimer = this.readInt32LE(); this.superChlorTimer = this.readInt32LE();
} }
static getResponseId() {
return MSG_ID + 1;
}
} }

View File

@ -1,8 +1,10 @@
const SLMessage = require('./SLMessage.js').SLMessage; const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 8120;
exports.SLVersionMessage = class SLVersionMessage extends SLMessage { exports.SLVersionMessage = class SLVersionMessage extends SLMessage {
constructor(buf) { constructor(buf) {
super(0, 8120); super(0, MSG_ID);
if (buf) { if (buf) {
this._wroteSize = true; this._wroteSize = true;
this.writeBuffer(buf, 0); this.writeBuffer(buf, 0);
@ -16,4 +18,8 @@ exports.SLVersionMessage = class SLVersionMessage extends SLMessage {
this.version = this.readSLString(); this.version = this.readSLString();
} }
static getResponseId() {
return MSG_ID + 1;
}
} }