UNPKG

node-red-contrib-symi

Version:

Node-RED nodes for smart home device communication with Home Assistant integration, supporting HassKit, Clowire, Alive, LF and complete Symi protocols

71 lines (60 loc) 1.85 kB
/** * TCP协议处理器 * 支持多种工业设备通信协议 */ class HandleTcp { constructor(tcpClient, devtype) { this.tcpClient = tcpClient this.devtype = devtype this.protocol = null this.onData = null // 加载对应的协议处理器 try { this.protocol = require(`./protocols/${devtype}`) } catch (e) { console.error(`协议 ${devtype} 不支持:`, e.message) return } // 注册数据接收回调 this.tcpClient.onData = [this.handleReceive.bind(this), this] } /** * 处理接收到的数据 * @param {Buffer} data */ handleReceive(data) { if (!this.protocol || !this.protocol.handleRece) return const processedData = this.protocol.handleRece(data) if (processedData && this.onData) { this.onData(processedData) } } /** * 写入数据(带锁) * @param {Buffer|Array} data */ write_lock(data) { if (!this.protocol || !this.protocol.handle_write) return const processedData = this.protocol.handle_write(data) if (processedData) { this.tcpClient.write_lock(processedData) } } /** * 获取对象转缓冲区的处理函数 * @param {string} domain */ getObjToBuf(domain) { if (!this.protocol) return null return this.protocol.getObjToBuf ? this.protocol.getObjToBuf(domain) : null } /** * 获取缓冲区转对象的处理函数 * @param {string} domain */ getBufToObj(domain) { if (!this.protocol) return null return this.protocol.getBufToObj ? this.protocol.getBufToObj(domain) : null } } module.exports = HandleTcp;