forked from FIWARE/data-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoi-server.js
133 lines (108 loc) · 3.14 KB
/
poi-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
const PORT = 1050; // Port on which the proxy will be listening to
var URL = require('url');
var fs = require('fs');
var Orion = require('fiware-orion-client');
var OrionHelper = Orion.NgsiHelper;
var OportoOST = require('./oporto-ost-module.js');
var loggerStream = fs.createWriteStream('./log.txt', {
flags: 'a',
encoding: 'utf-8',
mode: '0666'
});
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var app = express();
app.use(morgan('dev',{
stream: loggerStream
}));
app.use(bodyParser.json());
var typesMap = {
'PointOfInterest': 'pois'
};
var categoriesMap = {
'OffStreetParking': '418',
'Restaurant': '347',
'Hotel': '436',
'Museum': '311',
'Beach': '113',
'TouristInformationCenter': '439'
};
app.get('/v2/entities', function (req, resp) {
var type = req.query.type;
var limit = req.query.limit;
var offset = req.query.offset;
// Given by OST
const OFFSET_INCR = 25;
if (!type) {
resp.sendStatus(404);
return;
}
console.log('Type Requested: ', type);
var category = categoriesMap[type];
if (!category && type !== 'PointOfInterest') {
resp.sendStatus(404);
return;
}
var geoRel = req.query.georel;
var locationOptions = null;
if (geoRel) {
var coords = req.query.coords;
if (coords && geoRel.indexOf('near') !== -1) {
var components = geoRel.split(';');
var distance = components[1];
var maxDistance = parseInt(distance.split('=')[1], 10);
locationOptions = {
geometry: 'Circle',
radius: maxDistance,
coords: coords
};
}
}
var q = req.query.q;
if (q) {
var tokens = q.split(';');
tokens.forEach(function(aToken) {
var queryFields = aToken.split(':');
if (queryFields[0] === 'category') {
category = queryFields[1];
}
});
}
OportoOST.getData('pois', category, limit, offset, locationOptions).then(function(result) {
var limited = result.data.slice(0, limit);
var linkHeader;
if (!offset) {
offset = 0;
}
var next = Number(offset) + OFFSET_INCR;
linkHeader = '</v2/entities?type=PointOfInterest' + '&' + 'offset' +
'=' + next + '>;rel="next"';
var prev = offset - OFFSET_INCR;
var offsetParams = '';
if (prev > 0) {
offsetParams = '&offset=' + prev;
}
linkHeader += ', </v2/entities?type=PointOfInterest' + offsetParams + '>;rel="prev"';
resp.setHeader('Link', linkHeader);
resp.json(limited);
}).catch(function(err) {
console.error('Error while get data: ', err);
resp.sendStatus(500);
});
});
app.get('/v2/entities/:entityId', function (req, resp) {
console.log(req.params.entityId);
OportoOST.getEntity(req.params.entityId).then(function(result) {
if (!result) {
resp.sendStatus(404);
return;
}
resp.json(result);
}).catch(function(err) {
console.error('Error while get entity: ', err);
resp.sendStatus(500);
});
});
console.log('GSMA proxy up and running on port: ', PORT);
app.listen(PORT);