Skip to content

Commit

Permalink
fixing up meters and status
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhouser committed May 27, 2021
1 parent e4041b6 commit e8034dc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
13 changes: 7 additions & 6 deletions flexradio-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
});

Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion flexradio-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions flexradio-meter.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,8 +27,15 @@
<input type="text" id="node-input-radio" placeholder="FlexRadio">
</div>
<div class="form-row">
<label for="node-input-client-handle"><i class="fa fa-tag"></i> Handle</label>
<input type="text" id="node-input-client-handle">
<label for="node-input-meter_name"><i class="fa fa-tag"></i> Meter Name</label>
<input type="text" id="node-input-meter_name">
</div>
<div class="form-row">
<label for="node-input-output_mode"><i class="fa fa-sign-in"></i> Output</label>
<select id="node-input-output_mode">
<option value="value">Value Only</option>
<option value="context">Value and Context</option>
</select>
</div>
</script>

Expand Down
10 changes: 7 additions & 3 deletions flexradio-meter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand All @@ -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);
Expand Down
34 changes: 19 additions & 15 deletions flexradio-radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 = '';
Expand All @@ -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) {
Expand All @@ -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);
}
});
Expand Down
2 changes: 1 addition & 1 deletion flexradio-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e8034dc

Please sign in to comment.