1
1
// Portions from: https://github.com/antirez/dump1090/blob/master/dump1090.c
2
2
3
- var net = require ( 'net' ) ;
4
- var fs = require ( 'fs' ) ;
5
-
6
- // Config
7
- var host = '192.168.1.144' ;
8
- var port = 30002 ;
9
- var filename = null ;
10
-
11
- // Procss command-line args
12
- process . argv . forEach ( function ( val , index , array ) {
13
- if ( index < 2 ) return ;
14
-
15
- switch ( val ) {
16
- case '-h' :
17
- host = process . argv [ index + 1 ] ;
18
- break ;
19
- case '-p' :
20
- port = process . argv [ index + 1 ] ;
21
- break ;
22
- case '-f' :
23
- filename = process . argv [ index + 1 ] ;
24
- break ;
25
- default :
26
- break ;
27
- }
28
- } ) ;
3
+ exports = module . exports ;
29
4
30
5
// Constants
31
6
var MODES_LONG_MSG_BITS = 112 ;
@@ -39,57 +14,13 @@ var stats = {
39
14
crc_errors : 0
40
15
} ;
41
16
42
- // What we do depends on whether we have a file defined or not
43
- if ( filename ) {
44
- // Open the file and start parsing it
45
- fs . readFile ( filename , function ( err , data ) {
46
- if ( err ) throw err ;
47
-
48
- // Split lines and process them one at a time
49
- var packets = data . toString ( ) . trim ( ) . split ( "\n" ) ;
50
- for ( var i in packets ) {
51
- decodePacket ( packets [ i ] ) ;
52
- }
53
-
54
- // All done, output stats
55
- console . log ( '------------' ) ;
56
- console . log ( 'Total packets: ' + stats . packets ) ;
57
- console . log ( 'Invalid packets: ' + stats . invalid_packets ) ;
58
- console . log ( 'CRC Errors: ' + stats . crc_errors ) ;
59
- } ) ;
60
- }
61
- else {
62
- // Connect to our datasource and start processing incoming data
63
- var client = net . connect ( { host : host , port : port } , function ( ) {
64
- console . log ( 'client connected' ) ;
65
- } ) ;
66
-
67
- client . on ( 'data' , function ( data ) {
68
- // Sometimes we get multiple packets here -- need to split them
69
- var packets = data . toString ( ) . trim ( ) . split ( "\n" ) ;
70
- for ( var i in packets ) {
71
- decodePacket ( packets [ i ] ) ;
72
- }
73
- } ) ;
74
-
75
- client . on ( 'end' , function ( ) {
76
- console . log ( 'client disconnected' ) ;
77
-
78
- // All done, output stats
79
- console . log ( '------------' ) ;
80
- console . log ( 'Total packets: ' + stats . packets ) ;
81
- console . log ( 'Invalid packets: ' + stats . invalid_packets ) ;
82
- console . log ( 'CRC Errors: ' + stats . crc_errors ) ;
83
- } ) ;
84
-
85
- client . on ( 'error' , function ( ) {
86
- console . log ( 'connection error' ) ;
87
- } ) ;
88
- }
17
+ exports . getStats = function ( ) {
18
+ return stats ;
19
+ } ;
89
20
90
21
// Takes a string and decodes it, testing validity
91
22
// Returns a hash describing the packet
92
- function decodePacket ( data ) {
23
+ exports . decodePacket = function ( data ) {
93
24
stats . packets ++ ;
94
25
var len = data . length ;
95
26
if ( len > 16 ) return ; // Temporary
@@ -173,7 +104,7 @@ function decodePacket(data){
173
104
// Message type is first 5 bits, always present
174
105
// http://www.radartutorial.eu/13.ssr/sr24.en.html
175
106
msg . type = bytes [ 0 ] >> 3 ;
176
- console . log ( 'Type ' + msg . type + ': ' + messageTypeToString ( msg . type ) ) ;
107
+ console . log ( 'Type ' + msg . type + ': ' + this . messageTypeToString ( msg . type ) ) ;
177
108
178
109
switch ( msg . type ) {
179
110
case 16 :
@@ -194,7 +125,7 @@ function decodePacket(data){
194
125
msg . crc = ( bytes [ num_bytes - 3 ] << 16 ) | ( bytes [ num_bytes - 2 ] << 8 ) | bytes [ num_bytes - 1 ] ;
195
126
196
127
// Calculate our own and compare it
197
- var crc2 = modesChecksum ( bytes , msg . bits ) ;
128
+ var crc2 = this . modesChecksum ( bytes , msg . bits ) ;
198
129
msg . crc_ok = ( msg . crc == crc2 ) ;
199
130
if ( ! msg . crc_ok ) {
200
131
stats . crc_errors ++ ;
@@ -205,7 +136,7 @@ function decodePacket(data){
205
136
206
137
// Responder capabilities, always present
207
138
msg . ca = bytes [ 0 ] & 7 ; // Last 3 bits of the first byte
208
- console . log ( 'CA ' + msg . ca + ': ' + responderCapabilitiesToString ( msg . ca ) ) ;
139
+ console . log ( 'CA ' + msg . ca + ': ' + this . responderCapabilitiesToString ( msg . ca ) ) ;
209
140
210
141
// ICAO address, always present
211
142
// http://www.radartutorial.eu/13.ssr/sr82.en.html ???
@@ -216,18 +147,18 @@ function decodePacket(data){
216
147
// DF 17 type (assuming this is a DF17, otherwise not used)
217
148
msg . metype = bytes [ 4 ] >> 3 ; // First 5 bits of byte 5
218
149
msg . mesub = bytes [ 4 ] & 7 ; // Last 3 bits of byte 5
219
- console . log ( 'Extended Squitter Type ' + msg . metype + ', Subtype: ' + msg . mesub + ': ' + meTypeToString ( msg . metype , msg . mesub ) ) ;
150
+ console . log ( 'Extended Squitter Type ' + msg . metype + ', Subtype: ' + msg . mesub + ': ' + this . meTypeToString ( msg . metype , msg . mesub ) ) ;
220
151
221
152
// Fields for DF4, 5, 20, 21
222
153
msg . fs = bytes [ 0 ] & 7 ;
223
154
msg . dr = bytes [ 1 ] >> 3 & 31 ;
224
155
msg . um = ( ( bytes [ 1 ] & 7 ) << 3 ) | bytes [ 2 ] >> 5 ;
225
- console . log ( 'Flight Status ' + msg . fs + ': ' + flightStatusToString ( msg . fs ) ) ;
156
+ console . log ( 'Flight Status ' + msg . fs + ': ' + this . flightStatusToString ( msg . fs ) ) ;
226
157
227
158
// Decode 13 bit altitude for DF0, DF4, DF16, DF20
228
159
// This is mostly in "short" messages, except DF20, which is "long" or "extended"
229
160
if ( msg . type === 0 || msg . type == 4 || msg . type == 16 || msg . type == 20 ) {
230
- var ac13 = decodeAC13Field ( bytes ) ;
161
+ var ac13 = this . decodeAC13Field ( bytes ) ;
231
162
if ( ac13 ) {
232
163
msg . altitude = ac13 . altitude ;
233
164
msg . unit = ac13 . unit ;
@@ -258,7 +189,7 @@ function decodePacket(data){
258
189
// Airborne Position (Baro Altitude)
259
190
msg . fflag = bytes [ 6 ] & ( 1 << 2 ) ;
260
191
msg . tflag = bytes [ 6 ] & ( 1 << 3 ) ;
261
- var ac12 = decodeAC12Field ( bytes ) ;
192
+ var ac12 = this . decodeAC12Field ( bytes ) ;
262
193
if ( ac12 ) {
263
194
msg . altitude = ac12 . altitude ;
264
195
msg . unit = ac12 . unit ;
@@ -312,7 +243,7 @@ function decodePacket(data){
312
243
// Return our message hash
313
244
console . log ( '' ) ;
314
245
return msg ;
315
- }
246
+ } ;
316
247
317
248
/* Parity table for MODE S Messages.
318
249
* The table contains 112 elements, every element corresponds to a bit set
@@ -351,7 +282,7 @@ var modes_checksum_table = [
351
282
] ;
352
283
353
284
// http://www.radartutorial.eu/13.ssr/sr26.en.html
354
- function modesChecksum ( bytes , num_bits ) {
285
+ exports . modesChecksum = function ( bytes , num_bits ) {
355
286
var crc = 0 ;
356
287
var offset = ( num_bits == MODES_LONG_MSG_BITS ) ? 0 : ( MODES_LONG_MSG_BITS - MODES_SHORT_MSG_BITS ) ;
357
288
@@ -367,10 +298,10 @@ function modesChecksum(bytes, num_bits){
367
298
}
368
299
369
300
return crc ;
370
- }
301
+ } ;
371
302
372
303
// Convert downlink format (DF) # to string
373
- function messageTypeToString ( type ) {
304
+ exports . messageTypeToString = function ( type ) {
374
305
switch ( type ) {
375
306
case 0 :
376
307
return 'Short Air-Air Surveillance' ;
@@ -392,10 +323,10 @@ function messageTypeToString(type){
392
323
default :
393
324
return "Unknown type: " + type ;
394
325
}
395
- }
326
+ } ;
396
327
397
328
// Convert responder capabilities
398
- function responderCapabilitiesToString ( ca ) {
329
+ exports . responderCapabilitiesToString = function ( ca ) {
399
330
switch ( ca ) {
400
331
case 0 :
401
332
return "Level 1 (Surveillance Only)" ;
@@ -424,10 +355,10 @@ function responderCapabilitiesToString(ca){
424
355
default :
425
356
return "Unknown CA: " + ca ;
426
357
}
427
- }
358
+ } ;
428
359
429
360
// Convert extended squitter type and subtype to string
430
- function meTypeToString ( metype , mesub ) {
361
+ exports . meTypeToString = function ( metype , mesub ) {
431
362
var mename = "Unknown" ;
432
363
433
364
if ( metype >= 1 && metype <= 4 ) {
@@ -465,10 +396,10 @@ function meTypeToString(metype, mesub){
465
396
}
466
397
467
398
return mename ;
468
- }
399
+ } ;
469
400
470
401
// Convert flight status
471
- function flightStatusToString ( fs ) {
402
+ exports . flightStatusToString = function ( fs ) {
472
403
switch ( fs ) {
473
404
case 0 :
474
405
return "Normal, Airborne" ;
@@ -491,12 +422,12 @@ function flightStatusToString(fs){
491
422
default :
492
423
return "Unknown flight status: " + fs ;
493
424
}
494
- }
425
+ } ;
495
426
496
427
// Decode the 13 bit AC altitude field (in DF 20 and others).
497
428
// Returns the altitude and unit as a hash.
498
429
// Unit is either MODES_UNIT_METERS or MDOES_UNIT_FEETS.
499
- function decodeAC13Field ( bytes ) {
430
+ exports . decodeAC13Field = function ( bytes ) {
500
431
var m_bit = bytes [ 3 ] & ( 1 << 6 ) ;
501
432
var q_bit = bytes [ 3 ] & ( 1 << 4 ) ;
502
433
@@ -520,11 +451,11 @@ function decodeAC13Field(bytes) {
520
451
// TODO: Implement altitude when meter unit is selected.
521
452
return 0 ;
522
453
}
523
- }
454
+ } ;
524
455
525
456
// Decode the 12 bit AC altitude field (in DF 17 and others).
526
457
// Returns the altitude and the unit as a hash
527
- function decodeAC12Field ( bytes ) {
458
+ exports . decodeAC12Field = function ( bytes ) {
528
459
var q_bit = bytes [ 5 ] & 1 ;
529
460
530
461
if ( q_bit ) {
@@ -541,4 +472,4 @@ function decodeAC12Field(bytes){
541
472
// TODO
542
473
return 0 ;
543
474
}
544
- }
475
+ } ;
0 commit comments