node-red-contrib-kbr-ebus
Version:
KBR Gmbh - nodes for accessing ebus devices
242 lines (225 loc) • 6.77 kB
JavaScript
module.exports=class EbusDevice{
constructor(properties){
//Parameter-Store
this.props={}
//Werte-Store
this.state={}
//Default-Properties
this.setProperties({
classId:0,
address:65534,//= KBR-Auslieferadresse
name:"unbenannt",
reactionTime:10,//ms
scanMode:false,
})
this.setStates({
deviceStatusFlag:0
})
this.setProperties(properties)
}
get properties(){
return this.props
}
set properties(value){
this.props=value
}
setProperties(keyValueMap){
this.props=Object.assign({},this.props,keyValueMap)
}
setProperty(key,value){
this.props[key]=value
}
getProperty(key){
return this.props[key]
}
get states(){
return this.state
}
set states(value){
this.state=value
}
setStates(keyValueMap){
this.state=Object.assign({},this.state,keyValueMap)
}
setState(key,value){
this.state[key]=value
}
getState(key){
return this.state[key]
}
getStates(keys){
return keys.map(key=>{
return {key:this.state[key]}
})
}
get address(){
return this.props.address
}
get classId(){
return this.props.classId
}
get name(){
return this.props.name
}
get scanModeActive(){
return this.props.scanMode
}
controlDevice(control){
switch(control.cmd){
case 'set-property':
this.setProperty(control.id,control.value)
break
case 'set-properties':
this.setProperties(control.value)
break
case 'set-state':
this.setState(control.id,control.value)
break
case 'set-states':
this.setStates(control.value)
break
}
}
processTelex(telex){
let result
console.log("processTelex",JSON.stringify(telex))
switch(telex.to){
case this.address:
return this.processDeviceTelex(telex)
case 0xFFFF:
if(this.scanModeActive)
return this.processDeviceTelex(telex)
break
case 0://Broadcast/Multicast
return this.processBroadcastTelex(telex)
}
return
}
createBufferWithClassId(length){
const buffer= Buffer.alloc(length+1)
buffer.writeInt8(this.classId,0)
return buffer
}
processDeviceTelex(telex){
console.log("processDeviceTelex")
if(telex.data.length<2)
return
const classId=telex.data[0]
const command=telex.data[1]
const data=Buffer.from(telex.data.slice(2))
if(classId!=this.classId){
if(classId!=0)
return
console.log("processDeviceTelex:multicast")
//Multicast's
switch(command){
case 0x04:
return true
case 0x05://Ausnahme:
return this.getNameAndVersion()
case 0x02://Sync
this.syncEform(data)
break
}
return
}
//Gerätebefehle
switch(command){
case 0x01:
return this.resetDevice()
case 0x02:
return this.syncEform(data)
case 0x04:
return true
case 0x05:
return this.getNameAndVersion()
case 0x06:
return this.setName(data)
case 0x07:
return this.setAddress(data)
case 0x08:
return this.setReactionTime(data)
case 0x0F:
return this.getReactionTime()
case 0x0D:
return this.getDeviceStatusFlag()
case 0x0E:
return this.resetDeviceStatusFlag()
default:
console.log("no processing")
break
}
return -1
}
processBroadcastTelex(telex){
console.log("processBroadcastTelex")
if(telex.data.length<2)
return
const classId=telex.data[0]
const command=telex.data[1]
const data=Buffer.from(telex.data.slice(2))
switch(command){
case 0x01://Gerätereset
return this.resetDevice()
}
return -1
}
resetDevice(){
console.log("resetDevice")
return true
}
getNameAndVersion(){
console.log("getNameAndVersion")
var props=this.getProperties(['version','name'])
const buffer=this.createBufferWithClassId(23)
let pos=1
buffer.writeInt8(props.version[0],pos++)
buffer.writeInt8(props.version[1],pos++)
buffer.write(props.name,pos,20)
pos+=20
buffer.writeInt8(props.version[2],pos++)
return buffer
}
syncEform(data){
console.log("syncEform")
return true
}
setName(data){
console.log("setName")
this.setProperty('name',data.toString())
return true
}
setAddress(data){
console.log("setAddress",data)
this.setProperty('address',data.readInt16BE(0))
return true
}
setReactionTime(data){
console.log("setReactionTime")
if(data.length==1)
this.setProperty('reactionTime',data.readInt8(0)*10)
else if(data.length==2)
this.setProperty('reactionTime',data.readInt8(1))
else
return false
return true
}
getReactionTime(){
console.log("getReactionTime")
const buffer=this.createBufferWithClassId(2)
const reactionTime=this.getProperty('reactionTime')
buffer.writeInt8(reactionTime/10,1)
buffer.writeInt8(Math.min(reactionTime,255),2)
return buffer
}
getDeviceStatusFlag(){
console.log("getDeviceStatusFlag")
const buffer=this.createBufferWithClassId(2)
buffer.writeInt16BE(this.getState('deviceStatusFlag'),1)
return buffer
}
resetDeviceStatusFlag(){
console.log("resetDeviceStatusFlag")
return true
}
}