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

62 lines (53 loc) 1.41 kB
/** * LF协议处理器 * 另一种工业设备通信协议 */ const DATA_LENGTH = 8 const fns = { switch: require('./lf/Switch'), climate: require('./lf/Climate') } module.exports = { /** * 收到tcp数据预处理 * @param {Buffer} data */ handleRece: function (data) { // 大于8个字节超出LF数据帧取后8位,小于8位无效丢弃 if (data.length > DATA_LENGTH) { data = data.slice(-DATA_LENGTH) } else if (data.length < DATA_LENGTH) { return } // LF协议简单校验 if (data[0] === 0xFF && data[DATA_LENGTH - 1] === 0xFE) { return data } }, /** * 处理写入数据 * @param {Buffer|Array} buf */ handle_write: function (buf) { let data = Buffer.from(buf) if (data.length < DATA_LENGTH) return // LF协议添加帧头帧尾 data[0] = 0xFF data[DATA_LENGTH - 1] = 0xFE return data }, /** * 获取对象转缓冲区函数 * @param {string} domain */ getObjToBuf: function (domain) { return fns[domain] ? fns[domain].obj2buf : null }, /** * 获取缓冲区转对象函数 * @param {string} domain */ getBufToObj: function (domain) { return fns[domain] ? fns[domain].buf2obj : null } };