forked from Azure/azure-iot-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_monitoring.js
121 lines (105 loc) · 3.53 KB
/
remote_monitoring.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
// 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 Protocol = require('azure-iot-device-http').Http;
var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-device').ConnectionString;
var Message = require('azure-iot-device').Message;
// String containing Hostname, Device Id & Device Key in the following formats:
// "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
var connectionString = '[IoT Hub device connection string]';
var deviceId = ConnectionString.parse(connectionString).DeviceId;
// Sensors data
var temperature = 50;
var humidity = 50;
var externalTemperature = 55;
// Create IoT Hub client
var client = Client.fromConnectionString(connectionString, Protocol);
// Helper function to print results for an operation
function printErrorFor(op) {
return function printError(err) {
if (err) console.log(op + ' error: ' + err.toString());
};
}
// Helper function to generate random number between min and max
function generateRandomIncrement() {
return ((Math.random() * 2) - 1);
}
// Send device meta data
var deviceMetaData = {
'ObjectType': 'DeviceInfo',
'IsSimulatedDevice': 0,
'Version': '1.0',
'DeviceProperties': {
'DeviceID': deviceId,
'HubEnabledState': 1,
'CreatedTime': '2015-09-21T20:28:55.5448990Z',
'DeviceState': 'normal',
'UpdatedTime': null,
'Manufacturer': 'Contoso Inc.',
'ModelNumber': 'MD-909',
'SerialNumber': 'SER9090',
'FirmwareVersion': '1.10',
'Platform': 'node.js',
'Processor': 'ARM',
'InstalledRAM': '64 MB',
'Latitude': 47.617025,
'Longitude': -122.191285
},
'Commands': [{
'Name': 'SetTemperature',
'Parameters': [{
'Name': 'Temperature',
'Type': 'double'
}]
},
{
'Name': 'SetHumidity',
'Parameters': [{
'Name': 'Humidity',
'Type': 'double'
}]
}]
};
client.open(function (err) {
if (err) {
printErrorFor('open')(err);
} else {
console.log('Sending device metadata:\n' + JSON.stringify(deviceMetaData));
client.sendEvent(new Message(JSON.stringify(deviceMetaData)), printErrorFor('send metadata'));
client.on('message', function (msg) {
console.log('receive data: ' + msg.getData());
try {
var command = JSON.parse(msg.getData());
if (command.Name === 'SetTemperature') {
temperature = command.Parameters.Temperature;
console.log('New temperature set to :' + temperature + 'F');
}
client.complete(msg, printErrorFor('complete'));
}
catch (err) {
printErrorFor('parse received message')(err);
client.reject(msg, printErrorFor('reject'));
}
});
// start event data send routing
var sendInterval = setInterval(function () {
temperature += generateRandomIncrement();
externalTemperature += generateRandomIncrement();
humidity += generateRandomIncrement();
var data = JSON.stringify({
'DeviceID': deviceId,
'Temperature': temperature,
'Humidity': humidity,
'ExternalTemperature': externalTemperature
});
console.log('Sending device event data:\n' + data);
client.sendEvent(new Message(data), printErrorFor('send event'));
}, 1000);
client.on('error', function (err) {
printErrorFor('client')(err);
if (sendInterval) clearInterval(sendInterval);
client.close(printErrorFor('client.close'));
});
}
});