This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathiothub-explorer-monitor-feedback.js
58 lines (48 loc) · 2.07 KB
/
iothub-explorer-monitor-feedback.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
#!/usr/bin/env node
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
// external dependencies
var program = require('commander');
var chalk = require('chalk');
var prettyjson = require('prettyjson');
// local dependencies
var inputError = require('./common.js').inputError;
var serviceError = require('./common.js').serviceError;
var getSas = require('./common.js').getSas;
// Azure Event Hubs dependencies
var ServiceClient = require('azure-iothub').Client;
var showDeprecationText = require('./common.js').showDeprecationText;
showDeprecationText('az iot hub monitor-feedback');
program
.description('Monitor feedback messages sent by devices when they receive a cloud-to-device (c2d) message.')
.option('-l, --login <connectionString>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
.option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
.parse(process.argv);
var sas = getSas(program.login);
var client = ServiceClient.fromSharedAccessSignature(sas.toString());
client.open(function (err) {
if (err) {
inputError('Could not open the connection to the service: ' + err.message);
} else {
client.getFeedbackReceiver(function (err, receiver) {
if (err) serviceError(err);
if (!program.raw) {
console.log(chalk.yellow('Waiting for feedback...') + ' (Ctrl-C to quit)');
}
receiver.on('errorReceived', function (err) { serviceError(err); });
receiver.on('message', function (feedbackRecords) {
var records = JSON.parse(feedbackRecords.data);
var output = {
originalMessageId: records[0].originalMessageId,
'iothub-enqueuedtime': records[0].enqueuedTimeUtc,
body: records[0].description
};
var rendered = program.raw ?
JSON.stringify(output) :
'\nFeedback message\n' + prettyjson.render(output) + '\n--------------\n';
console.log(rendered);
});
});
}
});