Day mask helper functions / Refactor of time functions (#31)

* added helper methods for decode/encode dayMask

* moved isBitSet to SLMessage.js as this method is needed for several message types

* Refactored reusable functions to SLMessage
This commit is contained in:
Bruce Sheplan
2020-06-16 21:22:22 -05:00
committed by GitHub
parent 1aea09d95e
commit addd89bec5
2 changed files with 60 additions and 14 deletions

View File

@ -14,7 +14,6 @@ exports.SLGetScheduleData = class SLGetScheduleData extends SLMessage {
if (!buf) { if (!buf) {
// console.log('Requested Schedule type = ', scheduleType);
this.writeInt32LE(0); this.writeInt32LE(0);
this.writeInt32LE(scheduleType); this.writeInt32LE(scheduleType);
} else { } else {
@ -37,26 +36,16 @@ exports.SLGetScheduleData = class SLGetScheduleData extends SLMessage {
this.events[i].scheduleId = this.readUInt32LE(); this.events[i].scheduleId = this.readUInt32LE();
this.events[i].circuitId = this.readUInt32LE(); this.events[i].circuitId = this.readUInt32LE();
this.events[i].startTime = this.readTime(this.readUInt32LE()); this.events[i].startTime = this.decodeTime(this.readUInt32LE());
this.events[i].stopTime = this.readTime(this.readUInt32LE()); this.events[i].stopTime = this.decodeTime(this.readUInt32LE());
this.events[i].dayMask = this.readUInt32LE(); this.events[i].dayMask = this.readUInt32LE();
this.events[i].flags = this.readUInt32LE(); this.events[i].flags = this.readUInt32LE();
this.events[i].heatCmd = this.readUInt32LE(); this.events[i].heatCmd = this.readUInt32LE();
this.events[i].heatSetPoint = this.readUInt32LE(); this.events[i].heatSetPoint = this.readUInt32LE();
this.events[i].days = this.decodeDayMask(this.events[i].dayMask);
} }
} }
readTime(rawTime) {
var retVal;
retVal = Math.floor(rawTime / 60) * 100 + rawTime % 60;
retVal = String(retVal).padStart(4, '0');
return retVal;
}
static getResponseId() { static getResponseId() {
return MSG_ID + 1; return MSG_ID + 1;
} }

View File

@ -2,6 +2,16 @@
const SmartBuffer = require('smart-buffer').SmartBuffer; const SmartBuffer = require('smart-buffer').SmartBuffer;
const DAY_VALUES = [
['Mon', 0x1 ],
['Tue', 0x2 ],
['Wed', 0x4 ],
['Thu', 0x8 ],
['Fri', 0x10 ],
['Sat', 0x20 ],
['Sun', 0x40 ],
];
exports.SLMessage = class SLMessage extends SmartBuffer { exports.SLMessage = class SLMessage extends SmartBuffer {
constructor(senderId, messageId, size) { constructor(senderId, messageId, size) {
var options; var options;
@ -93,6 +103,53 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
this.dataLength = this.readInt32LE(); this.dataLength = this.readInt32LE();
} }
isBitSet(value, bit) {
return ((value >> bit) & 0x1) === 1;
}
decodeTime(rawTime) { // Takes 'rawTime' in mins past midnight and returns military time as a string
var retVal;
retVal = Math.floor(rawTime / 60) * 100 + rawTime % 60;
retVal = String(retVal).padStart(4, '0');
return retVal;
}
encodeTime(stringTime) { // Takes 'stringTime' as military time and returns mins past midnight
return Number(stringTime.substr(0, 2) * 60) + Number(stringTime.substr(2, 2));
}
decodeDayMask(dayMask) {
var days = [];
for (var i = 0; i < 7; i++) {
if (this.isBitSet(dayMask, i)) {
days.push(DAY_VALUES[i][0]);
}
}
return days;
}
encodeDayMask(daysArray) {
var dayMask = 0;
for (var i = 0; i < daysArray.length; i++) {
dayMask += this.getDayValue(daysArray[i]);
}
return dayMask;
}
getDayValue(dayName) {
for (var i = 0; i < DAY_VALUES.length; i++) {
if (DAY_VALUES[i][0] === dayName) {
return DAY_VALUES[i][1];
}
}
return 0;
}
static slackForAlignment(val) { static slackForAlignment(val) {
return (4 - val % 4) % 4; return (4 - val % 4) % 4;
} }