forked from Azure/azure-iot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregistry_bulk_sample.js
131 lines (116 loc) · 6.65 KB
/
registry_bulk_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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 azureStorage = require('azure-storage');
var iothubConnectionString = '[IoT Connection String]';
var storageConnectionString = '[Storage Connection String]';
var registry = iothub.Registry.fromConnectionString(iothubConnectionString);
var blobSvc = azureStorage.createBlobService(storageConnectionString);
var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);
var inputSharedAccessPolicy = {
AccessPolicy: {
Permissions: 'rl',
Start: startDate,
Expiry: expiryDate
},
};
var outputSharedAccessPolicy = {
AccessPolicy: {
Permissions: 'rwd',
Start: startDate,
Expiry: expiryDate
},
};
var inputContainerName = 'importcontainer';
var outputContainerName = 'exportcontainer';
var deviceFile = 'devices.txt';
blobSvc.createContainerIfNotExists(inputContainerName, function (error) {
if(error) {
console.error('Could not create input container: ' + error.message);
} else {
var inputSasToken = blobSvc.generateSharedAccessSignature(inputContainerName, null, inputSharedAccessPolicy);
var inputSasUrl = blobSvc.getUrl(inputContainerName, null, inputSasToken);
blobSvc.createBlockBlobFromLocalFile(inputContainerName, deviceFile, deviceFile, function (error) {
if (error) {
console.error('Could not create devices.txt: ' + error.message);
} else {
blobSvc.createContainerIfNotExists(outputContainerName, function (error) {
if (error) {
console.error('Could not create output container: ' + error.message);
} else {
var outputSasToken = blobSvc.generateSharedAccessSignature(outputContainerName, null, outputSharedAccessPolicy);
var outputSasUrl = blobSvc.getUrl(outputContainerName, null, outputSasToken);
/**
* There can only be one active job at a time, therefore, you can uncomment the export section and comment the import
* session or vice versa depending on what part of the code you would like to test.
*/
// registry.exportDevicesToBlob(outputSasUrl, false, function (error, result) {
// if (error) {
// console.error('Could not create export job: ' + error.message);
// } else {
// console.log('--------------\r\nDevices Export Job Identifier:--------------\r\n' + result);
// var jobId = result.jobId;
// var interval = setInterval(function () {
// registry.getJob(jobId, function (error, result) {
// if (error) {
// console.error('Could not get job status: ' + error.message + ' : ' + error.responseBody);
// } else {
// console.log('--------------\r\njob ' + jobId + ' status:\r\n--------------\r\n' + result);
// var status = result.status;
// if (status === "completed") {
// clearInterval(interval);
// }
// }
// });
// }, 500);
// }
// });
registry.importDevicesFromBlob(inputSasUrl, outputSasUrl, function (error, result) {
if (error) {
console.error('Could not create import devices: ' + error.message + ' : ' + error.responseBody);
} else {
console.log('--------------\r\nDevices Import Job Identifier:--------------\r\n' + result);
var jobId = result.jobId;
var interval = setInterval(function () {
/**
* Uncomment this code to test cancelling a job.
*/
// registry.cancelJob(jobId, function (error, result) {
// if (error) {
// console.error('Could not cancel job: ' + error.message + ' : ' + error.responseBody);
// } else {
// console.log('--------------\r\njob ' + jobId + ' cancelled:\r\n--------------\r\n' + result);
// clearInterval(interval);
// }
// });
registry.getJob(jobId, function (error, result) {
if (error) {
console.error('Could not get job status: ' + error.message + ' : ' + error.responseBody);
} else {
console.log('--------------\r\njob ' + jobId + ' status:\r\n--------------\r\n' + result);
var status = result.status;
if (status === "completed") {
clearInterval(interval);
}
}
});
}, 500);
}
});
registry.listJobs(function(error, result) {
if (error) {
console.error('Could not list jobs: ' + error.message + ' : ' + error.responseBody);
} else {
console.log('Job list:\r\n----------\r\n' + result);
}
});
}
});
}
});
}
});