/
serve_mqtt.js
176 lines (157 loc) · 5.55 KB
/
serve_mqtt.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* This is script to serve a gauge display on received MQTT messages
* topic: /sensor/<sensor_id>/gauge
* payload: [<timestamp>, <value>, <unit>]
* Markus Gebhard, Karlsruhe, 2015-2016
*
* Released under the MIT license. See LICENSE file for details.
*
* Static http server part taken from Ryan Florence (rpflorence on github)
* https://gist.github.com/rpflorence/701407
* ************************************************************
* Note: see package.json for dependencies
*/
// use http for page serving, fs for getting the *.html files
var httpport = 1080;
// use mqtt for client, socket.io for push,
var mqtt = require("mqtt");
// specify your MQTT broker's data here
// var mqttbroker = "192.168.0.50", mqttport = "1883";
// define the http server
var http = require("http").createServer(httphandler).listen(httpport);
var fs = require("fs");
var url = require("url");
var path = require("path");
var io = require("socket.io")(http);
// multicast DNS service discovery
var mdns = require("mdns");
// resolution requence added due to mdns issue - see https://github.com/agnat/node_mdns/issues/130
var sequence = [ mdns.rst.DNSServiceResolve(), "DNSServiceGetAddrInfo" in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({
families: [ 4 ]
}), mdns.rst.makeAddressesUnique() ];
// detect mqtt publishers and create corresponding servers
var mdnsbrowser = mdns.createBrowser(mdns.tcp("mqtt"), {
resolverSequence: sequence
});
mdnsbrowser.on("serviceUp", function(service) {
console.log("Detected MQTT service on: " + service.addresses[0] + ":" + service.port);
mqttconnect(service.addresses[0], service.port);
});
mdnsbrowser.on("serviceDown", function(service) {
console.log("MQTT service down: ", service);
});
mdnsbrowser.on("error", function(exception) {
console.log("An error occured: ", exception);
});
// for manual broker definition, comment out the next line
// mqttconnect(mqttbroker, mqttport);
// start the mdns browser
mdnsbrowser.start();
// define what to do on connecting the mqtt broker
function mqttconnect(mqttbroker, mqttport) {
var mqttclient;
// create the MQTT connection
mqttclient = mqtt.connect({
port: mqttport,
host: mqttbroker
});
// handle socketio requests
io.on("connection", function(socket) {
// handle subscription request(s)
socket.on("subscribe", function(data) {
mqttclient.subscribe(data.topic);
});
});
// check MQTT connection
mqttclient.on("connect", function() {
console.log("Connected: ", mqttbroker, ":", mqttport);
});
// log error from MQTT client
mqttclient.on("error", function() {
console.log("The MQTT client raised an error ...");
});
// handle mqtt messages
mqttclient.on("message", function(topic, message) {
// split topic into array to check for sub-topics
var topicArray = topic.split("/");
// now deal with the payload, which is sent as BUFFER object
var payload = message.toString();
// don't handle messages with weird tokens, e.g. compression
try {
// deal with the JSON decoded payload from here
payload = JSON.parse(payload);
} catch (error) {
return;
}
// compute different sub-topics here
switch (topicArray[1]) {
case "sensor":
handle_sensor(topicArray, payload);
break;
default:
break;
}
// emit received message to socket.io listener
// a stringified JSON object is passed on
io.sockets.emit("mqtt", {
topic: topic,
payload: JSON.stringify(payload)
});
});
// handle the sensor readings
function handle_sensor(topicArray, payload) {
switch (topicArray[3]) {
case "gauge":
// FLM gauges consist of timestamp, value, and unit
// gauge length 2 is sent from Arduino sensors (in my case):
// [<value:float>,"<unit>:string"]
switch (payload.length) {
case 2:
// enhance payload w/o timestamp by current timestamp
var now = parseInt(new Date().getTime() / 1e3);
payload.unshift(now);
break;
}
break;
default:
break;
}
}
}
// Serve the index.html page
function httphandler(req, res) {
var uri = url.parse(req.url).pathname, filename = path.join(process.cwd(), uri);
var contentTypesByExtension = {
".html": "text/html",
".css": "text/css",
".js": "text/javascript"
};
// serve requested files
fs.exists(filename, function(exists) {
if (!exists) {
res.writeHead(404, {
"Content-Type": "text/plain"
});
res.write("404 Not Found\n");
res.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += "/gauge.html";
fs.readFile(filename, "binary", function(err, file) {
if (err) {
res.writeHead(500, {
"Content-Type": "text/plain"
});
res.write(err + "\n");
res.end();
return;
}
var headers = {};
var contentType = contentTypesByExtension[path.extname(filename)];
if (contentType) headers["Content-Type"] = contentType;
res.writeHead(200, headers);
res.write(file, "binary");
res.end();
});
});
}