-
Notifications
You must be signed in to change notification settings - Fork 16
/
hapi.js
59 lines (55 loc) · 1.42 KB
/
hapi.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
'use strict';
const Hapi = require('hapi');
const Joi = require('joi');
const server = new Hapi.Server();
server.connection({ port: process.env.PORT || 4004 });
var getDownlinkData = function(incoming){
/*
*
* Insert here your own code, to send back the relevant 8-byte frame
*
*/
//In this example, we're just sending back a 'random' string
return require('child_process').execSync('head /dev/urandom | LC_CTYPE=C tr -dc a-f0-9 | head -c 16', {encoding:'utf-8'});
};
var downlinkHandler = (request, reply) => {
if (request.path.match(/empty/)){
/*
* Return Empty response
* No message will be delivered to the deviceId
**/
return reply().code(204);
}
/*
* Reply with the proper JSON format.
* The _downlinkData_ will be sent to the device
**/
var response = {};
response[request.payload.device] = {"downlinkData":getDownlinkData(request.payload)};
reply(response);
};
var downlinkConfig = {
handler: downlinkHandler,
validate: {
payload: {
device: Joi.string().hex().required(),
data: Joi.string().hex().max(24)
}
}
};
server.route({
method: 'POST',
path: '/sigfox-downlink-data',
config: downlinkConfig
});
server.route({
method: 'POST',
path: '/sigfox-downlink-empty',
config: downlinkConfig
});
server.start((err) => {
if (err) {
throw err;
}
console.log('info', 'Server running at: ' + server.info.uri);
});