node-red-contrib-clowire
Version:
clowire for node-red
118 lines (103 loc) • 2.67 kB
JavaScript
const fan_modes = ["low", "medium", "high", "off", "auto"]
const ac_modes = ["cool", "heat", "fan_only", "dry", "auto"]
/**
* buf数据to对象
*
* data 命令缓冲
*/
module.exports.buf2obj = function (data, id, address, uid) {
const protocolData = _protocol(data)
if (!protocolData) return null
protocolData.uid = uid
if (id === protocolData.id)
return protocolData
else
return null
}
/**
* 协议解析0x01,0x20,0x10,0x11,0x00,0x80 {name:"大灯开关",id:1,address:1,cmd:"click",uid:1},
* @param {*} data
*/
function _protocol(data) {
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 {*} 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
break
}
return obj
}
/**
* 写入状态至面板
* @param {*} uid
* uid 为返馈ID
*/
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)))
}
/**
* obj转buf
* @param {*} obj
*
*/
module.exports.obj2buf = function (obj, id, address, uid) {
if (uid === obj.entity_id) {
let arr = []
let buf = [id, 0x20, 0x10, 0x21, 0x00, 0x00]
handleClimateState(buf, obj, arr)
return arr
}
else
return null
}