UNPKG

neramirez-broadlink-ts

Version:

A TypeScript-enhanced Node.JS fork of broadlinkjs, designed for interacting with RM devices in homebridge-broadlink-rm. Now includes a feature for handling multiple requests to the same device, with a specific focus on supporting homebridge-broadlink-wind

143 lines 6.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BroadLinkDevice = void 0; const device_types_1 = require("../device.types"); const payload_handler_1 = require("../types/payload.handler"); const packet_handler_1 = require("../packet.handler"); const socket_handler_1 = require("../socket.handler"); class BroadLinkDevice { constructor(host, macAddress, deviceType, logger) { this.authenticate = async () => { const payload = Buffer.alloc(0x50, 0); //This device id payload[0x04] = 0x31; payload[0x05] = 0x31; payload[0x06] = 0x31; payload[0x07] = 0x31; payload[0x08] = 0x31; payload[0x09] = 0x31; payload[0x0a] = 0x31; payload[0x0b] = 0x31; payload[0x0c] = 0x31; payload[0x0d] = 0x31; payload[0x0e] = 0x31; payload[0x0f] = 0x31; payload[0x10] = 0x31; payload[0x11] = 0x31; payload[0x12] = 0x31; payload[0x1e] = 0x01; payload[0x2d] = 0x01; payload[0x30] = "T".charCodeAt(0); payload[0x31] = "e".charCodeAt(0); payload[0x32] = "s".charCodeAt(0); payload[0x33] = "t".charCodeAt(0); payload[0x34] = " ".charCodeAt(0); payload[0x35] = " ".charCodeAt(0); payload[0x36] = "1".charCodeAt(0); return this.dispatchCommandAndIncrementCounter(0x65, payload); }; this.onPayloadReceived = (_err, payload) => { this.logger.debug(`(${this.macAddress.toString("hex")}) Payload received:${payload.toString("hex")}`); const param = payload[0]; const PayloadHandlerClass = payload_handler_1.payloadHandlers[param]; if (PayloadHandlerClass) { const handlerInstance = new PayloadHandlerClass(this.rm4Type); handlerInstance.handle(payload); } }; this.checkData = () => { let packet = Buffer.from([0x04]); packet = Buffer.concat([this.request_header, packet]); return this.dispatchCommandAndIncrementCounter(0x65, packet); }; // Externally Accessed Methods this.sendData = (queueItem) => { let packet = Buffer.from([0x02, 0x00, 0x00, 0x00]); packet = Buffer.concat([this.code_sending_header, packet, queueItem.buffer]); return this.dispatchCommandAndIncrementCounter(0x6a, packet, queueItem); }; this.enterLearning = () => { let packet = Buffer.from([0x03]); packet = Buffer.concat([this.request_header, packet]); return this.dispatchCommandAndIncrementCounter(0x6a, packet); }; this.checkTemperature = () => { let packet = (device_types_1.rm4DeviceTypes[(this.deviceType)] || device_types_1.rm4PlusDeviceTypes[(this.deviceType)]) ? Buffer.from([0x24]) : Buffer.from([0x1]); packet = Buffer.concat([this.request_header, packet]); return this.dispatchCommandAndIncrementCounter(0x6a, packet); }; this.checkHumidity = () => { let packet = (device_types_1.rm4DeviceTypes[(this.deviceType)] || device_types_1.rm4PlusDeviceTypes[(this.deviceType)]) ? Buffer.from([0x24]) : Buffer.from([0x1]); packet = Buffer.concat([this.request_header, packet]); return this.dispatchCommandAndIncrementCounter(0x6a, packet); }; this.cancelLearn = () => { let packet = Buffer.from([0x1e]); packet = Buffer.concat([this.request_header, packet]); return this.dispatchCommandAndIncrementCounter(0x6a, packet); }; this.isProcessing = false; this.queue = []; this.logger = logger; this.promises = {}; this.host = host; this.macAddress = macAddress; this.deviceType = deviceType; this.requestCounter = 0; this.rm4Type = (device_types_1.rm4DeviceTypes[(deviceType)] || device_types_1.rm4PlusDeviceTypes[deviceType]); this.request_header = this.rm4Type ? Buffer.from([0x04, 0x00]) : Buffer.from([]); this.code_sending_header = this.rm4Type ? Buffer.from([0xda, 0x00]) : Buffer.from([]); //except 5f36 and 6508 ¯\_(ツ)_/¯ if (deviceType === parseInt(`0x5f36`) || deviceType === parseInt(`0x6508`)) { this.code_sending_header = Buffer.from([0xd0, 0x00]); this.request_header = Buffer.from([0x04, 0x00]); } this.packetHandler = new packet_handler_1.PacketHandler(this.logger); this.socketHandler = new socket_handler_1.SocketHandler(this.logger, this.host, this.macAddress, this.deviceType, this.packetHandler); } enqueue(command) { const p = new Promise((resolve, reject) => { this.queue.push({ buffer: command, promiseExecutor: { resolve, reject } }); }); if (!this.isProcessing) { this.processQueue(); } return p; } async processQueue() { if (this.queue.length === 0) { this.isProcessing = false; return; } this.isProcessing = true; const command = this.queue.shift(); if (command) { try { await this.sendData(command); } catch (err) { console.error(`Error processing command: ${err}`); } this.processQueue(); } } toJSON() { return { host: this.host, macAddress: this.macAddress, deviceType: this.deviceType, requestCounter: this.requestCounter, rm4Type: this.rm4Type, isProcessing: this.isProcessing, queue: this.queue }; } dispatchCommandAndIncrementCounter(command, payload, queueItem) { const packet = this.packetHandler.createPacket(command, payload, this.macAddress, this.requestCounter, this.deviceType); const responsePromise = this.socketHandler.sendPacket(command, packet, this.requestCounter, queueItem); this.requestCounter++; return responsePromise; } } exports.BroadLinkDevice = BroadLinkDevice; //# sourceMappingURL=broadLinkDevice.js.map