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
107 lines (92 loc) • 2.51 kB
JavaScript
/**
* HassKit协议 - 气候控制设备处理
*/
/**
* 缓冲区数据转对象
* @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[1] // HassKit协议中设备ID在第2字节
// 根据命令字节判断设备类型和操作
switch (data[3]) {
case 0x01: // 温度设置
obj.type = 'setTemperature'
obj.val = data[4]
break
case 0x02: // 模式设置
obj.type = 'mode'
obj.val = _getModeFromValue(data[4])
break
case 0x03: // 电源控制
obj.type = 'power'
obj.val = data[4] ? 'on' : 'off'
break
default:
return null
}
return obj
}
/**
* 根据数值获取模式
* @param {number} value
*/
function _getModeFromValue(value) {
const modes = ['off', 'heat', 'cool', 'auto', 'dry', 'fan_only']
return modes[value] || 'off'
}
/**
* 根据模式获取数值
* @param {string} mode
*/
function _getValueFromMode(mode) {
const modes = ['off', 'heat', 'cool', 'auto', 'dry', 'fan_only']
return modes.indexOf(mode) || 0
}
/**
* 对象转缓冲区
* @param {object} obj HA状态对象
* @param {object} obj1 节点对象
*/
function obj2buf(obj, obj1) {
if (obj1.entity_id === obj.entity_id) {
let commands = []
// 温度设置
if (obj.attributes && obj.attributes.temperature) {
let buf = [0xb2, obj1.uid, 0x00, 0x01, obj.attributes.temperature, 0x00, 0x00, 0x2b]
commands.push(buf)
}
// 模式设置
if (obj.state) {
let modeValue = _getValueFromMode(obj.state)
let buf = [0xb2, obj1.uid, 0x00, 0x02, modeValue, 0x00, 0x00, 0x2b]
commands.push(buf)
}
// 电源控制
let powerValue = obj.state === 'off' ? 0 : 1
let buf = [0xb2, obj1.uid, 0x00, 0x03, powerValue, 0x00, 0x00, 0x2b]
commands.push(buf)
return commands
}
else
return null
}
module.exports = {
buf2obj,
obj2buf
};