-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
112 lines (95 loc) · 3.23 KB
/
app.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
var MongoClient = require('mongodb').MongoClient;
var http = require('http');
var $ = require('cheerio');
var process = require('process');
var later = require('later');
var result = {
"_id": "",
"type": "FeatureCollection",
"features": []
};
var database = "mongodb://localhost:27017/planes";
var collection = "planes";
MongoClient.connect(database, function(err, db) {
if(err) throw err;
console.log('connected to db');
GetFlightData(db);
setInterval(function () {
GetFlightData(db);
}, 15000);
});
function updateNameById(obj, id, value) {
try {
if (obj == null)
return;
var done = false;
obj.some(function (o) {
if (o.properties != null && o.properties["id"] === id) {
o = value;
done = true;
// console.log('updating feature');
return true;
} else {
return false;
}
});
if (!done) {
// console.log('adding feature');
obj.push(value);
}
} catch (e) {
console.log('error');
}
}
function CheckFeature(f,db) {
updateNameById(result.features, f["properties"]["id"], f);
db.collection(collection).insert(f, function(err, inserted) { });
}
function GetFlightData(db) {
var options = {
host: 'lhr.data.fr24.com',
path: '/zones/fcgi/feed.js?bounds=55.67283539294783,51.552131597019454,-1.5024902343745907,17.149658203125&faa=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&vehicles=1&estimated=1&maxage=900&gliders=1&stats=1&'
};
var r = "";
http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (d) {
r += d;
});
res.on('end', function () {
result.features.forEach(function (f) {
f.properties["Active"] = "false";
});
var b = JSON.parse(r);
for (var screen in b) {
var s = b[screen];
if (s[1] != null && s[2] != null && s[0] != null) {
var sit = {
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [s[2], s[1]] },
"properties": {
"id": s[0],
"FeatureTypeId": "plane",
"Altitude": s[4],
"Track": s[3],
"Speed": s[5],
"Squawk": s[6],
"PlaneType": s[8],
"Active": "true",
"Time": new Date().getTime()
}
};
CheckFeature(sit,db);
}
}
var active = result;
active.features = active.features.filter(function (f) {
return f.properties["Active"] == "true";
});
console.log(result.features.length);
// ID to avoid having duplicate _id keys (still need to know why this happens)
result._id = "Flightradar_" + new Date().getTime();
//insertIntoDB(result);
});
}).end();
}