-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbhs.js
370 lines (283 loc) · 10.9 KB
/
bhs.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var restify = require('restify');
var clients = require('restify-clients')
var mongojs = require("mongojs");
var http = require('http');
var url = require('url');
var fs = require('fs');
var sys = require('sys');
var amqp = require('amqp');
//DATABASE
var connection_string = '127.0.0.1:27017/aeriaa';
var db = mongojs(connection_string, ['bhs']);
var bhsDepartures = db.collection("bhsDepartures");
var bhsCarrouselsByProximity = db.collection("bhsCarrouselsByProximity");
var bhsBsms = db.collection("bhsBsms");
//COMUNICACION CON EL ESB para intercambiar con el AODB
var connection = amqp.createConnection({ host: '127.0.0.1' });
// Wait for connection to become established.
connection.on('ready', function () {
// Use the default 'amq.topic' exchange
connection.queue('bhs', function(q){
// Catch all messages
q.bind('#');
console.log('Connected to queue bhs');
});
});
//ESB's CONNECTION FOR AIRLINE BSM - Recommended Practice 1745
var connectionDCS = amqp.createConnection({ host: '127.0.0.1' });
// Wait for connection to become established.
connectionDCS.on('ready', function () {
// Use the bsm topic' exchange
connectionDCS.queue('bsm', function(q){
// Catch all messages
q.bind('#');
console.log('Connected to queue bsm');
// Receive messages
q.subscribe(function (message) {
// Print messages to stdout
console.log(message.bsm);
insertBSM(message);
});
});
});
//WEB SERVER
var server;
// the HTTP port
var port = 8081;
server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
if (path == '/') {
path = '/bhs/home.html'
}
console.log("serving " + path);
fs.readFile(__dirname + path, function(err, data){
if (err) {
res.writeHead(404);
res.end();
} else {
res.writeHead(200, {'Content-Type': contentType(path)});
res.write(data, 'utf8');
res.end();
}
});
});
function contentType(path) {
if (path.match('.js$')) {
return "text/javascript";
} else if (path.match('.css$')) {
return "text/css";
} else {
return "text/html";
}
}
server.listen(port);
console.log("HTTP server running at http://0.0.0.0:" + port );
//REST CLIENT FOR REQUEST OTHER SYSTEMS
var client = clients.createJSONClient({
url : 'http://127.0.0.1:8181',
version : '*'
});
//load the departures flights
requestDeparturesFlightsToAODB();
//REST API and SERVER
var ip_addr = '127.0.0.1';
var port = '8182';
var server = restify.createServer({
name : "bhs"
});
server.listen(port ,ip_addr, function(){
console.log('%s listening at %s ', server.name , server.url);
});
//server.use(restify.queryParser());
//server.use(restify.bodyParser());
//server.use(restify.CORS());
//AODB WEB SERVICES REST API
var PATH = '/bhs'
server.get({path : PATH , version : '0.0.1'} , findAllDeparturesFlights);
server.get({path : PATH +'/:bsms' , version : '0.0.1'} , findAllBsms);
server.get({path : PATH +'/:Flight' , version : '0.0.1'} , findDepartureFlight);
server.post({path : PATH +'/:Flight', version: '0.0.1'} ,postNewDepartureFlight);
//server.put({path : PATH +'/:Flight', version: '0.0.1'} , updateDepartureFlight);
server.del({path : PATH +'/:Flight' , version: '0.0.1'} ,deleteDepartureFlight);
//falta actualizar
//AODB FUNCTIONS
function findAllDeparturesFlights(req, res , next){
res.setHeader('Access-Control-Allow-Origin','*');
bhsDepartures.find().sort({postedOn : -1}, function(err , success){
//departures.find().limit(20).sort({postedOn : -1} , function(err , success){
// console.log('Response success '+success);
// console.log('Response error '+err);
if(success){
res.send(200 , success);
return next();
}else{
return next(err);
}
});
}
function findAllBsms(req, res , next){
res.setHeader('Access-Control-Allow-Origin','*');
bhsBsms.find().sort({postedOn : -1}, function(err , success){
//departures.find().limit(20).sort({postedOn : -1} , function(err , success){
//console.log('buscando bsms: '+success);
// console.log('Response error '+err);
if(success){
res.send(200 , success);
return next();
}else{
return next(err);
}
});
}
function findDepartureFlight(req, res , next){
res.setHeader('Access-Control-Allow-Origin','*');
bhsDepartures.findOne({_id:mongojs.ObjectId(req.params.Flight)} , function(err , success){
// console.log('Response success '+success);
// console.log('Response error '+err);
if(success){
res.send(200 , success);
return next();
}
return next(err);
})
}
function postNewDepartureFlight(req , res , next){
var flight = {};
/*job.title = req.params.title;
job.description = req.params.description;
job.location = req.params.location;
job.postedOn = new Date();
res.setHeader('Access-Control-Allow-Origin','*');
jobs.save(job , function(err , success){
console.log('Response success '+success);
console.log('Response error '+err);
if(success){
res.send(201 , job);
return next();
}else{
return next(err);
}
});*/
}
function deleteDepartureFlight(req , res , next){
res.setHeader('Access-Control-Allow-Origin','*');
bhsDepartures.remove({_id:mongojs.ObjectId(req.params.Flight)} , function(err , success){
// console.log('Response success '+success);
// console.log('Response error '+err);
if(success){
res.send(204);
return next();
} else{
return next(err);
}
})
}
function requestDeparturesFlightsToAODB(){
client.get('/departures', function (err, req, res, obj) {
if(err){
console.log("An error ocurred:", err);
}else {
// console.log('Server returned: %j', obj);
for(var i=0;i<obj.length;i++){
var newFlight = obj[i];
var gate = new String(obj[i].Gate);
var flight = new String(obj[i].Flight);
var std = new String(obj[i].STD);
// console.log('Puerta recibida ' + gate);
//Asignación, probando primero por proximidad
//console.log('Puerta recogida '+obj[i].Gate);
bhsDepartures.save(newFlight, function(err , success){
if(success){
// console.log('Response success '+success);
}else{
console.log('Response error '+err);
}
})
// console.log('Despues de insertar voy a actualizar el vuelo ' + newFlight.Flight);
assignCarrousel (gate, flight, std);
}
}
})
setTimeout(function(){
//console.log('********************** GENERANDO FICHERO ***********************'); //Espero 1 seg para asegurar que se ha guardado todo en BD
createAssignmentsFile();
}, 2000);
}
function assignCarrousel (gate, flight, std) {
var gate = ""+ gate + "";
var flight = "" + flight + "";
var stdHour = std.substr(0,2);
var stdMinutes = std.substr(3,2);
var startTime = new Date();
var endTime = new Date();
//carrousel's assignment starts 3 hours before and ends 15 minutes before
startTime.setHours(stdHour - 2, stdMinutes, 0,0);
endTime.setHours(startTime.getHours() + 3, startTime.getMinutes() - 15, 0, 0);
var carrousel;
var messageToESB;
bhsCarrouselsByProximity.findOne({Gate: gate}, function(err , results){
if(results){
carrousel = "" + results.Carrousel + "";
bhsDepartures.update({Flight:flight},{$set:{ BELT: carrousel, START: startTime, END: endTime}}, function(error , success){
if(success){
messageESB = ''+ flight +'';
//Publish the update for the ESB (mainly to AODB)
connection.publish('bhs', {"flight": messageToESB, "BELT": carrousel});
}else{
console.log('Find Carrousel Response error '+ err);
}
});
}
});
}
//temporalmente me creo el fichero aquí
//consulto bhsDepartures cojo los campos ordenados por Carrousel y por hora ETD y el id debe ser secuencial/incremental
function createAssignmentsFile(){
var assignedCarrouselsFile = "bhs/assignedCarrousels.json";
var belt;
var stream = fs.createWriteStream(assignedCarrouselsFile);
bhsDepartures.find().sort({BELT:1, ETD:1}, function(err, results){
var actualCarrousel;
var previousCarrousel;//para ver si hemos cambiado de carrusel
var id = 1; //id de la tarea en el gantt
var parent = 1;
var carrousel = 1;
var carrouselCounter = 0;
var date = new Date();
//{id:1, text:"Carrousel #1",start_date:"01-04-2013 06:00:00", duration:24, progress: 0.6, open: true},
if (results){
stream.write('{"data":[\n' + '{"id":1, "text":"Carrousel #' + carrousel + '", "destination": "----", "start_date":"03-04-2013 00:00:00", "duration":24, "open": true},\n');
previousCarrousel = results[0].BELT;
date = results[0].START;
for(var i=0;i<(results.length - 1);i++){
id = id + 1;
date = results[i].START;
actualCarrousel = results[i].BELT;
if (actualCarrousel == previousCarrousel){
stream.write('{"id":'+ id +', "text":"' + results[i].Flight + '", "destination": "'+ results[i].Destination + '", "start_date":"03-04-2013 ' + date.getHours() + ':' + date.getMinutes() + ':00", "duration":2.5, "progress": 0.0, "open": true, "parent":' + parent + '},\n');
}else{
carrousel = carrousel + 1;
previousCarrousel = actualCarrousel;
parent = i + 2 + carrouselCounter;
stream.write('{"id":' + id + ', "text":"Carrousel #' + carrousel + '", "destination": "----" , "start_date":"03-04-2013 00:00:00", "duration":24, "open": true},\n');
stream.write('{"id":'+ (id + 1) +', "text":"' + results[i].Flight + '", "destination": "' + results[i].Destination + '", "start_date":"03-04-2013 ' + date.getHours() + ':' + date.getMinutes() + ':00", "duration":2.5, "progress": 0.0, "open": true, "parent":' + parent + '},\n');
id = id + 1;
carrouselCounter = carrouselCounter + 1;
}
}
//escribo el último sin coma para que el JSON sea válido
stream.write('{"id":'+ (id + 1) +', "text":"' + results[i].Flight + '", "destination": "' + results[i].Destination + '", "start_date":"03-04-2013 ' + date.getHours() + ':' + date.getMinutes() + ':00", "duration":2.5, "progress": 0.0, "open": true, "parent":' + parent + '}\n');
stream.write(']}');
stream.end();
}
});
}
function insertBSM(message){
bhsBsms.save(message, function(err , success){
if(success){
console.log('Response success '+success);
}else{
console.log('Response error '+err);
}
})
}