-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
157 lines (142 loc) · 4.14 KB
/
server.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
/*
broker © 2015
*/
var server_port = 8080
, http = require('http')
, mosca = require('mosca')
, express = require('express')
, app = express()
, domain = require('domain')
, broker = new mosca.Server({
port: 1883
})
, Device = require('./model/device').Device;
var serverDomain = domain.create();
app.use(require('body-parser').json());
app.get('/', function(req, res) {
res.json({v: 'MQTT:HTTP v0.0.1'});
});
app.post('/updates', function(req, res) {
var document = req.body;
Device.findByDeviceID(document.id, function(err, doc) {
if (doc) {
if (document.token !== doc.secret) return res.send(403);
for (var key in document.options) {
doc[key] = document.options[key];
}
doc.save();
}
});
res.json({status: 'frequency update success 200'})
});
var authenticate = function (client, deviceID, password, callback) {
console.log('authenticate', deviceID)
Device.findByDeviceID(deviceID, function(err, doc) {
if (err || !doc) {
callback(null, false);
} else {
doc.client = client;
doc.lastConnected = new Date;
doc.save();
client.deviceID = deviceID;
callback(null, true);
};
});
};
var authorizePublish = function (client, topic, payload, callback) {
console.log('authorizePublish', topic);
Device.topicAuthorized(client.deviceID, topic, function(err, truthy) {
if (err) truthy=false;
callback(null, truthy);
});
};
var authorizeSubscribe = function (client, topic, callback) {
console.log('authorizeSubscribe', topic);
Device.topicAuthorized(client.deviceID, topic, function(err, truthy) {
if (err) truthy=false;
callback(null, truthy);
});
};
function setup() {
broker.authenticate = authenticate;
broker.authorizePublish = authorizePublish;
broker.authorizeSubscribe = authorizeSubscribe;
console.log('Mosca broker is up and running.');
};
broker.on("error", function (err) {
console.log(new Date);
console.error(err);
});
broker.on('clientConnected', function (client) {
console.log({
msg: 'client connected',
date: new Date
});
});
broker.on('published', function (packet, client) {
if (!(packet.topic.match(/\$SYS/))) {
console.log('published', packet.topic);
Device.findByDeviceID(client.deviceID, function(err, doc) {
if (doc) {
doc.pubQueue({
date: new Date,
body: packet.payload.toString('utf-8'),
topic: packet.topic
});
};
});
};
});
broker.on('subscribed', function (topic, client) {
console.log({
msg: 'client subscribed',
topic: topic,
date: new Date
});
Device.findByDeviceID(client.deviceID, function(err, doc) {
if (doc) {
doc.lastSubscribed = new Date;
doc.save();
};
});
});
broker.on('clientDisconnecting', function (client) {
console.log({
msg: 'client disconnecting',
date: new Date
});
Device.findByDeviceID(client.deviceID, function(err, doc) {
if (doc) {
doc.lastDisconnecting = new Date;
doc.save();
};
});
});
broker.on('clientDisconnected', function (client) {
console.log({
msg: 'client disconnected',
date: new Date
});
Device.findByDeviceID(client.deviceID, function(err, doc) {
if (doc) {
doc.lastDisconnected = new Date;
doc.save();
};
});
});
serverDomain.run(function () {
http.createServer(function (req, res) {
var reqd = domain.create();
reqd.add(req);
reqd.add(res);
reqd.on('error', function (error) {
console.error('Error', error.code, error.message, req.url, new Date);
reqd.dispose();
});
app(req, res);
}).listen(server_port, function() {
console.log("MQTT:HTTP listening")
broker.on('ready', setup);
});
});