From 37d40b3386fa48cafd6d4c1422915698d690c0c8 Mon Sep 17 00:00:00 2001 From: Parnic Date: Mon, 6 Sep 2021 23:00:51 -0500 Subject: [PATCH] Add support for retrieving the current system date/time #56 --- README.md | 23 +++++++++++++++++++++++ index.js | 9 +++++++++ messages/SLGetSystemTime.js | 37 +++++++++++++++++++++++++++++++++++++ messages/index.js | 1 + 4 files changed, 70 insertions(+) create mode 100644 messages/SLGetSystemTime.js diff --git a/README.md b/README.md index 5780e5a..e0b054a 100755 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Tested with a Pentair ScreenLogic system on firmware versions 5.2 Build 736.0 Re * [SLCancelDelay](#slcanceldelay) * [SLAddClient](#sladdclient) * [SLRemoveClient](#slremoveclient) + * [SLGetSystemTime](#slgetsystemtime) ## Usage @@ -290,6 +291,10 @@ Registers to receive updates from controller when something changes. Takes a ran No longer receive `poolStatus` messages from controller. Emits the `removeClient` event when the request to remove a client is acknowledged. Takes a random number `clientId` that should match a previously registered client with `addClient`. `senderId` is an optional 16-bit integer and will be present as the `senderId` field on the returned message. +#### getSystemTime(senderId) + +Retrieves the current time the system is set to. Emits the `getSystemTime` event when response is received. `senderId` is an optional 16-bit integer and will be present as the `senderId` field on the returned message. + ### Events * `loggedIn` - Indicates that a connection to the server has been established and the login process completed. `get` methods can be called once this event has been emitted. @@ -314,6 +319,7 @@ No longer receive `poolStatus` messages from controller. Emits the `removeClient * `cancelDelay` - Indicates that a response to `cancelDelay()` has been received. Event handler receives a [`SLCancelDelay`](#slcanceldelay) object. * `addClient` - Indicates that a response to `addClient()` has been received. Event handler receives a [`SLAddClient`](#sladdclient) object. * `removeClient` - Indicates that a response to `removeClient()` has been received. Event handler receives a [`SLRemoveClient`](#slremoveclient) object. +* `getSystemTime` - Indicates that a response to `getSystemTime()` has been received. Event handler receives a [`SLGetSystemTime`](#slgetsystemtime) object. * `loginFailed` - Indicates that a remote login attempt via supplying a system address and password to `UnitConnection` has failed likely due to the incorrect password being used. * `badParameter` - Indicates that a bad parameter has been supplied to a function. This can be triggered, for example, by sending the wrong controller ID to a `set` function. * `error` - Indicates that an unhandled error was caught (such as the connection timing out) @@ -760,3 +766,20 @@ Passed as an argument to the emitted `addClient` event. ### SLRemoveClient Passed as an argument to the emitted `removeClient` event. + +### SLGetSystemTime + +Contains information about the system's current time and date. Passed as an argument to the emitted `getSystemTime` event. + +#### Properties + +* `year` - short representing current system year +* `month` - short representing current system month (where 1 is January, 2 is February, etc.) +* `day` - short representing current system day of the month +* `dayOfWeek` - short representing current system day of the week (where 0 is Monday and 6 is Sunday) +* `hour` - short representing current system hour (24-hour time where 0 is midnight, 13 is 1PM, etc.) +* `minute` - short representing current system minute +* `second` - short representing current system second +* `millisecond` - short representing current system millisecond +* `adjustForDST` - bool indicating whether the system should adjust for daylight saving time or not +* `date` - `Date` instance representing the current system datetime (convenience, constructed from the above properties) diff --git a/index.js b/index.js index 3ee2d62..88b2b12 100644 --- a/index.js +++ b/index.js @@ -297,6 +297,11 @@ class UnitConnection extends EventEmitter { this.client.write(new messages.SLRemoveClient(clientId, senderId).toBuffer()); } + getSystemTime(senderId) { + debugUnit('[%d] sending get system time query...', senderId || 0); + this.client.write(new messages.SLGetSystemTime(null, senderId).toBuffer()); + } + onClientMessage(msg) { debugUnit('received message of length %d', msg.length); if (msg.length < 4) { @@ -402,6 +407,10 @@ class UnitConnection extends EventEmitter { debugUnit(" it's async pool status"); this.emit('poolStatus', new messages.SLPoolStatusMessage(msg)); break; + case messages.SLGetSystemTime.getResponseId(): + debugUnit(" it's system time"); + this.emit('getSystemTime', new messages.SLGetSystemTime(msg)); + break; case 12501: debugUnit(" it's a schedule changed notification"); this.emit('scheduleChanged'); diff --git a/messages/SLGetSystemTime.js b/messages/SLGetSystemTime.js new file mode 100644 index 0000000..a1597ea --- /dev/null +++ b/messages/SLGetSystemTime.js @@ -0,0 +1,37 @@ +'use strict'; + +const SLMessage = require('./SLMessage.js').SLMessage; + +const MSG_ID = 8110; + +exports.SLGetSystemTime = class SLGetSystemTime extends SLMessage { + constructor(buf, senderId) { + if (buf) { + var size = buf.readInt32LE(4) + 8; + super(buf, MSG_ID, size); + } else { + super(senderId, MSG_ID); + } + } + + decode() { + super.decode(); + + this.year = this.readUInt16LE(); + this.month = this.readUInt16LE(); + this.dayOfWeek = this.readUInt16LE(); + this.day = this.readUInt16LE(); + this.hour = this.readUInt16LE(); + this.minute = this.readUInt16LE(); + this.second = this.readUInt16LE(); + this.millisecond = this.readUInt16LE(); + var adjustForDST = this.readInt32LE(); + this.adjustForDST = adjustForDST === 1; + + this.date = new Date(this.year, this.month - 1, this.day, this.hour, this.minute, this.second); + } + + static getResponseId() { + return MSG_ID + 1; + } +}; diff --git a/messages/index.js b/messages/index.js index e9dc7f1..cba8113 100644 --- a/messages/index.js +++ b/messages/index.js @@ -25,3 +25,4 @@ exports.SLSetPumpFlow = require('./SLSetPumpFlow.js').SLSetPumpFlow; exports.SLCancelDelay = require('./SLCancelDelay.js').SLCancelDelay; exports.SLAddClient = require('./SLAddClient.js').SLAddClient; exports.SLRemoveClient = require('./SLRemoveClient.js').SLRemoveClient; +exports.SLGetSystemTime = require('./SLGetSystemTime.js').SLGetSystemTime;