forked from Azure/azure-iot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregistry_addUpdateRemoveDevicesSample.js
92 lines (81 loc) · 2.27 KB
/
registry_addUpdateRemoveDevicesSample.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
86
87
88
89
90
91
92
// 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 uuid = require('uuid');
var connectionString = '[IoT Connection String]';
var registry = iothub.Registry.fromConnectionString(connectionString);
// Specify the new devices.
var deviceAddArray = [
{
deviceId: 'Device1',
status: 'disabled',
authentication: {
symmetricKey: {
primaryKey: new Buffer(uuid.v4()).toString('base64'),
secondaryKey: new Buffer(uuid.v4()).toString('base64')
}
}
},
{
deviceId: 'Device2',
status: 'disabled',
authentication: {
symmetricKey: {
primaryKey: new Buffer(uuid.v4()).toString('base64'),
secondaryKey: new Buffer(uuid.v4()).toString('base64')
}
}
},
{
deviceId: 'Device3',
status: 'disabled',
authentication: {
symmetricKey: {
primaryKey: new Buffer(uuid.v4()).toString('base64'),
secondaryKey: new Buffer(uuid.v4()).toString('base64')
}
}
}
];
var deviceUpdateArray = [
{
deviceId: deviceAddArray[0].deviceId,
status: 'enabled'
},
{
deviceId: deviceAddArray[1].deviceId,
status: 'enabled'
},
{
deviceId: deviceAddArray[2].deviceId,
status: 'enabled'
}
];
var deviceRemoveArray = [
{
deviceId: deviceAddArray[0].deviceId
},
{
deviceId: deviceAddArray[1].deviceId
},
{
deviceId: deviceAddArray[2].deviceId
}
];
console.log('Adding devices: ' + JSON.stringify(deviceAddArray));
registry.addDevices(deviceAddArray, printAndContinue( 'adding', function next() {
registry.updateDevices(deviceUpdateArray, true, printAndContinue('updating', function next() {
registry.removeDevices(deviceRemoveArray, true, printAndContinue('removing'));
}));
}));
function printAndContinue(op, next) {
return function printResult(err, resultData) {
if (err) console.log(op + ' error: ' + err.toString());
if (resultData) {
var arrayString = resultData.errors.length === 0 ? 'no errors' : JSON.stringify(resultData.errors);
console.log(op + ' isSuccessful: ' + resultData.isSuccessful + ', errors returned: ' + arrayString);
}
if (next) next();
};
}