-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (114 loc) · 3.53 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
var async = require('async');
function loadProtoBuf(cb) {
var request = require('request');
request.get('https://raw.githubusercontent.com/tabman83/phm-messages/master/sensor-message.proto', function (err, response, body) {
if(err) {
cb(err);
return;
}
if(response.statusCode !== 200) {
cb(new Error('Error while loading protobuf definition file (' + response.statusCode + ')'));
return;
}
var ProtoBuf = require('protobufjs');
var builder = ProtoBuf.loadProto(body);
if(!builder) {
cb(new Error('Error while decoding protobuf definition file'));
return;
}
var SensorMessage = builder.build('SensorMessage');
cb(null, SensorMessage);
});
}
function initializeBroker(SensorMessage, cb) {
var fs = require("fs");
var mosca = require('mosca');
var Authorizer = require("mosca/lib/authorizer");
var settings = {
port: process.env.MQTT_PORT || 1883,
http: {
port: 3000,
bundle: true
},
credentialsFile: './credentials.json'
};
function loadAuthorizer(program, cb) {
if (program.credentialsFile) {
fs.readFile(program.credentialsFile, function(err, data) {
if (err) {
cb(err);
return;
}
var authorizer = new Authorizer();
try {
authorizer.users = JSON.parse(data);
cb(null, authorizer);
} catch(err) {
cb(err);
}
});
} else {
cb(null, null);
}
}
//here we start mosca
var server = new mosca.Server(settings);
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
// setup authorizer
loadAuthorizer(settings, function(err, authorizer) {
if (err) {
console.error('Error loading authorizer.');
}
if (authorizer) {
console.log('Authorizer is configured.');
server.authenticate = authorizer.authenticate;
server.authorizeSubscribe = authorizer.authorizeSubscribe;
server.authorizePublish = authorizer.authorizePublish;
}
});
console.log('Mosca broker is up and running');
}
// fired whena client is connected
server.on('clientConnected', function(client) {
console.log('Client \'' + client.id + '\' just connected.');
});
// fired when a message is received
server.on('published', function(packet, client) {
var clientId = client ? client.id : 'unknown';
console.log('Client \'' + clientId + '\' published to the topic: ', packet.topic);
if(packet.topic === 'phm/sensors') {
var sensorMessage = SensorMessage.decode(packet.payload);
console.log(sensorMessage);
} else {
var payload = packet.payload.toString();
try {
payload = JSON.parse(packet.payload.toString());
}
catch(e) {}
console.log(payload);
}
});
// fired when a client subscribes to a topic
server.on('subscribed', function(topic, client) {
console.log('Client \'' + client.id + '\' subscribed to topic: ', topic);
});
// fired when a client subscribes to a topic
server.on('unsubscribed', function(topic, client) {
console.log('Client \'' + client.id + '\' unsubscribed from topic: ', topic);
});
// fired when a client is disconnecting
server.on('clientDisconnecting', function(client) {
console.log('Client \'' + client.id + '\' is about to disconnect.')
});
// fired when a client is disconnected
server.on('clientDisconnected', function(client) {
console.log('Client \'' + client.id + '\' disconnected.');
});
}
async.waterfall([loadProtoBuf, initializeBroker], function(err) {
if(err) {
console.log(err);
}
});