Add support for specifying the sender id to each call

This parameter is optional, so compatibility shouldn't be affected. Each SLMessage can set its own sender ID which is present on the returned message. This allows callers to fire multiple requests at once, even of the same type, while being able to identify which response went with which request. If not specified, the default value is 0.

Also went ahead and documented some of the helper functions present on SLMessage (so, available on all message instances).

Finally, since I was in and messing with each message anyway, I simplified and removed some repeated code from each derived message and had it call into the super to take advantage of shared decoding functionality.

The lambdas ("arrow functions") in test functions were removed per advice from Mocha's documentation where the implicit `this` rebinding can apparently cause problems. This should probably have been its own commit, but, again, I was already in there messing with stuff, so...oh well.

Closes #43
This commit is contained in:
2020-08-11 22:20:58 -05:00
parent ab36d17a38
commit 89b8775ce3
27 changed files with 254 additions and 267 deletions

View File

@ -5,15 +5,15 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12522;
exports.SLAddClient = class SLAddClient extends SLMessage {
constructor(senderId) {
super(0, MSG_ID);
constructor(clientId, senderId) {
super(senderId, MSG_ID);
this.senderId = senderId;
this.clientId = clientId;
}
encode() {
this.writeInt32LE(0);
this.writeInt32LE(this.senderId);
this.writeInt32LE(this.clientId);
super.encode();
}

View File

@ -6,23 +6,15 @@ const MSG_ID = 12544;
exports.SLAddNewScheduleEvent = class SLAddNewScheduleEvent extends SLMessage {
constructor(buf, scheduleType) {
var size;
constructor(buf, scheduleType, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
super(senderId, MSG_ID);
if (!buf) {
// console.log('Requested Schedule type = ', scheduleType);
this.writeInt32LE(0);
this.writeInt32LE(scheduleType);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
this.decode();
}
}

View File

@ -5,9 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12580;
exports.SLCancelDelay = class SLCancelDelay extends SLMessage {
constructor() {
super(0, MSG_ID);
constructor(senderId) {
super(senderId, MSG_ID);
}
encode() {

View File

@ -5,20 +5,14 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12592;
exports.SLChemDataMessage = class SLChemDataMessage extends SLMessage {
constructor(buf) {
var size;
constructor(buf, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
if (!buf) {
this.writeInt32LE(0); // controller index
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
super(senderId, MSG_ID);
this.decode();
this.writeInt32LE(0); // controller index
}
}

View File

@ -13,21 +13,15 @@ const CIRCUIT_NAME_VALUE_MAP = [
];
exports.SLControllerConfigMessage = class SLControllerConfigMessage extends SLMessage {
constructor(buf) {
var size;
constructor(buf, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
if (!buf) {
this.writeInt32LE(0);
this.writeInt32LE(0);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
super(senderId, MSG_ID);
this.decode();
this.writeInt32LE(0);
this.writeInt32LE(0);
}
}

View File

@ -5,8 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12546;
exports.SLDeleteScheduleEventById = class SLDeleteScheduleEventById extends SLMessage {
constructor(scheduleId) {
super(0, MSG_ID);
constructor(scheduleId, senderId) {
super(senderId, MSG_ID);
this.writeInt32LE(0);
this.writeInt32LE(scheduleId);

View File

@ -5,21 +5,15 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12566;
exports.SLEquipmentConfigurationMessage = class SLEquipmentConfigurationMessage extends SLMessage {
constructor(buf) {
var size;
constructor(buf, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
if (!buf) {
this.writeInt32LE(0);
this.writeInt32LE(0);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
super(senderId, MSG_ID);
this.decode();
this.writeInt32LE(0);
this.writeInt32LE(0);
}
}

View File

@ -5,21 +5,15 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12584;
exports.SLGetPumpStatus = class SLGetPumpStatus extends SLMessage {
constructor(buf, pumpId) {
var size;
constructor(buf, pumpId, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
super(senderId, MSG_ID);
if (!buf) {
this.writeInt32LE(0);
this.writeInt32LE(pumpId);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
this.decode();
}
}

View File

@ -5,22 +5,15 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12542;
exports.SLGetScheduleData = class SLGetScheduleData extends SLMessage {
constructor(buf, scheduleType) {
var size;
constructor(buf, scheduleType, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
super(senderId, MSG_ID);
if (!buf) {
this.writeInt32LE(0);
this.writeInt32LE(scheduleType);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
this.decode();
}
}

View File

@ -5,8 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12556;
exports.SLLightControl = class SLLightControl extends SLMessage {
constructor(controllerIndex, command) {
super(0, MSG_ID);
constructor(controllerIndex, command, senderId) {
super(senderId, MSG_ID);
this.controllerIndex = controllerIndex;
this.command = command;

View File

@ -27,7 +27,7 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
this.writeUInt16LE(messageId || 0);
this._wroteSize = false;
} else if (senderId) {
} else if (typeof senderId === 'object') {
this._wroteSize = true;
var buffer = senderId;
this.writeBuffer(buffer, 0);

View File

@ -9,20 +9,14 @@ const SPA_CIRCUIT_ID = 500;
const POOL_CIRCUIT_ID = 505;
exports.SLPoolStatusMessage = class SLPoolStatusMessage extends SLMessage {
constructor(buf) {
var size;
constructor(buf, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
if (!buf) {
this.writeInt32LE(0);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
super(senderId, MSG_ID);
this.decode();
this.writeInt32LE(0);
}
}

View File

@ -5,15 +5,15 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12524;
exports.SLRemoveClient = class SLRemoveClient extends SLMessage {
constructor(senderId) {
super(0, MSG_ID);
constructor(clientId, senderId) {
super(senderId, MSG_ID);
this.senderId = senderId;
this.clientId = clientId;
}
encode() {
this.writeInt32LE(0);
this.writeInt32LE(this.senderId);
this.writeInt32LE(this.clientId);
super.encode();
}

View File

@ -5,20 +5,14 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12572;
exports.SLSaltCellConfigMessage = class SLSaltCellConfigMessage extends SLMessage {
constructor(buf) {
var size;
constructor(buf, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
if (!buf) {
this.writeInt32LE(0); // controller index
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
super(senderId, MSG_ID);
this.decode();
this.writeInt32LE(0); // controller index
}
}

View File

@ -6,8 +6,8 @@ const MSG_ID = 12550;
exports.SLSetCircuitRuntimeById = class SLSetCircuitRuntimeById extends SLMessage {
constructor(circuitId, runTime) {
super(0, MSG_ID);
constructor(circuitId, runTime, senderId) {
super(senderId, MSG_ID);
this.circuitId = circuitId;
this.runTime = runTime;

View File

@ -5,8 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12530;
exports.SLSetCircuitStateMessage = class SLSetCircuitStateMessage extends SLMessage {
constructor(controllerId, circuitId, circuitState) {
super(0, MSG_ID);
constructor(controllerId, circuitId, circuitState, senderId) {
super(senderId, MSG_ID);
this.controllerId = controllerId;
this.circuitId = circuitId;

View File

@ -5,8 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12538;
exports.SLSetHeatMode = class SLSetHeatMode extends SLMessage {
constructor(controllerIndex, bodyType, heatMode) {
super(0, MSG_ID);
constructor(controllerIndex, bodyType, heatMode, senderId) {
super(senderId, MSG_ID);
this.controllerIndex = controllerIndex;
this.bodyType = bodyType;

View File

@ -5,8 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12528;
exports.SLSetHeatSetPoint = class SLSetHeatSetPoint extends SLMessage {
constructor(controllerIndex, bodyType, temperature) {
super(0, MSG_ID);
constructor(controllerIndex, bodyType, temperature, senderId) {
super(senderId, MSG_ID);
this.controllerIndex = controllerIndex;
this.bodyType = bodyType;

View File

@ -5,8 +5,9 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12586;
exports.SLSetPumpFlow = class SLSetPumpFlow extends SLMessage {
constructor(pumpId, circuitId, setPoint, isRPMs) {
super(0, MSG_ID);
constructor(pumpId, circuitId, setPoint, isRPMs, senderId) {
super(senderId, MSG_ID);
this.pumpId = pumpId;
this.circuitId = circuitId;
this.setPoint = setPoint;

View File

@ -5,8 +5,8 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 12576;
exports.SLSetSaltCellConfigMessage = class SLSetSaltCellConfigMessage extends SLMessage {
constructor(controllerIndex, poolOutput, spaOutput) {
super(0, MSG_ID);
constructor(controllerIndex, poolOutput, spaOutput, senderId) {
super(senderId, MSG_ID);
this.controllerIndex = controllerIndex;
this.poolOutput = poolOutput;

View File

@ -6,15 +6,13 @@ const MSG_ID = 12548;
exports.SLSetScheduleEventById = class SLSetScheduleEventById extends SLMessage {
constructor(buf, scheduleId, circuitId, startTime, stopTime, dayMask, flags, heatCmd, heatSetPoint) {
var size;
constructor(buf, scheduleId, circuitId, startTime, stopTime, dayMask, flags, heatCmd, heatSetPoint, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
super(senderId, MSG_ID);
if (!buf) {
this.writeInt32LE(0);
this.writeInt32LE(scheduleId);
this.writeInt32LE(circuitId);
@ -24,11 +22,6 @@ exports.SLSetScheduleEventById = class SLSetScheduleEventById extends SLMessage
this.writeInt32LE(flags);
this.writeInt32LE(heatCmd);
this.writeInt32LE(heatSetPoint);
} else {
this._wroteSize = true;
this.writeBuffer(buf, 0);
this.decode();
}
}

View File

@ -5,18 +5,12 @@ const SLMessage = require('./SLMessage.js').SLMessage;
const MSG_ID = 8120;
exports.SLVersionMessage = class SLVersionMessage extends SLMessage {
constructor(buf) {
var size;
constructor(buf, senderId) {
if (buf) {
size = buf.readInt32LE(4) + 8;
}
super(0, MSG_ID, size);
if (buf) {
this._wroteSize = true;
this.writeBuffer(buf, 0);
this.decode();
var size = buf.readInt32LE(4) + 8;
super(buf, MSG_ID, size);
} else {
super(senderId, MSG_ID);
}
}