forked from Azure/azure-iot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule_sample.js
98 lines (83 loc) · 2.57 KB
/
module_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
// 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 Hub Connection String]";
var deviceId = '[Device ID]';
var moduleId = '[Module ID]';
var registry = iothub.Registry.fromConnectionString(connectionString);
var getModulesOnDevice = function(done) {
console.log();
console.log('getting all modules from device ' + deviceId);
registry.getModulesOnDevice(deviceId, function(err, modules) {
if (err) {
console.log('getModulesOnDevice failed ' + err);
done();
} else {
console.log(modules.length.toString() + ' modules found');
modules.forEach(function(module) {
console.log(module.moduleId);
});
done();
}
});
};
var addNewModule = function(done) {
console.log();
console.log('adding new module with moduleId=' + moduleId);
registry.addModule({deviceId: deviceId, moduleId: moduleId}, function(err) {
if (err) {
console.log('addModule failed ' + err);
done();
} else {
console.log('addModule succeeded');
done();
}
});
};
var updateModule = function(done) {
console.log();
console.log('updating module with moduleId=' + moduleId);
registry.getModule(deviceId, moduleId, function(err, module) {
if (err) {
console.log('getModule failed ' + err);
done();
} else {
console.log('getModule succeeded');
var oldPrimary = module.authentication.symmetricKey.primaryKey;
module.authentication.symmetricKey.primaryKey = module.authentication.symmetricKey.secondaryKey;
module.authentication.symmetricKey.secondaryKey = oldPrimary;
console.log('using updateModule to set primary key to ' + module.authentication.symmetricKey.primaryKey);
registry.updateModule(module, function(err) {
if (err) {
console.log('updateModule failed ' + err);
done();
} else {
console.log('updateModule succeeded');
done();
}
});
}
});
};
var removeModule = function(done) {
console.log();
console.log('removing module with moduleId=' + moduleId);
registry.removeModule(deviceId, moduleId, function(err) {
if (err) {
console.log('removeModule failed ' + err);
done();
} else {
console.log('removeModule succeeded');
done();
}
});
};
getModulesOnDevice(function() {
addNewModule(function() {
updateModule(function() {
removeModule(function() {
});
});
});
});