-
Notifications
You must be signed in to change notification settings - Fork 0
/
bs_parse.rb
383 lines (372 loc) · 10.6 KB
/
bs_parse.rb
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#--------------------------------------------------------------------------------------------------
#bs_parse - basestation format parser and logger
#Version 0.1
#bs_parse is free software licensed under GNU GPLv3
#Copyright (C) 2017 - Alexey V. Voronin @ FoxyLab
#Email: [email protected]
#Website: https://acdc.foxylab.com
#
#CREDITS
#
#Rob Altenburg - icao_db: Sqlite3 database of icao mode_s codes, aircraft type, and tail numbers
#https://github.com/RobAltenburg/icao_db
#
#AeroTransport Data Bank - http://www.aerotransport.org/html/ICAO_hex_decode.html
#---------------------------------------------------------------------------------------------------
require 'socket'
require 'date'
require 'colorize' #gem install colorize
require 'csv'
require_relative 'country.rb'
exit_requested = false
Kernel.trap( "INT" ) { exit_requested = true }
hostname = 'localhost' #local computer
port = 30003 #basestation port (TCP BaseStation output listen port, --net-sbs-port xxxxx)
#Ruby represents each table row as an array
icao_tail = Hash.new("")
icao_type = Hash.new("")
CSV.foreach('icao.csv') do |row| #353043 records
icao_tail[row[0]] = row[1]
icao_type[row[0]] = row[2]
end
sock = TCPSocket.open(hostname, port)
#hashes create
trk_cs = Hash.new("") #callsign
trk_sq = Hash.new("") #squawk
trk_alt = Hash.new("") #altitude
trk_trk = Hash.new("") #track
trk_gs = Hash.new("") #ground speed
trk_lat = Hash.new("") #latitude
trk_lng = Hash.new("") #longitude
trk_start_date = Hash.new("") #start date
trk_start_time = Hash.new("") #start time
trk_stop_date = Hash.new("") #stop date
trk_stop_time = Hash.new("") #stop time
trk_tail = Hash.new("") #tail number
trk_type = Hash.new("") #type
trk_country = Hash.new("") #country
while (line = sock.gets.chomp) && (!exit_requested)
#read next line from the socket - Ruby uses LF = \n to detect newline
#gets returns a string and a '\n' character, while chomp removes this '\n'
#gets returns nil at end of file.
#when a socket is closed, it sends eof to the other side.
#therefore gets() returns nil
fields = line.split(",")
#AC data init
hexident = ""
aircraftid = ""
flightid = ""
cs = ""
alt = ""
gs = ""
trk = ""
lat = ""
lng = ""
vr = ""
sq = ""
alrt = ""
emer = ""
spi = ""
gnd = ""
start_date = ""
start_time = ""
stop_date = ""
stop_time = ""
tail = ""
type = ""
country = ""
#types
case fields[0].to_s
when "SEL"
#SELECTION CHANGE MESSAGE
#Generated when the user changes the selected aircraft in BaseStation
print "SEL"
print ":"
if (trk_cs.has_key?(fields[4].to_s))
hexident = fields[4].to_s
cs = fields[10].to_s
trk_cs[hexident] = cs
print hexident
end
print "\r\n"
when "ID"
#NEW ID MESSAGE
#Generated when an aircraft being tracked sets or changes its callsign
print "ID"
print ":"
if (trk_cs.has_key?(fields[4].to_s))
hexident = fields[4].to_s
cs = fields[10].to_s
trk_cs[hexident] = cs
print hexident
end
print "\r\n"
when "AIR"
#NEW AIRCRAFT MESSAGE
#Generated when the SBS picks up a signal for an aircraft that it isn't currently tracking
print "AIR"
print ":"
hexident = fields[4].to_s
start_date = fields[6].to_s
start_time = fields[7].to_s
#start date/time
trk_start_date[hexident] = start_date
trk_start_time[hexident] = start_time
#database search
#tail number, aircraft type
tail = ""
type = ""
if (icao_tail.has_key?(hexident))
tail = icao_tail[hexident]
type = icao_type[hexident]
end
trk_tail[hexident] = tail
trk_type[hexident] = type
#country
hexnum = hexident.to_i(base=16)
country = Country.hex2country(hexnum)
trk_country[hexident] = country
print hexident
print ":"
print country
print ":"
print tail
print ":"
print type
print "\r\n"
#key-value pairs init
trk_cs[hexident] = ""
trk_sq[hexident] = ""
trk_alt[hexident] = ""
trk_trk[hexident] = ""
trk_gs[hexident] = ""
trk_lat[hexident] = ""
trk_lng[hexident] = ""
trk_stop_date[hexident] = ""
trk_stop_time[hexident] = ""
when "STA"
#STATUS CHANGE MESSAGE
#Generated when an aircraft's status changes according to the time-out
#values in the Data Settings menu.
print "STA"
print ":"
if (trk_cs.has_key?(fields[4].to_s))
hexident = fields[4].to_s
print hexident
print ":"
print fields[10].to_s
# STA message uses the callsign field to record status flags based on user time-out values.
# Values are PL (Position Lost), SL (Signal Lost), RM (Remove), AD (Delete) and OK (used to reset time-outs if aircraft returns into cover)
if (fields[10].to_s)=="RM"
#stop date/time
stop_date = fields[6].to_s
stop_time = fields[7].to_s
trk_stop_date[hexident] = stop_date
trk_stop_time[hexident] = stop_time
#save last data to file
filename = Time.now.utc.strftime("%Y%m%d")
filename = 'bs_' + filename
filename = filename + '.txt'
print " >> "
print filename
File.open(filename,'ab') do |fo|
fo.write hexident #HEX Aircraft Mode S hexadecimal code, given in upper case (military and civilian)
fo.putc "\t"
fo.write trk_country[hexident] #country
fo.putc "\t"
fo.write trk_tail[hexident] #tail number
fo.putc "\t"
fo.write trk_type[hexident] #aircraft type
fo.putc "\t"
fo.write trk_cs[hexident] #CS eight digit flight ID
fo.putc "\t"
fo.write trk_sq[hexident] #SQ Assigned Mode A squawk code (transponder Mode 3/A) 0000...7777
#4-digit octal identification code for the aircraft,
#assigned by the air traffic controller. (military and civilian)
#7700: General Emergency
#7600: Lost Communications
#7500: Unlawful Interference (Hijack)
fo.putc "\t"
fo.write trk_gs[hexident] #GS Speed over ground (not indicated airspeed) in knots
fo.putc "\t"
fo.write trk_alt[hexident] #ALT Mode C altitude. Height relative to 1013.2mb (Flight Level) (transponder Mode C)
#4-digit octal code for aircraft's pressure altitude. (military and civilian)
fo.putc "\t"
fo.write trk_trk[hexident] #TRK Track of aircraft (not heading), in degrees
fo.putc "\t"
fo.write trk_lat[hexident] #LAT
fo.putc "\t"
fo.write trk_lng[hexident] #LNG
fo.putc "\t"
fo.write trk_start_date[hexident] #DMG Generation time of the message
fo.putc "\t"
fo.write trk_start_time[hexident] #TMG
fo.putc "\t"
fo.write trk_stop_date[hexident] #DMG
fo.putc "\t"
fo.write trk_stop_time[hexident] #TMG
fo.putc "\r"
fo.putc "\n"
#the file is closed automatically
end
#key-value pairs delete
trk_cs.delete(hexident)
trk_sq.delete(hexident)
trk_alt.delete(hexident)
trk_trk.delete(hexident)
trk_gs.delete(hexident)
trk_lat.delete(hexident)
trk_lng.delete(hexident)
trk_start_date.delete(hexident)
trk_start_time.delete(hexident)
trk_stop_date.delete(hexident)
trk_stop_time.delete(hexident)
trk_tail.delete(hexident)
trk_type.delete(hexident)
trk_country.delete(hexident)
hexident = ""
end
end
print "\r\n"
when "CLK"
#CLICK MESSAGE
#Generated when the user double-clicks (or presses return) on an aircraft
print "CLK"
print ":"
print "\r\n"
when "MSG"
#TRANSMISSION MESSAGE
#Generated by the aircraft. There are eight different MSG types
print "MSG"
print ":"
if (trk_cs.has_key?(fields[4].to_s))
hexident = fields[4].to_s
#MSG sub types
#BDS Y,Z -> Y - BDS1 code; Z - BDS2 code
#DF = Downlink Format, BDS = Binary Data Store
if (fields[1].to_s)=="1"
#ES Identification and Category
#AC category and index information
#DF17 BDS 0,8
cs = fields[10].to_s
trk_cs[hexident] = cs
print hexident
print ":"
print "ES ID"
print ":"
print cs
end
if (fields[1].to_s)=="2"
#ES Surface Position Message
#information about position on ground
#DF17 BDS 0,6
#Triggered by nose gear squat switch
lat = fields[14].to_s
lng = fields[15].to_s
trk_lat[hexident] = lat
trk_lng[hexident] = lng
alt = fields[11].to_s
trk_alt[hexident] = alt
gs = fields[12].to_s
trk_gs[hexident] = gs
trk = fields[13].to_s
trk_trk[hexident] = trk
print hexident
print ":"
print "ES POS SUR"
print ":"
print alt
print ":"
print gs
print ":"
print trk
print ":"
print lat
print ":"
print lng
end
if (fields[1].to_s)=="3"
#ES Airborne Position Message
#information about position in air
#DF17 BDS 0,5
lat = fields[14].to_s
lng = fields[15].to_s
trk_lat[hexident] = lat
trk_lng[hexident] = lng
alt = fields[11].to_s
trk_alt[hexident] = alt
print hexident
print ":"
print "ES POS AIR"
print ":"
print alt
print ":"
print lat
print ":"
print lng
end
if (fields[1].to_s)=="4"
#ES Airborne Velocity Message
#information about velocity in air
#DF17 BDS 0,9
gs = fields[12].to_s
trk_gs[hexident] = gs
trk = fields[13].to_s
trk_trk[hexident] = trk
print hexident
print ":"
print "ES VEL AIR"
print ":"
print gs
print ":"
print trk
end
if (fields[1].to_s)=="5"
#Surveillance Alt Message
#DF4, DF20
alt = fields[11].to_s
trk_alt[hexident] = alt
print hexident
print ":"
print "GND ALT"
print ":"
print alt
end
if (fields[1].to_s)=="6"
#Surveillance ID Message
#DF5, DF21
sq = fields[17].to_s
trk_sq[hexident] = sq
alt = fields[11].to_s
trk_alt[hexident] = alt
print hexident
print ":"
print "GND ID"
print ":"
print alt
print ":"
print sq
end
if (fields[1].to_s)=="7"
#Air To Air Message
#DF16
alt = fields[11].to_s
trk_alt[hexident] = alt
print hexident
print ":"
print "TCAS"
print ":"
print alt
end
if (fields[1].to_s)=="8"
#All Call Reply
#DF11
print hexident
print ":"
print "GND ALL"
end
end
print "\r\n"
end
end
sock.close # Close the socket when done