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.
This commit is contained in:
2021-09-06 22:45:41 -05:00
parent 37d40b3386
commit f271554d89
2 changed files with 32 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);
});
});