Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicast downlink #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"node-red": {
"nodes": {
"device-event": "src/device-event.js",
"device-downlink": "src/device-downlink.js"
"device-downlink": "src/device-downlink.js",
"multicast-downlink": "src/multicastGroup-downlink.js"
}
}
}
74 changes: 74 additions & 0 deletions src/multicastGroup-downlink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<script type="text/javascript">
RED.nodes.registerType('multicast group downlink',{
category: 'ChirpStack',
color: '#a6bbcf',
defaults: {
name: {value:""},
server: {value: ""},
useTls: {value: false},
apiToken: {value: ""},
encoding: {value: "hex"}
},
inputs:1,
outputs:1,
icon: "bridge.svg",
label: function() {
return this.name || "multicast group downlink";
}
});
</script>

<script type="text/html" data-template-name="multicast group downlink">
<div class="form-row">
<label for="node-input-server"><i class="fa fa-server"></i> Server</label>
<input type="text" id="node-input-server" placeholder="hostname:port, e.g. localhost:8080 or example.com:443">
</div>
<div class="form-row">
<label for="node-input-useTls"><i class="fa fa-lock"></i> Use TLS</label>
<input type="checkbox" id="node-input-useTls">
</div>
<div class="form-row">
<label for="node-input-apiToken"><i class="fa fa-key"></i> API Token</label>
<input type="password" id="node-input-apiToken" placeholder="API Token">
</div>
<div class="form-row">
<label for="node-input-encoding"><i class="fa fa-file-code-o"></i> Payload Encoding</label>
<select id="node-input-encoding">
<option value="hex">HEX</option>
<option value="base64">Base64</option>
</select>
</div>
<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>
</script>

<script type="text/html" data-help-name="multicast group downlink">
<p>
Node to enqueue a multicast group downlink payload for the given <code>multicastGroupId</code>.
</p>

<h3>Details</h3>
<p>
This node enqueues the input payload using the ChirpStack gRPC API.
This requires access to the ChirpStack gRPC endpoint and an API token,
which can be retrieved using the ChirpStack web-interface.
</p>

<h3>Input</h3>
<dl class="message-properties">
<dt>multicastGroupId <span class="property-type">string</span></dt>
<dd>Multicast group Id (e.g. <code>81d5545f-973f-4c45-87f2-f917d509581d</code>).</dd>
<dt>fPort <span class="property-type">number</span></dt>
<dd>Downlink FPort.</dd>
<dt>payload <span class="property-type">string</span></dt>
<dd>HEX or BASE64 encoded payload (select the encoding in the node settings). If <code>payload</code> is <code>undefined</code>, then an empty downlink will be enqueued.</dd>
</dl>

<h3>Output</h3>
<dl class="message-properties">
<dt>fCnt <span class="property-type">string</span></dt>
<dd>Frame counter.</dd>
</dl>
</script>
64 changes: 64 additions & 0 deletions src/multicastGroup-downlink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = function(RED) {
"use strict";
var multicastGroup = require("@chirpstack/chirpstack-api/api/multicast_group_grpc_pb");
var multicastGroup_pb = require("@chirpstack/chirpstack-api/api/multicast_group_pb");
var grpc = require("@grpc/grpc-js");

function MulticastGroupDownlink(config) {
RED.nodes.createNode(this, config);
var node = this;
var client = null;

if (config.useTls) {
client = new multicastGroup.MulticastGroupServiceClient(config.server, grpc.credentials.createSsl());
} else {
client = new multicastGroup.MulticastGroupServiceClient(config.server, grpc.credentials.createInsecure());
}


var meta = new grpc.Metadata();
meta.add('authorization', 'Bearer ' + config.apiToken);


node.on("input", function(msg) {

var item = new multicastGroup_pb.MulticastGroupQueueItem();
var req = new multicastGroup_pb.EnqueueMulticastGroupQueueItemRequest();

if (msg.multicastGroupId === undefined) {
node.error("multicastGroupId is undefined");
return;
} else {
item.setMulticastGroupId(msg.multicastGroupId);
}

if (msg.fPort === undefined) {
node.error("fPort is undefined");
return;
} else {
item.setFPort(msg.fPort);
}

if (msg.payload !== undefined) {
item.setData(Buffer.from(msg.payload, config.encoding).toString("base64"));
} else {
node.log("payload is undefined, assuming empty downlink frame");
}

req.setQueueItem(item);
client.enqueue(req, meta, function(err, resp) {
if (err !== null) {
node.error("Enqueue error: ", err);
} else {
node.log("Downlink enqueued");

node.send({
fCnt: resp.getFCnt()
});
}
});
});
}

RED.nodes.registerType("multicast group downlink", MulticastGroupDownlink);
}