forked from vynci/MQTT-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (25 loc) · 719 Bytes
/
index.js
File metadata and controls
31 lines (25 loc) · 719 Bytes
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
var Hapi = require('hapi');
var mqtt = require('mqtt');
var server = new Hapi.Server();
var port = Number(process.env.PORT || 4444);
server.connection({ port: port, routes: { cors: true } });
var client = mqtt.connect('mqtt://test.mosquitto.org:1883');
var mqttPublish = function(topic, msg){
client.publish(topic, msg, function() {
console.log('msg sent: ' + msg);
});
}
server.route([
{
method: 'POST',
path: '/device/control',
handler: function (request, reply) {
var deviceInfo = 'dev' + request.payload.deviceNum + '-' + request.payload.command;
reply(deviceInfo);
mqttPublish('device/control', deviceInfo, {
'qos' : 2
});
}
}
]);
server.start();