forked from Azure/azure-iot-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_sample.js
45 lines (36 loc) · 1.54 KB
/
registry_sample.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
// 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 iothub = require('azure-iothub');
var connectionString = '[IoT Connection String]';
var registry = iothub.Registry.fromConnectionString(connectionString);
// List devices
console.log('**listing devices...');
registry.list(function (err, deviceList) {
deviceList.forEach(function (device) {
var key = device.authentication ? device.authentication.symmetricKey.primaryKey : '<no primary key>';
console.log(device.deviceId + ': ' + key);
});
// Create a new device
var device = {
deviceId: 'sample-device-' + Date.now()
};
console.log('\n**creating device \'' + device.deviceId + '\'');
registry.create(device, printAndContinue('create', function next() {
// Get the newly-created device
console.log('\n**getting device \'' + device.deviceId + '\'');
registry.get(device.deviceId, printAndContinue('get', function next() {
// Delete the new device
console.log('\n**deleting device \'' + device.deviceId + '\'');
registry.delete(device.deviceId, printAndContinue('delete'));
}));
}));
});
function printAndContinue(op, next) {
return function printResult(err, deviceInfo, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.statusCode + ' ' + res.statusMessage);
if (deviceInfo) console.log(op + ' device info: ' + JSON.stringify(deviceInfo));
if (next) next();
};
}