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
128 lines (112 loc) • 2.76 kB
JavaScript
/**
* 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
}
/**
* 协议解析
* @param {Buffer} data
*/
function _protocol(data) {
if(!data) return;
let obj = {}
obj.id = data[0]
if (data[3] <= 0x39 && data[3] >= 0x23) {
if (data[1] !== 0x06) return false
return _handleThermostat(obj, data) //温控器
}
return false
}
/**
* 温控器处理
* @param {object} obj
* @param {Buffer} data
*/
function _handleThermostat(obj, data) {
switch (data[3]) {
case 0x33:
obj.type = 'fan_speed'
obj.val = fan_modes[data[5]]
break
case 0x32:
obj.type = 'mode'
obj.val = ac_modes[data[5]]
break
case 0x35:
obj.type = 'setTemperature'
obj.val = data[5]
break
case 0x36:
obj.type = 'power'
obj.val = data[5] ? '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]
handleClimateState(buf, obj, arr)
return arr
}
else
return null
}
module.exports = {
buf2obj,
obj2buf
};