knxultimate
Version:
KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.
94 lines • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const KnxLog_1 = require("../KnxLog");
const timeRegexp = /(\d{1,2}):(\d{1,2}):(\d{1,2})/;
const logger = (0, KnxLog_1.module)('DPT10');
const config = {
id: 'DPT10',
formatAPDU: (value) => {
const apdu_data = Buffer.alloc(3);
let dow;
let hour;
let minute;
let second;
switch (typeof value) {
case 'string':
{
const match = timeRegexp.exec(value);
if (match) {
dow = ((new Date().getDay() - 7) % 7) + 7;
hour = parseInt(match[1]);
minute = parseInt(match[2]);
second = parseInt(match[3]);
}
else {
logger.warn('invalid time format (%s)', value);
}
}
break;
case 'object':
if (value.constructor.name !== 'Date') {
logger.warn('Must supply a Date or String for DPT10 time');
break;
}
case 'number':
value = new Date(value);
default:
dow = ((value.getDay() - 7) % 7) + 7;
hour = value.getHours();
minute = value.getMinutes();
second = value.getSeconds();
}
apdu_data[0] = (dow << 5) + hour;
apdu_data[1] = minute;
apdu_data[2] = second;
return apdu_data;
},
fromBuffer: (buf) => {
if (buf.length !== 3) {
logger.error('Buffer should be 3 bytes long, got', buf.length);
return null;
}
const d = new Date();
let dow = (buf[0] & 0b11100000) >> 5;
const hours = buf[0] & 0b00011111;
const minutes = buf[1];
const seconds = buf[2];
if (hours >= 0 &&
hours <= 23 &&
minutes >= 0 &&
minutes <= 59 &&
seconds >= 0 &&
seconds <= 59) {
if (d.getDay() !== dow && dow > 0) {
if (dow === 7)
dow = 0;
d.setDate(d.getDate() + dow - d.getDay());
}
d.setHours(hours);
d.setMinutes(minutes);
d.setSeconds(seconds);
}
else {
logger.warn('buffer %j (decoded as %d:%d:%d) is not a valid time', buf, hours, minutes, seconds);
}
return d;
},
basetype: {
bitlength: 24,
valuetype: 'composite',
desc: 'day of week + time of day',
help: `// Send the time to the bus!
msg.payload = new Date().toString();
return msg;`,
helplink: 'https://github.com/Supergiovane/node-red-contrib-knx-ultimate/wiki/-Sample---DateTime-to-BUS',
},
subtypes: {
'001': {
name: 'Time of day',
desc: 'time of day',
},
},
};
exports.default = config;
//# sourceMappingURL=dpt10.js.map