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

129 lines (113 loc) 2.89 kB
/** * Alive协议 - 气候控制设备处理 * 基于Clowire协议但有细微差别 */ const fan_modes = ["low", "medium", "high", "off", "auto"] const ac_modes = ["cool", "heat", "fan_only", "dry", "auto"] /** * 缓冲区数据转对象 * @param {Buffer} data 命令缓冲 * @param {object} obj 节点对象 */ function buf2obj(data, obj) { const protocolData = _protocol(data) if (!protocolData) return null protocolData.uid = obj.entity_id if (obj.uid === protocolData.id) return protocolData else return null } /** * 协议解析 (Alive协议从第2字节开始解析) * @param {Buffer} data */ function _protocol(data) { if(!data) return; let obj = {} obj.id = data[1] // Alive协议跳过第一个标识字节 if (data[4] <= 0x39 && data[4] >= 0x23) { if (data[2] !== 0x06) return false return _handleThermostat(obj, data) } return false } /** * 温控器处理 * @param {object} obj * @param {Buffer} data */ function _handleThermostat(obj, data) { switch (data[4]) { case 0x33: obj.type = 'fan_speed' obj.val = fan_modes[data[6]] break case 0x32: obj.type = 'mode' obj.val = ac_modes[data[6]] break case 0x35: obj.type = 'setTemperature' obj.val = data[6] break case 0x36: obj.type = 'power' obj.val = data[6] ? 'on' : 'off' break default: return null } return obj } /** * 处理气候设备状态 * @param {Array} buf * @param {object} obj * @param {Array} arr */ async function handleClimateState(buf, obj, arr) { if(!obj.attributes) { arr = [] return } let i = ac_modes.indexOf(obj.state) buf[2] = buf[4] = 0 if (i >= 0) { buf[3] = 0x32 buf[5] = i arr.push(JSON.parse(JSON.stringify(buf))) } i = fan_modes.indexOf(obj.attributes.fan_mode) if (i >= 0) { buf[3] = 0x33 buf[5] = i arr.push(JSON.parse(JSON.stringify(buf))) } buf[3] = 0x36 buf[5] = obj.state === 'off' ? 0 : 1 arr.push(JSON.parse(JSON.stringify(buf))) buf[3] = 0x34 buf[4] = obj.attributes.current_temperature || 25 buf[5] = obj.attributes.temperature || 16 arr.push(JSON.parse(JSON.stringify(buf))) } /** * 对象转缓冲区 * @param {object} obj HA状态对象 * @param {object} obj1 节点对象 */ function obj2buf(obj, obj1) { if (obj1.entity_id === obj.entity_id) { let arr = [] let buf = [obj1.uid, 0x20, 0x10, 0x21, 0x00, 0x00] // Alive协议不包含标识字节 handleClimateState(buf, obj, arr) return arr } else return null } module.exports = { buf2obj, obj2buf };