forked from Azure/azure-iot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
85 lines (74 loc) · 3 KB
/
query.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
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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 provisioningServiceClient = require('azure-iot-provisioning-service').ProvisioningServiceClient;
var argv = require('yargs')
.usage('Usage: $0 --connectionString <DEVICE PROVISIONING CONNECTION STRING> ')
.option('connectionString', {
alias: 'c',
describe: 'The connection string for the Device Provisioning instance',
type: 'string',
demandOption: true
})
.argv;
var connectionString = argv.connectionString;
var serviceClient = provisioningServiceClient.fromConnectionString(connectionString);
var queryForEnrollments = serviceClient.createIndividualEnrollmentQuery({
"query": "*"
}, 10);
var queryForEnrollmentGroups = serviceClient.createEnrollmentGroupQuery({
"query": "*"
}, 10);
var onEnrollmentResults = function (err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
// Do something with the results
results.forEach(function (enrollment) {
console.log(JSON.stringify(enrollment, null, 2));
});
if (queryForEnrollments.hasMoreResults) {
queryForEnrollments.next(onEnrollmentResults);
} else {
console.log('Querying for the Enrollment Groups');
queryForEnrollmentGroups.next(onEnrollmentGroupResults);
}
}
};
var onEnrollmentGroupResults = function (err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
// Do something with the results
results.forEach(function (enrollmentGroup) {
console.log(JSON.stringify(enrollmentGroup, null, 2));
var alreadyPrintedSomeDeviceRegistrations = false;
var queryForDeviceRegistrationState = serviceClient.createEnrollmentGroupDeviceRegistrationStateQuery({
"query": "*"
}, enrollmentGroup.enrollmentGroupId, 10);
var onDeviceRegistrationStateResults = function (err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
// Do something with the results
results.forEach(function (deviceRegistrationState) {
if (!alreadyPrintedSomeDeviceRegistrations) {
alreadyPrintedSomeDeviceRegistrations = true;
console.log('For ' + enrollmentGroup.enrollmentGroupId + ', all of its the Device Registrations Status objects: ')
}
console.log(JSON.stringify(deviceRegistrationState, null, 2));
});
if (queryForDeviceRegistrationState.hasMoreResults) {
queryForDeviceRegistrationState.next(onDeviceRegistrationState);
}
}
};
queryForDeviceRegistrationState.next(onDeviceRegistrationStateResults);
});
if (queryForEnrollmentGroups.hasMoreResults) {
queryForEnrollmentGroups.next(onEnrollmentGroupResults);
}
}
};
console.log('Querying for the enrollments: ');
queryForEnrollments.next(onEnrollmentResults);