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
61 lines (52 loc) • 1.25 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 && obj.address === protocolData.address)
return protocolData
else
return null
}
/**
* 协议解析
* @param {Buffer} data
*/
function _protocol(data) {
if(!data) return;
let obj = {}
obj.id = data[1] // 设备ID
obj.address = data[2] // 设备地址
// 开关命令判断
if (data[3] === 0x10) { // 开关控制命令
obj.type = 'power'
obj.val = data[4] ? 'on' : 'off'
return obj
}
return null
}
/**
* 对象转缓冲区
* @param {object} obj HA状态对象
* @param {object} obj1 节点对象
*/
function obj2buf(obj, obj1) {
if (obj1.entity_id === obj.entity_id) {
let state = obj.state === 'on' ? 1 : 0
let buf = [0xb2, obj1.uid, obj1.address, 0x10, state, 0x00, 0x00, 0x2b]
return buf
}
else
return null
}
module.exports = {
buf2obj,
obj2buf
};