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 (61 loc) 1.73 kB
/** * Alive协议处理器 * 基于Clowire协议的变种 */ const crc16 = require("../crc16"); const DATA_LENGTH = 9 const fns = { switch: require('./alive/Switch'), climate: require('./alive/Climate') } module.exports = { /** * 收到tcp数据预处理 * @param {Buffer} data */ handleRece: function (data) { // 大于9个字节超出alive数据帧取后9位,小于9位无效丢弃 if (data.length > DATA_LENGTH) { data = data.slice(-DATA_LENGTH) } else if (data.length < DATA_LENGTH) { return } // Alive协议特殊标识检查 if (data[0] !== 0xAA) return var crc = crc16(data.slice(1, -2)); if (data.readUInt16LE(data.length - 2) === crc) { return data } }, /** * 处理写入数据 * @param {Buffer|Array} buf */ handle_write: function (buf) { let data = Buffer.from(buf) if (data.length < DATA_LENGTH - 2) { return } var buffer = Buffer.alloc(data.length + 2); buffer[0] = 0xAA; // Alive协议标识 for (let i = 0; i < data.length; i++) { buffer[i + 1] = data[i] } buffer.writeUInt16LE(crc16(data), data.length + 1) return buffer }, /** * 获取对象转缓冲区函数 * @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 } };