forked from Azure/azure-iot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_sample_module_twin.js
67 lines (56 loc) · 1.88 KB
/
simple_sample_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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 ModuleClient = require('azure-iot-device').ModuleClient;
var Protocol = require('azure-iot-device-mqtt').Mqtt;
var fs = require('fs');
var connectionString = process.env.EdgeHubConnectionString;
var client = ModuleClient.fromConnectionString(connectionString, Protocol);
console.log('got client');
client.on('error', function (err) {
console.error(err.message);
});
client.setOptions({
ca: fs.readFileSync(process.env.EdgeModuleCACertificateFile).toString('ascii')
}, function(err) {
if (err) {
console.log('error:' + err);
} else {
// connect to the edge instance
client.open(function(err) {
if (err) {
console.error('could not open IotHub client');
} else {
console.log('client opened');
// Create device Twin
client.getTwin(function(err, twin) {
if (err) {
console.error('could not get twin');
} else {
console.log('twin created');
console.log('twin contents:');
console.log(twin.properties);
twin.on('properties.desired', function(delta) {
console.log('new desired properties received:');
console.log(JSON.stringify(delta));
});
// create a patch to send to the hub
var patch = {
updateTime: new Date().toString(),
firmwareVersion:'1.2.1',
weather:{
temperature: 72,
humidity: 17
}
};
// send the patch
twin.properties.reported.update(patch, function(err) {
if (err) throw err;
console.log('twin state reported');
});
}
});
}
});
}
});