-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (78 loc) · 2.61 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
const express = require('express');
const mqtt = require('mqtt'); // Using MQTT.js npm
const app = express();
app.get('/', (req, res) => {
res.send('Hello WISE-PaaS!');
});
// Typically in the hosting environment for node application, there's an env variable called 'PORT'
const port = process.env.PORT || 3030;
const server = app.listen(port, () => console.log(`Listening on port ${port}...`));
// Start Config
var config = {};
config.mqtt = {};
/** Modify this config ***/
// SYS
config.timeout = 120 * 1000;
// Local environment MQTT configuration
config.mqtt.broker = "MODIFY_BROKER_HOST"; // mqtt://broker-host
config.mqtt.username = "MODIFY_YOUR_USERNAME";
config.mqtt.port = 1883;
config.mqtt.password = "MODIFY_YOUR_PASSWORD";
/*****/
config.mqtt.serviceName = "p-rabbitmq"; // 'p-rabbitmq'
var vcap_services = {
"p-rabbitmq": [
{
"credentials": {
"protocols": {
"mqtt": {
"username": config.mqtt.username,
"password": config.mqtt.password,
"port": config.mqtt.port,
"host": config.mqtt.broker
}
}
}
}
]
};
if (process.env.VCAP_SERVICES != null) {
console.log("Using VCAP_SERVICES");
vcap_services = JSON.parse(process.env.VCAP_SERVICES);
}
// Parsing credentials from VCAP_SERVICES for binding service
if (vcap_services[config.mqtt.serviceName]) {
console.log("Parsing " + config.mqtt.serviceName);
config.mqtt.broker = "mqtt://" + vcap_services[config.mqtt.serviceName][0].credentials.protocols.mqtt.host;
config.mqtt.username = vcap_services[config.mqtt.serviceName][0].credentials.protocols.mqtt.username.trim();
config.mqtt.password = vcap_services[config.mqtt.serviceName][0].credentials.protocols.mqtt.password.trim();
config.mqtt.port = vcap_services[config.mqtt.serviceName][0].credentials.protocols.mqtt.port;
}
config.mqtt.options = {
broker: config.mqtt.broker,
reconnectPeriod: 1000,
port: config.mqtt.port,
username: config.mqtt.username,
password: config.mqtt.password
};
console.log(config.mqtt.options);
config.mqtt.topic = "#";
config.mqtt.retain = true; // MQTT Publish Retain
// Start MQTT
var client = mqtt.connect(config.mqtt.broker, config.mqtt.options);
client.on('connect', function () {
client.subscribe(config.mqtt.topic);
console.log("[MQTT]:", "Connected.");
});
client.on('message', function (topic, message) {
console.log("[" + topic + "]:" + message.toString());
});
client.on('error', function (err) {
console.log(err);
});
client.on('close', function () {
console.log("[MQTT]: close");
});
client.on('offline', function () {
console.log("[MQTT]: offline");
});