node-red-contrib-clowire
Version:
clowire for node-red
38 lines (34 loc) • 970 B
JavaScript
const states = {};
/**
* buf数据to对象
*
* data 命令缓冲
*/
module.exports.buf2obj = function (data, obj) {
if (!data) return;
if (data[0] !== obj.uid ||
data[3] !== 0x02 ||
data[1] !== 0x03
) return;
if(data[5] !== (1 << obj.address -1))
return;
return { id: obj.uid, address: obj.address, uid: obj.entity_id };
};
/**
* obj转buf
* @param {*} obj
*
*/
module.exports.obj2buf = function ({ entity_id, state }, obj1) {
if (states[obj1.uid] === undefined) states[obj1.uid] = 0; //状态暂存
if (obj1.entity_id === entity_id) {
let buf = [obj1.uid, 0x06, 0x10, 8, 1, 0x00];
if (state === 'on') {
buf[5] = states[obj1.uid] | (1 << (obj1.address - 1));
} else {
buf[5] = states[obj1.uid] & ~(1 << (obj1.address - 1));
}
states[obj1.uid] = buf[5];
return buf;
} else return null;
};