-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (61 loc) · 2.11 KB
/
app.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
'use strict';
var mosca = require('mosca'),
Auth0Mosca = require('auth0mosca'),
TimeSeriesStore = require('ts-store'),
mongoose = require('mongoose'),
config = require('./config.json'),
uriUtil = require('mongodb-uri');
var options = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
}
};
var mongodbTSUri = config.tsstore.dbConnection + config.tsstore.db + "?authMechanism=SCRAM-SHA-1",
mongooseTSUri = uriUtil.formatMongoose(mongodbTSUri),
mongodbMqttUri = config.mqtt.dbConnection + config.mqtt.db + "?authMechanism=SCRAM-SHA-1",
TSconnection = mongoose.createConnection(mongooseTSUri, options);
var settings = {
port: config.mqtt.port,
logger: {
level: config.mqtt.loglevel
},
persistence: {
url: mongodbMqttUri,
factory: mosca.persistence.Mongo
},
http: {
port: config.mqtt.httpPort,
static: __dirname + "/static",
bundle: true,
stats: true
},
onlyHttp: false
},
server = new mosca.Server(settings),
// Wire up time series database
timeSeries = new TimeSeriesStore(TSconnection, {verbose: config.tsstore.verbose}),
auth0 = new Auth0Mosca(config.domains);
if (config.enableAuth) {
//Wire up authentication & authorization to mosca
server.authenticate = auth0.authenticate();
server.authorizePublish = auth0.authorizePublish();
server.authorizeSubscribe = auth0.authorizeSubscribe();
}
//Wire up persistence to time series database
server.published = timeSeries.publish();
server.on('clientConnected', function (client) {
console.log('client connected', client.id + ' ' + client.deviceProfile.name + ' at: ' + client.deviceProfile.domain);
});
server.on('published', function (packet, client) {
if (client !== undefined) {
console.log('Event Published: ' + packet.payload + ' Client :' + client.id);
}
});
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}
server.on('ready', setup);