node-red-contrib-knx-ultimate
Version:
Control your KNX and KNX Secure intallation via Node-Red! A bunch of KNX nodes, with integrated Philips HUE control, ETS group address importer, and KNX routing between interfaces. Easy to use and highly configurable.
84 lines (76 loc) • 2.18 kB
JavaScript
/* eslint-disable prefer-arrow-callback */
module.exports = function (RED) {
function knxUltimateHATranslator (config) {
RED.nodes.createNode(this, config)
this.config = config
const node = this
function setNodeStatus ({ fill, shape, text }) {
const dDate = new Date()
node.status({
fill,
shape,
text:
text +
' (' +
dDate.getDate() +
', ' +
dDate.toLocaleTimeString() +
')'
})
}
setNodeStatus({ fill: 'grey', shape: 'dot', text: 'Waiting' })
this.on('input', function (msg) {
// 11/11/2021 Clone input message and replace only relevant topics
const utils = require('./utils/utils.js')
const sPayload = utils.fetchFromObject(
msg,
config.payloadPropName || 'payload'
)
// 15/11/2021 inform user about undefined topic or payload
if (sPayload === undefined) {
setNodeStatus({
fill: 'red',
shape: 'dot',
text: 'Received invalid payload from ' + msg.topic || ''
})
return
}
let bRes = null
try {
bRes = utils.ToBoolean(
sPayload,
node.config // Retrieve the config node. It can be null, but it's handled in utils.js; // 15/11/2021 Convert input to boolean.);
)
} catch (error) { }
if (bRes === undefined || bRes === null) {
setNodeStatus({
fill: 'red',
shape: 'dot',
text:
'Received non convertible boolean value ' +
sPayload +
' from ' +
msg.topic
})
return
}
const msgOUt = RED.util.cloneMessage(msg)
try {
msgOUt.payload = bRes
setNodeStatus({
fill: 'green',
shape: 'dot',
text: '(Send) ' + msgOUt.payload
})
node.send(msgOUt)
} catch (error) {
setNodeStatus({
fill: 'red',
shape: 'dot',
text: 'Unable to invert the input payload ' + bRes
})
}
})
}
RED.nodes.registerType('knxUltimateHATranslator', knxUltimateHATranslator)
}