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

68 lines (59 loc) 1.64 kB
/** * Clowire协议处理器 * 支持气候控制和开关设备 */ const crc16 = require("../crc16"); const DATA_LENGTH = 9 const fns = { switch: require('./clowire/Switch'), climate: require('./clowire/Climate') } module.exports = { /** * 收到tcp数据预处理 * @param {Buffer} data */ handleRece: function (data) { // 大于9个字节超出clowire数据帧取后9位,小于9位无效丢弃 if (data.length > DATA_LENGTH) { data = data.slice(-DATA_LENGTH) } else if (data.length < DATA_LENGTH) { return } var crc = crc16(data.slice(0, -3)); if (data.readUInt16LE(data.length - 3, data) === crc) { return data } }, /** * 处理写入数据 * @param {Buffer|Array} buf */ handle_write: function (buf) { let data = Buffer.from(buf) if (data.length < DATA_LENGTH - 3) { return } var buffer = Buffer.alloc(data.length + 3); for (let i = 0; i < data.length; i++) { buffer[i] = data[i] } buffer.writeUInt16LE(crc16(data), data.length) buffer[buffer.length - 1] = 0xaa 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 } };