UNPKG

lib-comfoair

Version:

Library to communicate with Zehnder ComfoAirQ ventilation unit through the ComfoControl gateway

80 lines (79 loc) 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComfoControlHeader = exports.COMFO_MESSAGE_HEADER_LENGTH = void 0; const comfoConnect_1 = require("./protocol/comfoConnect"); /** * All ComfoAir messages start with a 38 byte header. */ exports.COMFO_MESSAGE_HEADER_LENGTH = 38; class ComfoControlHeader { /** * The total length of the message, excluding the totalLength field (4 bytes). */ messageLength; /** * The UUID of the sender device. */ senderUuid; /** * The UUID of the receiver device. */ receiverUuid; /** * The length of the operation data. */ opLength; /** * The total length of the message including msg and op data. */ get length() { return exports.COMFO_MESSAGE_HEADER_LENGTH + this.opLength + this.messageLength; } get messageOffset() { return this.opLength + exports.COMFO_MESSAGE_HEADER_LENGTH; } get opOffset() { return exports.COMFO_MESSAGE_HEADER_LENGTH; } constructor(senderUuid, receiverUuid, opLength, messageLength) { this.senderUuid = senderUuid.padStart(32, '0'); this.receiverUuid = receiverUuid.padStart(32, '0'); this.opLength = opLength; this.messageLength = messageLength; } static fromBinary(data, offset = 0) { if (data.length - offset < exports.COMFO_MESSAGE_HEADER_LENGTH) { throw new Error(`Not enough bytes in buffer to read header; expected 38 bytes but got ${data.length - offset}`); } const totalLength = data.readUInt32BE(0 + offset) + 4; const senderUuid = data.toString('hex', 4 + offset, 20 + offset); const receiverUuid = data.toString('hex', 20 + offset, 36 + offset); const opLength = data.readUInt16BE(36 + offset); const messageLength = totalLength - exports.COMFO_MESSAGE_HEADER_LENGTH - opLength; return new ComfoControlHeader(senderUuid, receiverUuid, opLength, messageLength); } toBinary() { const header = Buffer.alloc(exports.COMFO_MESSAGE_HEADER_LENGTH); header.writeUInt32BE(this.length - 4, 0); header.write(this.senderUuid, 4, 16, 'hex'); header.write(this.receiverUuid, 20, 16, 'hex'); header.writeUInt16BE(this.opLength, 36); return header; } getOperationBuffer(data, offset = 0) { if (data.length - offset < this.opOffset + this.opLength) { throw new Error(`Not enough bytes in buffer to read operation; expected ${this.opLength} bytes but buffer has ${Math.max(data.length - (this.opOffset + offset), 0)} bytes left`); } return data.subarray(this.opOffset + offset, this.opOffset + this.opLength + offset); } getMessageBuffer(data, offset = 0) { if (data.length - offset < this.messageOffset + this.messageLength) { throw new Error(`Not enough bytes in buffer to read message; expected ${this.messageLength} bytes but buffer has ${Math.max(data.length - (this.messageOffset + offset), 0)} bytes left`); } return data.subarray(this.messageOffset + offset, this.messageOffset + this.messageLength + offset); } parseOperation(data, offset = 0) { return comfoConnect_1.GatewayOperation.fromBinary(this.getOperationBuffer(data, offset)); } } exports.ComfoControlHeader = ComfoControlHeader;