From f271554d89b9ce57a1c3d4c1916ebc3fa07fabc5 Mon Sep 17 00:00:00 2001 From: Parnic Date: Mon, 6 Sep 2021 22:45:41 -0500 Subject: [PATCH] Add DateTime writing support to SLMessages There may be some value in providing a read version of this as well, but it is comprised of so many properties that I'm leaving that out for now. If it becomes a need in the future, it will be straightforward to add. --- messages/SLMessage.js | 15 +++++++++++++++ test/slmessage.spec.js | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/messages/SLMessage.js b/messages/SLMessage.js index e049410..5fe3718 100644 --- a/messages/SLMessage.js +++ b/messages/SLMessage.js @@ -150,6 +150,21 @@ exports.SLMessage = class SLMessage extends SmartBuffer { return 0; } + writeSLDateTime(date) { + this.writeInt16LE(date.getFullYear()); + this.writeInt16LE(date.getMonth() + 1); + var dayOfWeek = date.getDay() - 1; + if (dayOfWeek < 0) { + dayOfWeek = 6; + } + this.writeInt16LE(dayOfWeek); + this.writeInt16LE(date.getDate()); + this.writeInt16LE(date.getHours()); + this.writeInt16LE(date.getMinutes()); + this.writeInt16LE(date.getSeconds()); + this.writeInt16LE(date.getMilliseconds()); + } + static slackForAlignment(val) { return (4 - val % 4) % 4; } diff --git a/test/slmessage.spec.js b/test/slmessage.spec.js index 58d9b1c..7fc5737 100644 --- a/test/slmessage.spec.js +++ b/test/slmessage.spec.js @@ -172,4 +172,21 @@ describe('SLMessage utilities', function() { assert.strictEqual(decodedMsg.dataLength, decodedMsg.readOffset - 8); } }); + + it('encodes Date as SLTime', function() { + let msg = new SLMessage(); + let date = new Date(2021, 8, 6, 22, 8, 5); + msg.writeSLDateTime(date); + let decodedMsg = new SLMessage(msg.toBuffer()); + assert.equal(decodedMsg.readUInt16LE(), 2021); + // javascript Date() month is 0-based, ScreenLogic month matches the calendar + assert.equal(decodedMsg.readUInt16LE(), 9); + // ScreenLogic day-of-week starts with Monday as 0 + assert.equal(decodedMsg.readUInt16LE(), 0); + assert.equal(decodedMsg.readUInt16LE(), 6); + assert.equal(decodedMsg.readUInt16LE(), 22); + assert.equal(decodedMsg.readUInt16LE(), 8); + assert.equal(decodedMsg.readUInt16LE(), 5); + assert.equal(decodedMsg.readUInt16LE(), 0); + }); });