-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (82 loc) · 2.96 KB
/
index.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
'use strict';
const IotHubProtocol = require('azure-iot-device-mqtt').Mqtt;
const IotHubClient = require('azure-iot-device').Client;
const IotHubMessage = require('azure-iot-device').Message;
const mqtt = require('mqtt')
function onIotHubMessage(msg) {
console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
};
function onIotHubStart(request, response) {
console.log('Try to invoke method start(' + request.payload || '' + ')');
response.send(200, 'Successully start sending message to cloud', function (err) {
if (err) {
console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
}
});
}
function ontIotHubStop(request, response) {
console.log('Try to invoke method stop(' + request.payload || '' + ')')
response.send(200, 'Successully stop sending message to cloud', function (err) {
if (err) {
console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
}
});
}
function iotHubInit(iotHubSAS) {
console.log('Connecting to IoT Hub.');
const iotHubClient = IotHubClient.fromSharedAccessSignature(iotHubSAS, IotHubProtocol);
iotHubClient.open((err) => {
if (err) {
console.error('IoT hub client connect error: ' + err.message);
return;
}
console.log('IoTHub connection successful.');
iotHubClient.on('message', onIotHubMessage);
iotHubClient.on('error', function (err) {
console.error(err.message);
});
iotHubClient.on('disconnect', function () {
clearInterval(sendInterval);
iotHubClient.removeAllListeners();
iotHubClient.open(connectCallback);
});
iotHubClient.onDeviceMethod('start', onIotHubStart);
iotHubClient.onDeviceMethod('stop', ontIotHubStop);
});
return iotHubClient;
}
function mqttInit(mqttServer) {
console.log('Connecting to mqtt broker.');
const mqttClient = mqtt.connect(mqttServer)
mqttClient.on('connect', function () {
console.log('Connection to mqtt broker succesful.');
mqttClient.subscribe('#', function (err) {
if (err) {
console.error('Mqtt client connect error: ' + err.message);
}
})
});
mqttClient.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
});
return mqttClient;
}
(function (iotHubSAS, mqttServer) {
if (!mqttServer) {
mqttServer = 'mqtt://localhost';
}
console.log(`Mqtt server: ${mqttServer}.`);
iotHubSAS = iotHubSAS || process.env['AzureIoTHubDeviceSAS'];
if (!iotHubSAS) {
console.error('Please define AzureIoTHubDeviceSAS env var or pass it as first param.');
return;
}
const iotHubClient = iotHubInit(iotHubSAS);
const mqttClient = mqttInit(mqttServer);
mqttClient.on('message', function (topic, message) {
const iotHubMessage = new IotHubMessage(topic + ' ' + message);
// iotHubMessage.properties.add('randomPropertyName', 'randomPropertyValue');
iotHubClient.sendEvent(iotHubMessage);
});
})(process.argv[2], process.argv[3]);