-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·91 lines (81 loc) · 2.59 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
#!/usr/bin/env node
const Dat = require('dat-node')
const rtlsdr = require('./lib/rtlsdr')
const storeEmitter = require('./lib/store').storeEmitter
const store = require('./lib/store').store
const station = require('./package').station
const dataFolder = 'station'
const flightFolder = 'flight_data/'
rtlsdr.start()
Dat(dataFolder, {indexing: false, latest:true, sparse: true}, function(err, dat) {
if(err) throw err
dat.joinNetwork()
console.log('Serving dat://'+dat.key.toString('hex'))
dat.archive.readFile('station.json', function(err, file) {
if(err) {
if(err.notFound) {
station.radio = rtlsdr.stat();
dat.archive.writeFile('station.json', JSON.stringify(station), function(err) {
if(err) throw err
})
}
else throw err
}
else {
station.radio = rtlsdr.stat();
if(file != JSON.stringify(station)) {
dat.archive.writeFile('station.json', JSON.stringify(station), function(err) {
if(err) throw err
})
}
}
})
dat.archive.readdir('/' + flightFolder, function(err, files) {
if(err) {
if(err.notFound) {
dat.archive.mkdir(flightFolder, function(err) { if(err) throw err })
}
else throw err
}
else {
console.log('Cleaning up Legacy Files')
files.forEach(function(legacyFile) {
dat.archive.unlink(flightFolder + legacyFile, function(err) {
if(err) throw err
})
})
console.log('%d files removed', files.length)
}
})
setInterval(function() {
store.getAircrafts().filter(function(aircraft) {
return aircraft.lat
})
.forEach(function(aircraft) {
dat.archive.readFile(flightFolder + aircraft.icao + '.json', 'utf8', (err, data) => {
if(err) {
if(err.notFound === true) {
dat.archive.writeFile(flightFolder + aircraft.icao + '.json', JSON.stringify(aircraft), function(err) {
if(err) throw err
})
}
else throw err
}
else {
var prevRecord = JSON.parse(data)
if(prevRecord.lat != aircraft.lat && prevRecord.lng != aircraft.lng) {
dat.archive.writeFile(flightFolder + aircraft.icao + '.json', JSON.stringify(aircraft), function(err) {
if(err) throw err
})
}
}
})
})
}, 3000)
storeEmitter.on('aircraftTimeout', function(icao) {
dat.archive.unlink(flightFolder + icao + '.json', function(err) {
// Expect some icao's to not be found as we arn't logging them all.
if(err && err.status != 404) throw err
})
})
})