UNPKG

knxultimate

Version:

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

58 lines 2.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const KNXConstants_1 = require("./KNXConstants"); const KNXAddress_1 = __importDefault(require("./KNXAddress")); class KNXAddresses { constructor() { this._type = KNXConstants_1.KNX_CONSTANTS.KNX_ADDRESSES; this._addresses = new Set(); } getAddressCount() { return this._addresses.size; } get length() { return 2 + this._addresses.size * 2; } get type() { return this._type; } static createFromBuffer(buffer, offset = 0) { if (offset + this.length >= buffer.length) { throw new Error(`offset ${offset} out of buffer range ${buffer.length}`); } const structureLength = buffer.readUInt8(offset); if (offset + structureLength > buffer.length) { throw new Error(`offset ${offset} block length: ${structureLength} out of buffer range ${buffer.length}`); } offset++; const type = buffer.readUInt8(offset++); if (type !== KNXConstants_1.KNX_CONSTANTS.KNX_ADDRESSES) { throw new Error(`Invalid KNXAddresses type ${type}`); } const knxAddresses = new KNXAddresses(); for (let i = 2; i < structureLength; i += 2) { knxAddresses.add(buffer.readUInt16BE(offset).toString()); offset += 2; } return knxAddresses; } add(address) { this._addresses.add(KNXAddress_1.default.createFromString(address)); } toBuffer() { const buffer = Buffer.alloc(this.length); let offset = 0; buffer.writeUInt8(this.length, offset++); buffer.writeUInt8(KNXConstants_1.KNX_CONSTANTS.KNX_ADDRESSES, offset++); for (const knxAddress of this._addresses) { buffer.writeUInt16BE(knxAddress.get(), offset); offset += 2; } return buffer; } } exports.default = KNXAddresses; //# sourceMappingURL=KNXAddresses.js.map