lib-comfoair
Version:
Library to communicate with Zehnder ComfoAirQ ventilation unit through the ComfoControl gateway
67 lines (66 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComfoControlMessage = void 0;
const comfoConnect_1 = require("./protocol/comfoConnect");
const opcodes_1 = require("./opcodes");
const comfoControlHeader_1 = require("./comfoControlHeader");
/**
* Describes the header of the ComfoAir messages, each message starts with this header.
* The header is always 38 bytes long.
*/
class ComfoControlMessage {
operation;
message;
get opcode() {
return this.operation.opcode;
}
get opcodeName() {
return comfoConnect_1.Opcode[this.operation.opcode];
}
get id() {
return this.operation.id;
}
get resultCode() {
return this.operation.result ?? comfoConnect_1.Result.OK;
}
get resultName() {
return comfoConnect_1.Result[this.resultCode];
}
constructor(operation, message) {
this.operation = operation;
this.message = message;
}
static fromJson(operation, message) {
return new ComfoControlMessage(operation,
// eslint-disable-next-line
Buffer.from(opcodes_1.opcodes[operation.opcode].toBinary((message ?? {}))));
}
static fromBinary(data) {
const messages = [];
for (let offset = 0; offset < data.length;) {
const header = comfoControlHeader_1.ComfoControlHeader.fromBinary(data, offset);
const operation = comfoConnect_1.GatewayOperation.fromBinary(header.getOperationBuffer(data, offset));
const message = header.getMessageBuffer(data, offset);
messages.push(new ComfoControlMessage(operation, message));
offset += header.length;
}
return messages;
}
toString() {
return `${this.opcodeName} (${this.id}) - ${this.resultName} (${this.resultCode})`;
}
deserialize(options) {
if (!opcodes_1.opcodes[this.opcode]) {
throw new Error(`Unsupported opcode: ${comfoConnect_1.Opcode[this.opcode]}`);
}
return opcodes_1.opcodes[this.opcode].fromBinary(this.message, options);
}
/**
* Returns the message as a untyped JSON object.
* @returns The message as a JSON object.
*/
toJson() {
return this.deserialize({ readUnknownField: true });
}
}
exports.ComfoControlMessage = ComfoControlMessage;