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
62 lines (53 loc) • 1.39 kB
JavaScript
/**
* Alive协议 - 开关设备处理
* 基于Clowire协议但有细微差别
*/
/**
* 缓冲区数据转对象
* @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
}
/**
* 协议解析 (Alive协议从第2字节开始解析)
* @param {Buffer} data
*/
function _protocol(data) {
if(!data) return;
let obj = {}
obj.id = data[1] // Alive协议跳过第一个标识字节
obj.address = data[3] // 地址位置调整
// 开关命令判断
if (data[2] === 0x20 && data[4] === 0x11) {
obj.type = 'power'
obj.val = data[6] ? '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 = [obj1.uid, 0x20, obj1.address, 0x11, 0x00, state] // Alive协议不包含标识字节
return buf
}
else
return null
}
module.exports = {
buf2obj,
obj2buf
};