Skip to content

Commit 186b596

Browse files
committed
Lib-ify the code
1 parent 6b40c59 commit 186b596

File tree

4 files changed

+89
-98
lines changed

4 files changed

+89
-98
lines changed

example-file.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var adsb = require('./lib/adsb.js');
2+
var fs = require('fs');
3+
4+
var filename = process.argv[2];
5+
if (!filename){
6+
console.error("You must specify a filename.");
7+
}
8+
else{
9+
// Open the file and start parsing it
10+
fs.readFile(filename, function(err, data){
11+
if (err) throw err;
12+
13+
// Split lines and process them one at a time
14+
var packets = data.toString().trim().split("\n");
15+
for (var i in packets){
16+
adsb.decodePacket(packets[i]);
17+
}
18+
19+
// All done, output stats
20+
var stats = adsb.getStats();
21+
console.log('------------');
22+
console.log('Total packets: '+stats.packets);
23+
console.log('Invalid packets: '+stats.invalid_packets);
24+
console.log('CRC Errors: '+stats.crc_errors);
25+
});
26+
}

example-net.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var adsb = require('./lib/adsb.js');
2+
var net = require('net');
3+
4+
// Config
5+
var host = '192.168.1.144';
6+
var port = 30002;
7+
8+
// Connect to our datasource and start processing incoming data
9+
var client = net.connect({host: host, port: port}, function(){
10+
console.log('client connected');
11+
});
12+
13+
client.on('data', function(data){
14+
// Sometimes we get multiple packets here -- need to split them
15+
var packets = data.toString().trim().split("\n");
16+
for (var i in packets){
17+
adsb.decodePacket(packets[i]);
18+
}
19+
});
20+
21+
client.on('end', function(){
22+
console.log('client disconnected');
23+
24+
// All done, output stats
25+
var stats = adsb.getStats();
26+
console.log('------------');
27+
console.log('Total packets: '+stats.packets);
28+
console.log('Invalid packets: '+stats.invalid_packets);
29+
console.log('CRC Errors: '+stats.crc_errors);
30+
});
31+
32+
client.on('error', function(){
33+
console.log('connection error');
34+
});

adsb.js lib/adsb.js

+27-96
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
11
// Portions from: https://github.com/antirez/dump1090/blob/master/dump1090.c
22

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;
294

305
// Constants
316
var MODES_LONG_MSG_BITS = 112;
@@ -39,57 +14,13 @@ var stats = {
3914
crc_errors: 0
4015
};
4116

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+
};
8920

