This repository was 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
Expand file tree
/
Copy pathiothub-explorer-query-twin.js
More file actions
45 lines (38 loc) · 1.55 KB
/
iothub-explorer-query-twin.js
File metadata and controls
45 lines (38 loc) · 1.55 KB
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
#!/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';
var program = require('commander');
var prettyjson = require('prettyjson');
var inputError = require('./common.js').inputError;
var serviceError = require('./common.js').serviceError;
var getSas = require('./common.js').getSas;
var Registry = require('azure-iothub').Registry;
var showDeprecationText = require('./common.js').showDeprecationText;
showDeprecationText('az iot hub query');
program
.description('Query the registry for twins matching the SQL query passed as argument')
.usage('[options] <device-id>')
.option('-l, --login <connection-string>', 'use the connection string provided as argument to use to authenticate with your IoT Hub instance')
.option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
.parse(process.argv);
if(!program.args[0]) inputError('You must specify a SQL query.');
var sqlQuery = program.args[0];
var sas = getSas(program.login);
var registry = Registry.fromSharedAccessSignature(sas);
var query = registry.createQuery(sqlQuery);
var onNewResults = function(err, results) {
if (err) {
serviceError(err);
} else {
results.forEach(function(twin) {
delete twin._registry;
var output = program.raw ? JSON.stringify(twin) : prettyjson.render(twin);
console.log(output);
});
if(query.hasMoreResults) {
query.nextAsTwin(onNewResults);
}
}
};
query.nextAsTwin(onNewResults);