forked from Azure/azure-iot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule_twin.js
46 lines (41 loc) · 1.34 KB
/
module_twin.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
// 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 Registry = require('azure-iothub').Registry;
var connectionString = "[IoT Hub Connection String]";
var deviceId = '[Device ID]';
var moduleId = '[Module ID]';
var registry = Registry.fromConnectionString(connectionString);
registry.getModuleTwin(deviceId, moduleId, function(err, twin) {
if (err) {
console.error(err.message);
} else {
console.log(JSON.stringify(twin, null, 2));
var twinPatch = {
tags: {
city: "Redmond"
},
properties: {
desired: {
telemetryInterval: 1000
}
}
};
// method 1: using the update method directly on the twin
twin.update(twinPatch, function(err, twin) {
if (err) {
console.error(err.message);
} else {
console.log(JSON.stringify(twin, null, 2));
// method 2: using the updateTwin method on the Registry object
registry.updateModuleTwin(twin.deviceId, twin.moduleId, { properties: { desired: { telemetryInterval: 2000 }}}, twin.etag, function(err, twin) {
if (err) {
console.error(err.message);
} else {
console.log(JSON.stringify(twin, null, 2));
}
});
}
});
}
});