9021
// Takes a string and decodes it, testing validity
9122
// Returns a hash describing the packet
92-
function decodePacket(data){
23+
exports.decodePacket = function(data){
9324
stats.packets++;
9425
var len = data.length;
9526
if (len > 16) return; // Temporary
@@ -173,7 +104,7 @@ function decodePacket(data){
173104
// Message type is first 5 bits, always present
174105
// http://www.radartutorial.eu/13.ssr/sr24.en.html
175106
msg.type = bytes[0] >> 3;
176-
console.log('Type '+msg.type+': '+messageTypeToString(msg.type));
107+
console.log('Type '+msg.type+': '+this.messageTypeToString(msg.type));
177108

178109
switch (msg.type){
179110
case 16:
@@ -194,7 +125,7 @@ function decodePacket(data){
194125
msg.crc = (bytes[num_bytes - 3] << 16) | (bytes[num_bytes - 2] << 8) | bytes[num_bytes - 1];
195126

196127
// Calculate our own and compare it
197-
var crc2 = modesChecksum(bytes, msg.bits);
128+
var crc2 = this.modesChecksum(bytes, msg.bits);
198129
msg.crc_ok = (msg.crc == crc2);
199130
if (!msg.crc_ok){
200131
stats.crc_errors++;
@@ -205,7 +136,7 @@ function decodePacket(data){
205136

206137
// Responder capabilities, always present
207138
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));
209140

210141
// ICAO address, always present
211142
// http://www.radartutorial.eu/13.ssr/sr82.en.html ???
@@ -216,18 +147,18 @@ function decodePacket(data){
216147
// DF 17 type (assuming this is a DF17, otherwise not used)
217148
msg.metype = bytes[4] >> 3; // First 5 bits of byte 5
218149
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));
220151

221152
// Fields for DF4, 5, 20, 21
222153
msg.fs = bytes[0] & 7;
223154
msg.dr = bytes[1] >> 3 & 31;
224155
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));
226157

227158
// Decode 13 bit altitude for DF0, DF4, DF16, DF20
228159
// This is mostly in "short" messages, except DF20, which is "long" or "extended"
229160
if (msg.type === 0 || msg.type == 4 || msg.type == 16 || msg.type == 20){
230-
var ac13 = decodeAC13Field(bytes);
161+
var ac13 = this.decodeAC13Field(bytes);
231162
if (ac13){
232163
msg.altitude = ac13.altitude;
233164
msg.unit = ac13.unit;
@@ -258,7 +189,7 @@ function decodePacket(data){
258189
// Airborne Position (Baro Altitude)
259190
msg.fflag = bytes[6] & (1<<2);
260191
msg.tflag = bytes[6] & (1<<3);
261-
var ac12 = decodeAC12Field(bytes);
192+
var ac12 = this.decodeAC12Field(bytes);
262193
if (ac12){
263194
msg.altitude = ac12.altitude;
264195
msg.unit = ac12.unit;
@@ -312,7 +243,7 @@ function decodePacket(data){
312243
// Return our message hash
313244
console.log('');
314245
return msg;
315-
}
246+
};
316247

317248
/* Parity table for MODE S Messages.
318249
* The table contains 112 elements, every element corresponds to a bit set
@@ -351,7 +282,7 @@ var modes_checksum_table = [
351282
];
352283

353284
// http://www.radartutorial.eu/13.ssr/sr26.en.html
354-
function modesChecksum(bytes, num_bits){
285+
exports.modesChecksum = function(bytes, num_bits){
355286
var crc = 0;
356287
var offset = (num_bits == MODES_LONG_MSG_BITS) ? 0 : (MODES_LONG_MSG_BITS - MODES_SHORT_MSG_BITS);
357288

@@ -367,10 +298,10 @@ function modesChecksum(bytes, num_bits){
367298
}
368299

369300
return crc;
370-
}
301+
};
371302

372303
// Convert downlink format (DF) # to string
373-
function messageTypeToString(type){
304+
exports.messageTypeToString = function(type){
374305
switch (type){
375306
case 0:
376307
return 'Short Air-Air Surveillance';
@@ -392,10 +323,10 @@ function messageTypeToString(type){
392323
default:
393324
return "Unknown type: "+type;
394325
}
395-
}
326+
};
396327

397328
// Convert responder capabilities
398-
function responderCapabilitiesToString(ca){
329+
exports.responderCapabilitiesToString = function(ca){
399330
switch (ca){
400331
case 0:
401332
return "Level 1 (Surveillance Only)";
@@ -424,10 +355,10 @@ function responderCapabilitiesToString(ca){
424355
default:
425356
return "Unknown CA: "+ca;
426357
}
427-
}
358+
};
428359

429360
// Convert extended squitter type and subtype to string
430-
function meTypeToString(metype, mesub){
361+
exports.meTypeToString = function(metype, mesub){
431362
var mename = "Unknown";
432363

433364
if (metype >= 1 && metype <= 4){
@@ -465,10 +396,10 @@ function meTypeToString(metype, mesub){
465396
}
466397

467398
return mename;
468-
}
399+
};
469400

470401
// Convert flight status
471-
function flightStatusToString(fs){
402+
exports.flightStatusToString = function(fs){
472403
switch (fs){
473404
case 0:
474405
return "Normal, Airborne";
@@ -491,12 +422,12 @@ function flightStatusToString(fs){
491422
default:
492423
return "Unknown flight status: "+fs;
493424
}
494-
}
425+
};
495426

496427
// Decode the 13 bit AC altitude field (in DF 20 and others).
497428
// Returns the altitude and unit as a hash.
498429
// Unit is either MODES_UNIT_METERS or MDOES_UNIT_FEETS.
499-
function decodeAC13Field(bytes) {
430+
exports.decodeAC13Field = function(bytes) {
500431
var m_bit = bytes[3] & (1<<6);
501432
var q_bit = bytes[3] & (1<<4);
502433

@@ -520,11 +451,11 @@ function decodeAC13Field(bytes) {
520451
// TODO: Implement altitude when meter unit is selected.
521452
return 0;
522453
}
523-
}
454+
};
524455

525456
// Decode the 12 bit AC altitude field (in DF 17 and others).
526457
// Returns the altitude and the unit as a hash
527-
function decodeAC12Field(bytes){
458+
exports.decodeAC12Field = function(bytes){
528459
var q_bit = bytes[5] & 1;
529460

530461
if (q_bit) {
@@ -541,4 +472,4 @@ function decodeAC12Field(bytes){
541472
// TODO
542473
return 0;
543474
}
544-
}
475+
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "node-adsb",
33
"version": "0.1.0",
44
"description": "ADS-B decoder",
5-
"main": "adsb.js",
5+
"main": "./lib/adsb.js",
66
"scripts": {
77
"test": "mocha"
88
},
@@ -23,6 +23,6 @@
2323
"sdr"
2424
],
2525
"devDependencies": {
26-
"mocha": "~1.8.2"
26+
"mocha": "*"
2727
}
2828
}

0 commit comments

Comments
 (0)