This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
node_device.js
72 lines (58 loc) · 2.18 KB
/
node_device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
var querystring = require('querystring');
var client = null;
var deviceConnectionString;
function main() {
// receive the IoT Hub connection string as a command line parameter
if(process.argv.length < 3) {
console.error('Usage: node_device.js <<IoT Hub Device Connection String>>');
process.exit(1);
}
// open a connection to the device
deviceConnectionString = process.argv[2];
client = Client.fromConnectionString(deviceConnectionString, Protocol);
client.open(onConnect);
}
function onConnect(err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
client.on('message', function (msg) {
console.log('Received Message from IoT Hub: ' + msg.data);
client.complete(msg, printResultFor('completed'));
// We are done here
setTimeout(function(){ process.exit(1);}, 3000);
});
client.on('error', function (err) {
console.error(err.message);
});
client.on('disconnect', function () {
clearInterval(sendInterval);
client.removeAllListeners();
client.connect(connectCallback);
});
// Create a message and send it to the IoT Hub
// Extract deviceId from connection string
var dId = querystring.parse(deviceConnectionString, ';', null, null).DeviceId;
// Creating JSON message to send to Azure IoT Hub
var data = JSON.stringify({ deviceId: dId, message: "Hello echo" });
var message = new Message(data);
console.log('Sending message: ' + message.getData());
// Sending message to IoT Hub
client.sendEvent(message, printResultFor('send'));
}
}
// Helper function to print results in the console
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.constructor.name);
};
}
main();