Skip to content

Commit

Permalink
break out status as own node, add radio connection states
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhouser committed May 27, 2021
1 parent e5e5d08 commit e4041b6
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 83 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ Todo:
S6190EABE|profile mic list=Default^Default FHM-1^Default FHM-1 DX^Default FHM-2^Default FHM-2 DX^Default FHM-2 ESSB^Default FHM-3^Default FHM-3 DX^Default FHM-3 ESSB^Default HM-Pro^Default PR781^Default PR781 ESSB 3_2k^Default ProSet HC6^Inrad M629^Inrad M650^RadioSport DX M207^RadioSport DX M208^RadioSport DX M350-ADJ^RadioSport DX M360/EM56^RadioSport WIDE M207^RadioSport WIDE M208^RadioSport WIDE M350-ADJ^RadioSport WIDE M360/EM56^RTTYDefault^
```

- [ ] Flesh out the `flexradio-subscription` node to allow one-stop-shopping for subscribing and receiving messages from the radio. Ex, `sub meter 1` and all the meter messages for meter 1 should appear at the output of the node.
- [ ] Flesh out the `flexradio-subscription` node to allow one-stop-shopping for subscribing and receiving messages from the radio. Ex, `sub meter 1` and all the meter messages for meter 1 should appear at the output of the node.

- [ ] Flow messages on connect, disconnect, etc. So you can trigger `sub` commands when the radio connects and if it disconnects and reconnects.
31 changes: 2 additions & 29 deletions flexradio-messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,13 @@
color: '#a6bbcf',
defaults: {
name: {value: ''},
radio: {value: '', type: 'flexradio-radio', required: true},
radio_messages: {value: ''},
status_messages: {value: ''},
client_handle: {value: ''},
radio: {value: '', type: 'flexradio-radio', required: true}
},
inputs: 0,
outputs: 1,
icon: 'file.png',
label: function() {
return this.name || 'flexradio-messages';
},
oneditprepare: function() {
var updateOptions = function() {
const status_messages = $('#node-input-status_messages');
if (status_messages.is(":checked")) {
$('#client-handle').show();
} else {
$('#client-handle').hide();
}
};
updateOptions();
$("#node-input-status_messages").change(updateOptions);
}
});
</script>
Expand All @@ -39,20 +24,8 @@
<label for="node-input-radio"><i class="fa fa-sign-in"></i> FlexRadio</label>
<input type="text" id="node-input-radio" placeholder="FlexRadio">
</div>
<div class="form-row">
<label><i class="fa fa-sign-out" ></i> Output</label>
<input style="width:20px; vertical-align:baseline; margin-right:5px;" type="checkbox" id="node-input-radio_messages">
<label style="width:auto; margin-top:7px;" for="node-input-radio_messages">Radio Messages</label>
<input style="width:20px; vertical-align:baseline; margin-right:5px;" type="checkbox" id="node-input-status_messages">
<label style="width:auto; margin-top:7px;" for="node-input-status_messages">Status Messages</label>
</div>
</div>
<div id="client-handle" class="form-row" style="padding-left:20px;">
<label for="node-input-client_handle"><i class="fa fa-tag"></i> Handle</label>
<input type="text" id="node-input-client_handle" placeholder="Client Handle (optional)">
</div>
</script>

<script type="text/html" data-help-name="flexradio-messages">
<p>Receives FlexRadio 6xxx series messages and status messages and outputs them.</p>
<p>Receives FlexRadio 6xxx series messages injects them into the flow.</p>
</script>
48 changes: 5 additions & 43 deletions flexradio-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = function(RED) {
const node = this;
node.name = config.name;
node.radio = RED.nodes.getNode(config.radio);
node.outputRadioMessages = config.radio_messages || false;
node.outputStatusMessages = config.status_messages || false;
node.clientHandle = config.client_handle;

if (!node.radio) { // No config node configured, should not happen
node.status({fill:'red', shape:'circle', text:'not configured'});
Expand All @@ -32,49 +29,14 @@ module.exports = function(RED) {

radio.on('message', function(message_data) {
node.log(JSON.stringify(message_data));
if (node.outputRadioMessages) {
const message_msg = {
message_id: message_data.message_id,
payload: message_data.message
};
const message_msg = {
message_id: message_data.message_id,
payload: message_data.message
};

node.send(message_msg);
}
node.send(message_msg);
});

radio.on('status', function(status_data) {
node.log(JSON.stringify(status_data));
if (node.outputStatusMessages) {
const topic = extractMessageTopic(status_data);
const status_msg = {
topic: topic,
client: status_data.client,
payload: status_data[topic]
};

node.send(status_msg);
}
});

function extractMessageTopic(message) {
// remove 'header' fields and find topical fields of message
const topics = Object.keys(message).filter(function(key) {
return !(['type', 'client'].includes(key));
});

if (topics.length != 1) {
node.log("ERROR: message from radio has more than one TOPIC!");
node.log(topics);
}

if (topics.length >= 1) {
return topics[0];
}

return null;
}


function updateNodeStatus() {
switch (radio.state) {
case 'connecting':
Expand Down
32 changes: 23 additions & 9 deletions flexradio-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ module.exports = function(RED) {
return;
}

node.on('input', function(msg_in) {
node.radio.send(msg_in, function(msg_out) {
if (!('payload' in msg_out)
|| !msg_out.payload
|| Object.keys(msg_out.payload).length === 0) {
msg_out.payload = msg_in.payload;
}
node.on('input', function(msg, send, done) {
if (Array.isArray(msg.payload)) {
msg.payload.forEach(function(payload) {
sendRequest({ ...msg, 'payload': payload }, send);
})
} else {
sendRequest(msg, send);
}

node.send(msg_out);
});
if (done) {
done();
}
});

const radio = node.radio;
Expand All @@ -38,6 +40,18 @@ module.exports = function(RED) {
updateNodeStatus();
});

function sendRequest(msg, send) {
radio.send(msg, function(msg_out) {
if (!('payload' in msg_out)
|| !msg_out.payload
|| Object.keys(msg_out.payload).length === 0) {
msg_out.payload = msg.payload;
}

send(msg_out);
});
}

function updateNodeStatus() {
switch (radio.state) {
case 'connecting':
Expand Down
36 changes: 36 additions & 0 deletions flexradio-status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script type="text/javascript">
RED.nodes.registerType('flexradio-status', {
category: 'flexradio',
color: '#a6bbcf',
defaults: {
name: {value: ''},
radio: {value: '', type: 'flexradio-radio', required: true},
client_handle: {value: ''}
},
inputs: 0,
outputs: 1,
icon: 'file.png',
label: function() {
return this.name || 'flexradio-status';
}
});
</script>

<script type="text/html" data-template-name="flexradio-status">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-radio"><i class="fa fa-sign-in"></i> FlexRadio</label>
<input type="text" id="node-input-radio" placeholder="FlexRadio">
</div>
<div id="client-handle" class="form-row" style="padding-left:20px;">
<label for="node-input-client_handle"><i class="fa fa-tag"></i> Handle</label>
<input type="text" id="node-input-client_handle" placeholder="Client Handle (optional)">
</div>
</script>

<script type="text/html" data-help-name="flexradio-status">
<p>Receives FlexRadio 6xxx series status messages and inects them into the flow. This includes radio connection changes.</p>
</script>
91 changes: 91 additions & 0 deletions flexradio-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

module.exports = function(RED) {
"use strict"

function FlexRadioStatusNode(config) {
RED.nodes.createNode(this, config);

const node = this;
node.name = config.name;
node.radio = RED.nodes.getNode(config.radio);
node.clientHandle = config.client_handle;

if (!node.radio) { // No config node configured, should not happen
node.status({fill:'red', shape:'circle', text:'not configured'});
return;
}

const radio = node.radio;
radio.on('connecting', function() {
updateNodeStatus();
});

radio.on('connected', function() {
updateNodeStatus();
});

radio.on('disconnected', function() {
updateNodeStatus();
});

radio.on('status', function(status_data) {
node.log(JSON.stringify(status_data));
const topic = extractMessageTopic(status_data);
const status_msg = {
topic: topic,
client: status_data.client,
payload: status_data[topic]
};

node.send(status_msg);
});

function extractMessageTopic(message) {
// remove 'header' fields and find topical fields of message
const topics = Object.keys(message).filter(function(key) {
return !(['type', 'client'].includes(key));
});

if (topics.length != 1) {
node.log("ERROR: message from radio has more than one TOPIC!");
node.log(topics);
}

if (topics.length >= 1) {
return topics[0];
}

return null;
}

function updateNodeStatus() {
switch (radio.state) {
case 'connecting':
node.status({fill:'green', shape:'circle', text:'connecting'});
break;
case 'connected':
node.status({fill:'green', shape:'dot', text:radio.radioName()});
break;
case 'disconnected':
node.status({fill:'red', shape:'dot', text:'not connected'});
break;
default:
node.status({fill:'red', shape:'circle', text:radio.state});
break;
}

// Inject changes in radio state to the flow
const status_msg = {
topic: 'connection',
client: null,
payload: radio.state
};

node.send(status_msg);
}

updateNodeStatus();
}

RED.nodes.registerType("flexradio-status", FlexRadioStatusNode);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-flexradio",
"version": "0.2.1",
"version": "0.3.0",
"description": "FlexRadio Network Control and Monitoring Nodes",
"main": "index.js",
"scripts": {
Expand All @@ -18,6 +18,7 @@
"flexradio-radio": "flexradio-radio.js",
"flexradio-request": "flexradio-request.js",
"flexradio-messages": "flexradio-messages.js",
"flexradio-status": "flexradio-status.js",
"flexradio-meter": "flexradio-meter.js",
"flexradio-decode": "flexradio-decode.js"
}
Expand Down

0 comments on commit e4041b6

Please sign in to comment.