Skip to content

Commit

Permalink
Added dynamic deviceid selection
Browse files Browse the repository at this point in the history
Added dynamic deviceid selection in msg.deviceid
  • Loading branch information
dtabuencav committed May 6, 2017
1 parent 0c933d0 commit 60e2ce9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 66 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# azureiotc2d
*detailed instructions to come!
Node to send Azure IoT C2D messages
Azure IoT Hub - Send a Cloud to Device message

This is a node-red node to send Cloud to Device messages from a node-red flow.
The content of ´{msg.payload} will be sent to the device specified in the node.
Inputs

msg.payload is used as the payload of the sent message.

The deviceid used can be configured in the node or, if left blank, can be set by msg.deviceid.
21 changes: 20 additions & 1 deletion azureiotc2d/azureiotc2d.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,26 @@
</script>

<script type="text/x-red" data-help-name="azureiotc2d">
<p>Azure IoT Hub node for sending message to a device</p>
<p>Send a Cloud to Device message</p>

<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload

</dt>
<dd> the payload of the message to send </dd>
<dt class="optional">deviceid</dt>
<dd> the deviceid to send message to</dd>
</dl>



<h3>Details</h3>
<p><code>msg.payload</code> is used as the payload of the sent message.
</p>
<p>The deviceid used can be configured in the node or, if left blank, can be set
by <code>msg.deviceid</code>.</p>

</script>

<script type="text/javascript">
Expand Down
66 changes: 41 additions & 25 deletions azureiotc2d/azureiotc2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;

//var serviceClient = Client.fromConnectionString(connectionString);
var serviceClient = null;

/* Node for configuring Azure IoT */

Expand Down Expand Up @@ -38,48 +38,64 @@

if (node.endpointConfig) {

node.deviceid = n.deviceid;

node.deviceid = n.deviceid;



} else {
console.log("Missing Endpoint");
}

this.status({fill:"red",shape:"ring",text:"waiting"});


this.status({fill:"blue",shape:"ring",text:"waiting"});

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

if (serviceClient){
console.log(serviceClient);


if (serviceClient){
serviceClient.removeAllListeners();
serviceClient.close(printResultFor('close'));
//serviceClient = null;

}
serviceClient = null;
}



var serviceClient = Client.fromConnectionString(this.endpointConfig.connString);
if (msg.deviceid != null){
node.deviceid = msg.deviceid;
}

serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
serviceClient.getFeedbackReceiver(receiveFeedback);
var message = new Message('Cloud to device message.');
message.ack = 'full';
console.log('Sending message: ' + msg.payload);
node.status({fill:"red",shape:"ring",text:"sending"});
serviceClient.send(node.deviceid, msg.payload, printResultFor('send'));

}
});
if (err) {
console.error('Could not connect: ' + err.message);
} else {

serviceClient.getFeedbackReceiver(receiveFeedback);
var message = new Message('Cloud to device message.');
message.ack = 'full';
msg.ack = 'full';

console.log('Sending message: ' + msg.payload);

node.status({fill:"red",shape:"ring",text:"sending"});
serviceClient.send(node.deviceid, msg.payload, printResultFor('send'));
node.status({fill:"red",shape:"ring",text:"sent"});
}
});





});

this.on('close', function() {

serviceClient.close(printResultFor('close'));
if (serviceClient){
serviceClient.removeAllListeners();
serviceClient.close(printResultFor('close'));
serviceClient = null;
}
});

};
Expand Down
35 changes: 0 additions & 35 deletions npm-debug.log

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name" : "node-red-contrib-azureiotc2d",
"version" : "0.0.7",
"version" : "0.0.8",
"description" : "node to send Azure IoT C2D",
"author": {
"name": "Daniel Tabuenca",
},
"dependencies": {
"azure-iothub": "1.1.10",
"azure-iot-common": "1.1.7"
Expand Down

0 comments on commit 60e2ce9

Please sign in to comment.