From e8034dca6a3f0dc52ec522652a0dcf0bf7bcff55 Mon Sep 17 00:00:00 2001 From: Stephen Houser Date: Thu, 27 May 2021 15:14:58 -0400 Subject: [PATCH] fixing up meters and status --- flexradio-discovery.js | 13 +++++++------ flexradio-messages.js | 2 +- flexradio-meter.html | 14 +++++++++++--- flexradio-meter.js | 10 +++++++--- flexradio-radio.js | 34 +++++++++++++++++++--------------- flexradio-status.js | 2 +- 6 files changed, 46 insertions(+), 29 deletions(-) diff --git a/flexradio-discovery.js b/flexradio-discovery.js index e3b38fd..fee6399 100644 --- a/flexradio-discovery.js +++ b/flexradio-discovery.js @@ -17,22 +17,23 @@ module.exports = function(RED) { node.status({fill:'red', shape:'dot', text:'starting...'}); discoveryListener.on('connecting', function() { - node.log('connecting'); + // node.log('connecting'); node.status({fill:'green', shape:'circle', text:'connecting'}); }); discoveryListener.on('connected', function() { - node.log('connected'); + // node.log('connected'); node.status({fill:'green', shape:'circle', text:'connected'}); }); discoveryListener.on('listening', function() { - node.log('listening'); + // node.log('listening'); node.status({fill:'green', shape:'dot', text:'listening'}); }); discoveryListener.on('error', function(error) { node.log('error'); + node.error(error); node.status({fill:'red', shape:'dot', text:'error'}); }); @@ -48,16 +49,16 @@ module.exports = function(RED) { }); discoveryListener.on('stopped', function() { - node.log('stopped'); + // node.log('stopped'); node.status({fill:'red', shape:'circle', text:'stopped'}); }); - node.log('listener at udp4' + node.host + ':' + node.port); + node.log('start listener at udp4: ' + node.host + ':' + node.port); discoveryListener.start(); } node.on('close', function(done) { - node.log('node close: ' + node.host + ':' + node.port); + node.log('close listnener at udp4: ' + node.host + ':' + node.port); if (node.discoveryListener) { node.discoveryListener.close(); } diff --git a/flexradio-messages.js b/flexradio-messages.js index f55d9b7..ca1599c 100644 --- a/flexradio-messages.js +++ b/flexradio-messages.js @@ -28,7 +28,7 @@ module.exports = function(RED) { }); radio.on('message', function(message_data) { - node.log(JSON.stringify(message_data)); + // node.log(JSON.stringify(message_data)); const message_msg = { message_id: message_data.message_id, payload: message_data.message diff --git a/flexradio-meter.html b/flexradio-meter.html index 5216de0..c00c658 100644 --- a/flexradio-meter.html +++ b/flexradio-meter.html @@ -5,7 +5,8 @@ defaults: { name: { value: '' }, radio: {value: '', type: 'flexradio-radio', required: true}, - client_handle: { value: '' }, + output_mode: { value: 'value', required: true }, + meter_name: { value: '#' } }, inputs: 0, outputs: 1, @@ -26,8 +27,15 @@
- - + + +
+
+ +
diff --git a/flexradio-meter.js b/flexradio-meter.js index 96b6073..0b1b4a9 100644 --- a/flexradio-meter.js +++ b/flexradio-meter.js @@ -6,7 +6,11 @@ module.exports = function(RED) { const node = this; node.name = config.name; node.radio = RED.nodes.getNode(config.radio); - node.client_handle = config.client_handle; + // TODO: Use meter_name as MQTT style topic filter + node.meter_name = config.meter_name; + + // Should we include context with the injected message or just the value + node.output_mode = config.output_mode; if (!node.radio) { // No config node configured, should not happen node.status({fill:'red', shape:'circle', text:'not configured'}); @@ -15,11 +19,11 @@ module.exports = function(RED) { const radio = node.radio; radio.on('meter', function(meter) { - node.log(JSON.stringify(meter)); + // node.log(JSON.stringify(meter)); const msg = { topic: 'meter/' + meter.nam, - payload: meter + payload: node.output_mode == 'value' ? meter.value : meter }; node.send(msg); diff --git a/flexradio-radio.js b/flexradio-radio.js index 5421606..3b1a75d 100644 --- a/flexradio-radio.js +++ b/flexradio-radio.js @@ -25,37 +25,37 @@ module.exports = function(RED) { if (node.radio) { const radio = node.radio; - radio.on('connecting', function() { - updateNodeState(); + radio.on('connecting', function(data) { + updateNodeState(data); }); - radio.on('connected', function() { - updateNodeState(); + radio.on('connected', function(data) { + updateNodeState(data); }); radio.on('message', function(message) { - node.log('received message: ' + JSON.stringify(message)); + // node.log('received message: ' + JSON.stringify(message)); node.emit('message', message); }); radio.on('status', function(status) { - node.log('received status: ' + JSON.stringify(status)); + // node.log('received status: ' + JSON.stringify(status)); node.emit('status', status); }); radio.on('meter', function(meter) { - node.log('received meters: ' + JSON.stringify(meter)); + node.trace('received meters: ' + JSON.stringify(meter)); node.emit('meter', meter); }); radio.on('error', function(error) { // don't re-emit errors. They are treated differently by // the EventEmitter and will crash if not handled. - node.log(error); + node.error(error); }); - radio.on('disconnected', function() { - updateNodeState(); + radio.on('disconnected', function(data) { + updateNodeState(data); node.reconnectTimeout = setTimeout(() => { radio.connect(); @@ -66,12 +66,12 @@ module.exports = function(RED) { updateNodeState(); } - function updateNodeState() { + function updateNodeState(data) { if (node.radio) { const state = node.radio.getConnectionState(); node.state = state; - node.log('radio ' + state); + node.log('radio ' + state + ' ' + (data ? data : '')); node.emit(node.state); } else { node.state = ''; @@ -85,13 +85,17 @@ module.exports = function(RED) { } node.getMeterName = function(meter_index) { + return this.getMeter(meter_index).nam; + } + + node.getMeter = function(meter_index) { const node = this; const radio = node.radio; - return radio.getMeterName(meter_index); + return radio.getMeter(meter_index); } node.send = function(msg, response_handler) { - node.log('send request: ' + msg.payload); + // node.log('send request: ' + msg.payload); if (node.radio) { const radio = node.radio; radio.send(msg.payload, function(response) { @@ -102,7 +106,7 @@ module.exports = function(RED) { payload: response.response }; - node.log('recevied response: ' + JSON.stringify(response_data)); + // node.log('recevied response: ' + JSON.stringify(response_data)); response_handler(response_data); } }); diff --git a/flexradio-status.js b/flexradio-status.js index 18f72a6..3b545e9 100644 --- a/flexradio-status.js +++ b/flexradio-status.js @@ -29,7 +29,7 @@ module.exports = function(RED) { }); radio.on('status', function(status_data) { - node.log(JSON.stringify(status_data)); + // node.log(JSON.stringify(status_data)); const topic = extractMessageTopic(status_data); const status_msg = { topic: topic,