Add reading of SLTimes

This commit is contained in:
2022-04-15 14:55:26 -05:00
parent 0db9e4945e
commit 77b034ce57
4 changed files with 41 additions and 12 deletions

View File

@ -779,16 +779,16 @@ Contains information about the system's current time and date. Passed as an argu
#### Properties #### Properties
* `date` - `Date` instance representing the current system datetime (preferred, the other properties are derived from this one and provided for backward compatibility purposes)
* `year` - short representing current system year * `year` - short representing current system year
* `month` - short representing current system month (where 1 is January, 2 is February, etc.) * `month` - short representing current system month (where 1 is January, 2 is February, etc.)
* `day` - short representing current system day of the month * `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) * `dayOfWeek` - short representing current system day of the week (where 0 is Sunday and 6 is Sunday)
* `hour` - short representing current system hour (24-hour time where 0 is midnight, 13 is 1PM, etc.) * `hour` - short representing current system hour (24-hour time where 0 is midnight, 13 is 1PM, etc.)
* `minute` - short representing current system minute * `minute` - short representing current system minute
* `second` - short representing current system second * `second` - short representing current system second
* `millisecond` - short representing current system millisecond * `millisecond` - short representing current system millisecond
* `adjustForDST` - bool indicating whether the system should adjust for daylight saving time or not * `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)
### SLSetSystemTime ### SLSetSystemTime

View File

@ -17,18 +17,18 @@ exports.SLGetSystemTime = class SLGetSystemTime extends SLMessage {
decode() { decode() {
super.decode(); super.decode();
this.year = this.readUInt16LE(); this.date = this.readSLDateTime();
this.month = this.readUInt16LE(); this.year = this.date.getFullYear();
this.dayOfWeek = this.readUInt16LE(); this.month = this.date.getMonth() + 1; // + 1 is for backward compatibility, SLTime represents months as 1-based
this.day = this.readUInt16LE(); this.dayOfWeek = this.date.getDay(); // should probably be tweaked to adjust what days are 0-6 as SLTime and Javascript start on different days of the week
this.hour = this.readUInt16LE(); this.day = this.date.getDate();
this.minute = this.readUInt16LE(); this.hour = this.date.getHours();
this.second = this.readUInt16LE(); this.minute = this.date.getMinutes();
this.millisecond = this.readUInt16LE(); this.second = this.date.getSeconds();
this.millisecond = this.date.getMilliseconds();
var adjustForDST = this.readInt32LE(); var adjustForDST = this.readInt32LE();
this.adjustForDST = adjustForDST === 1; this.adjustForDST = adjustForDST === 1;
this.date = new Date(this.year, this.month - 1, this.day, this.hour, this.minute, this.second);
} }
static getResponseId() { static getResponseId() {

View File

@ -165,6 +165,20 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
this.writeInt16LE(date.getMilliseconds()); this.writeInt16LE(date.getMilliseconds());
} }
readSLDateTime() {
let date = new Date();
date.setFullYear(this.readInt16LE());
date.setMonth(this.readInt16LE() - 1);
this.readInt16LE();
date.setDate(this.readInt16LE());
date.setHours(this.readInt16LE());
date.setMinutes(this.readInt16LE());
date.setSeconds(this.readInt16LE());
date.setMilliseconds(this.readInt16LE());
return date;
}
static slackForAlignment(val) { static slackForAlignment(val) {
return (4 - val % 4) % 4; return (4 - val % 4) % 4;
} }

View File

@ -189,4 +189,19 @@ describe('SLMessage utilities', function() {
assert.equal(decodedMsg.readUInt16LE(), 5); assert.equal(decodedMsg.readUInt16LE(), 5);
assert.equal(decodedMsg.readUInt16LE(), 0); assert.equal(decodedMsg.readUInt16LE(), 0);
}); });
it('decodes SLTime as Date', function() {
let msg = new SLMessage();
let date = new Date(2021, 8, 6, 22, 8, 5);
msg.writeSLDateTime(date);
let decodedMsg = new SLMessage(msg.toBuffer());
let decodedDate = decodedMsg.readSLDateTime();
assert.equal(date.getFullYear(), decodedDate.getFullYear());
assert.equal(date.getMonth(), decodedDate.getMonth());
assert.equal(date.getDate(), decodedDate.getDate());
assert.equal(date.getHours(), decodedDate.getHours());
assert.equal(date.getMinutes(), decodedDate.getMinutes());
assert.equal(date.getSeconds(), decodedDate.getSeconds());
assert.equal(date.getMilliseconds(), decodedDate.getMilliseconds());
});
}); });