UNPKG

knxultimate

Version:

KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.

75 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const KnxLog_1 = require("../KnxLog"); const utils_1 = require("../utils"); const logger = (0, KnxLog_1.module)('DPT11'); const config = { id: 'DPT11', formatAPDU: (value) => { if (!value) { logger.error('cannot write null value for DPT11'); return null; } const apdu_data = Buffer.alloc(3); switch (typeof value) { case 'string': case 'number': value = new Date(value); break; case 'object': if (value.constructor.name !== 'Date' && (0, utils_1.hasProp)(value, 'day') && (0, utils_1.hasProp)(value, 'month') && (0, utils_1.hasProp)(value, 'year')) { value = new Date(parseInt(value.year), parseInt(value.month), parseInt(value.day)); } } if (isNaN(value.getDate())) { logger.error('Must supply a numeric timestamp, Date or String object for DPT11 Date'); return null; } apdu_data[0] = value.getDate(); apdu_data[1] = value.getMonth() + 1; const year = value.getFullYear(); apdu_data[2] = year - (year >= 2000 ? 2000 : 1900); return apdu_data; }, fromBuffer: (buf) => { if (buf.length !== 3) { logger.error(`Buffer should be 3 bytes long. Received ${buf.length}`); return null; } const day = buf[0] & 31; const month = buf[1] & 15; let year = buf[2] & 127; year += year > 89 ? 1900 : 2000; if (day >= 1 && day <= 31 && month >= 1 && month <= 12 && year >= 1990 && year <= 2089) { return new Date(year, month - 1, day); } logger.error('%j => %d/%d/%d is not valid date according to DPT11, setting to 1990/01/01', buf, day, month, year); throw new Error('Error converting date buffer to Date object.'); return null; }, basetype: { bitlength: 24, valuetype: 'composite', desc: '3-byte date value', help: `// Send the date 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: 'Date', desc: 'Date', }, }, }; exports.default = config; //# sourceMappingURL=dpt11.js.map