UNPKG

node-red-contrib-kbr-ebus

Version:

KBR Gmbh - nodes for accessing ebus devices

70 lines (60 loc) 2.48 kB
module.exports = function(RED) { var ebus= require("./ebus"); function KBREbusTelexCreator(config) { RED.nodes.createNode(this,config); var node=this; node.on("close", function() { }); node.on('input', function(msg) { msg.from=msg.payload.from||config.from||0; msg.to=msg.payload.to||config.to||0; var data=msg.payload.data||config.data||""; var buffer=Buffer.from([]); if(Array.isArray(data)){ buffer=Buffer.from(data); } else if(Buffer.isBuffer(data)){ buffer=data; } else if (typeof data === 'string' || data instanceof String) { if(data.startsWith("[")){ buffer=Buffer.from(JSON.parse(data)); } else{ var replace=data.indexOf("$"); if(replace>=0){ var payload=msg.payload; var replbuffer=Buffer.from([]); if(msg.payload.replbuffer) payload=msg.payload.replbuffer; if(Array.isArray(payload)){ replbuffer=Buffer.from(payload); } else if(Buffer.isBuffer(payload)){ replbuffer=payload; } var hexvals=[]; replbuffer.forEach(function(byte){ hexvals.push(byte.toString(16)); }); var replstr=hexvals.join(","); data=data.replace("$",replstr); } var strvals=data.split(","); var arr=[]; strvals.forEach(function(item){ item=item.trim(); arr.push(parseInt("0x"+item,16)); }); buffer=Buffer.from(arr); } } msg.data=buffer msg.payload=ebus.createTelex(from,to,buffer); msg.topic=config.topic; node.send(msg); }); } RED.nodes.registerType("kbr-ebus-telex-creator",KBREbusTelexCreator); }