2010-10-19 00:59:08 +08:00
|
|
|
#
|
|
|
|
# Copyright 2010 Nick Foster
|
|
|
|
#
|
|
|
|
# This file is part of gr-air-modes
|
|
|
|
#
|
|
|
|
# gr-air-modes is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# gr-air-modes is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gr-air-modes; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
import time, os, sys, socket
|
|
|
|
from string import split, join
|
2012-07-17 10:27:09 +08:00
|
|
|
import air_modes
|
2010-10-18 10:45:19 +08:00
|
|
|
from datetime import *
|
2012-07-17 10:27:09 +08:00
|
|
|
from air_modes.exceptions import *
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-07-17 10:27:09 +08:00
|
|
|
class output_sbs1(air_modes.parse):
|
2012-07-03 22:45:34 +08:00
|
|
|
def __init__(self, mypos, port):
|
2012-07-17 10:27:09 +08:00
|
|
|
air_modes.parse.__init__(self, mypos)
|
2010-10-18 10:45:19 +08:00
|
|
|
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2012-06-01 05:59:30 +08:00
|
|
|
self._s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2012-07-03 22:45:34 +08:00
|
|
|
self._s.bind(('', port))
|
2010-10-18 10:45:19 +08:00
|
|
|
self._s.listen(1)
|
|
|
|
self._s.setblocking(0) #nonblocking
|
|
|
|
self._conns = [] #list of active connections
|
2011-07-08 02:46:51 +08:00
|
|
|
self._aircraft_id_map = {} # dictionary of icao24 to aircraft IDs
|
|
|
|
self._aircraft_id_count = 0 # Current Aircraft ID count
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self._s.close()
|
|
|
|
|
|
|
|
def get_aircraft_id(self, icao24):
|
|
|
|
if icao24 in self._aircraft_id_map:
|
|
|
|
return self._aircraft_id_map[icao24]
|
|
|
|
|
|
|
|
# Adding this new ID to the dictionary
|
|
|
|
self._aircraft_id_count += 1
|
|
|
|
self._aircraft_id_map[icao24] = self._aircraft_id_count
|
|
|
|
|
|
|
|
# Checking to see if we need to clean up in the event that the
|
|
|
|
# dictionary is getting too large.
|
|
|
|
if len(self._aircraft_id_map) > 1e4:
|
2012-11-05 10:40:45 +08:00
|
|
|
minimum = min(self._aircraft_id_map.values()) + (len(self._aircraft_id_map) - 1e4)
|
2012-10-30 00:19:04 +08:00
|
|
|
for icao, _id in self._aircraft_id_map:
|
2012-10-30 00:39:09 +08:00
|
|
|
if _id < minimum:
|
2012-10-30 00:19:04 +08:00
|
|
|
del self._aircraft_id_map[icao]
|
2011-07-08 02:46:51 +08:00
|
|
|
|
|
|
|
# Finally return the new pair
|
|
|
|
return self._aircraft_id_count
|
2010-10-18 13:17:48 +08:00
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
def output(self, msg):
|
2012-06-20 06:43:43 +08:00
|
|
|
try:
|
|
|
|
sbs1_msg = self.parse(msg)
|
2012-06-26 22:46:32 +08:00
|
|
|
if sbs1_msg is not None:
|
|
|
|
for conn in self._conns[:]: #iterate over a copy of the list
|
|
|
|
conn.send(sbs1_msg)
|
2012-06-20 06:43:43 +08:00
|
|
|
except socket.error:
|
|
|
|
self._conns.remove(conn)
|
|
|
|
print "Connections: ", len(self._conns)
|
|
|
|
except ADSBError:
|
|
|
|
pass
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
def add_pending_conns(self):
|
|
|
|
try:
|
|
|
|
conn, addr = self._s.accept()
|
|
|
|
self._conns.append(conn)
|
|
|
|
print "Connections: ", len(self._conns)
|
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
2011-03-27 10:01:12 +08:00
|
|
|
def current_time(self):
|
|
|
|
timenow = datetime.now()
|
2011-07-09 16:40:07 +08:00
|
|
|
return [timenow.strftime("%Y/%m/%d"), timenow.strftime("%H:%M:%S.%f")[0:-3]]
|
2011-03-27 10:01:12 +08:00
|
|
|
|
|
|
|
def decode_fs(self, fs):
|
|
|
|
if fs == 0:
|
|
|
|
return "0,0,0,0"
|
|
|
|
elif fs == 1:
|
|
|
|
return "0,0,0,1"
|
|
|
|
elif fs == 2:
|
|
|
|
return "1,0,0,0"
|
|
|
|
elif fs == 3:
|
|
|
|
return "1,0,0,1"
|
|
|
|
elif fs == 4:
|
2013-01-09 09:25:28 +08:00
|
|
|
return "1,0,1,"
|
2011-03-27 10:01:12 +08:00
|
|
|
elif fs == 5:
|
2013-01-09 09:25:28 +08:00
|
|
|
return "0,0,1,"
|
2011-03-27 10:01:12 +08:00
|
|
|
else:
|
|
|
|
return ",,,"
|
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
def parse(self, message):
|
|
|
|
#assembles a SBS-1-style output string from the received message
|
|
|
|
|
2012-06-27 14:27:58 +08:00
|
|
|
[data, ecc, reference, timestamp] = message.split()
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-07-17 10:27:09 +08:00
|
|
|
data = air_modes.modes_reply(long(data, 16))
|
2010-10-18 10:45:19 +08:00
|
|
|
ecc = long(ecc, 16)
|
2012-06-27 14:27:58 +08:00
|
|
|
msgtype = data["df"]
|
2010-10-18 10:45:19 +08:00
|
|
|
outmsg = None
|
|
|
|
|
2011-03-27 10:01:12 +08:00
|
|
|
if msgtype == 0:
|
2012-06-27 14:27:58 +08:00
|
|
|
outmsg = self.pp0(data, ecc)
|
2011-03-27 10:01:12 +08:00
|
|
|
elif msgtype == 4:
|
2012-06-27 14:27:58 +08:00
|
|
|
outmsg = self.pp4(data, ecc)
|
2011-03-27 10:01:12 +08:00
|
|
|
elif msgtype == 5:
|
2012-06-27 14:27:58 +08:00
|
|
|
outmsg = self.pp5(data, ecc)
|
2011-03-27 10:01:12 +08:00
|
|
|
elif msgtype == 11:
|
2012-06-27 14:27:58 +08:00
|
|
|
outmsg = self.pp11(data, ecc)
|
2011-03-27 10:01:12 +08:00
|
|
|
elif msgtype == 17:
|
2012-06-27 14:27:58 +08:00
|
|
|
outmsg = self.pp17(data)
|
2012-06-21 02:53:28 +08:00
|
|
|
else:
|
2012-06-26 14:09:11 +08:00
|
|
|
raise NoHandlerError(msgtype)
|
2010-10-18 10:45:19 +08:00
|
|
|
return outmsg
|
|
|
|
|
2012-06-14 06:33:20 +08:00
|
|
|
def pp0(self, shortdata, ecc):
|
2011-03-27 10:01:12 +08:00
|
|
|
[datestr, timestr] = self.current_time()
|
2012-06-14 06:33:20 +08:00
|
|
|
[vs, cc, sl, ri, altitude] = self.parse0(shortdata)
|
2011-07-08 02:46:51 +08:00
|
|
|
aircraft_id = self.get_aircraft_id(ecc)
|
2012-06-19 01:25:41 +08:00
|
|
|
retstr = "MSG,7,0,%i,%06X,%i,%s,%s,%s,%s,,%s,,,,,,,,,," % (aircraft_id, ecc, aircraft_id+100, datestr, timestr, datestr, timestr, altitude)
|
2011-03-27 10:01:12 +08:00
|
|
|
if vs:
|
2012-09-15 00:06:51 +08:00
|
|
|
retstr += "1\r\n"
|
2011-03-27 10:01:12 +08:00
|
|
|
else:
|
2012-09-15 00:06:51 +08:00
|
|
|
retstr += "0\r\n"
|
2011-03-27 10:01:12 +08:00
|
|
|
return retstr
|
|
|
|
|
2012-06-14 06:33:20 +08:00
|
|
|
def pp4(self, shortdata, ecc):
|
2011-03-27 10:01:12 +08:00
|
|
|
[datestr, timestr] = self.current_time()
|
2012-06-14 06:33:20 +08:00
|
|
|
[fs, dr, um, altitude] = self.parse4(shortdata)
|
2011-07-08 02:46:51 +08:00
|
|
|
aircraft_id = self.get_aircraft_id(ecc)
|
2012-06-19 01:25:41 +08:00
|
|
|
retstr = "MSG,5,0,%i,%06X,%i,%s,%s,%s,%s,,%s,,,,,,," % (aircraft_id, ecc, aircraft_id+100, datestr, timestr, datestr, timestr, altitude)
|
2012-09-15 00:06:51 +08:00
|
|
|
return retstr + self.decode_fs(fs) + "\r\n"
|
2011-03-27 10:01:12 +08:00
|
|
|
|
2012-06-14 06:33:20 +08:00
|
|
|
def pp5(self, shortdata, ecc):
|
2011-03-27 10:01:12 +08:00
|
|
|
[datestr, timestr] = self.current_time()
|
2012-07-22 02:10:15 +08:00
|
|
|
[fs, dr, um, ident] = self.parse5(shortdata)
|
2011-07-08 02:46:51 +08:00
|
|
|
aircraft_id = self.get_aircraft_id(ecc)
|
2013-01-09 09:25:28 +08:00
|
|
|
retstr = "MSG,6,0,%i,%06X,%i,%s,%s,%s,%s,,,,,,,,%04i," % (aircraft_id, ecc, aircraft_id+100, datestr, timestr, datestr, timestr, ident)
|
2012-09-15 00:06:51 +08:00
|
|
|
return retstr + self.decode_fs(fs) + "\r\n"
|
2011-03-27 10:01:12 +08:00
|
|
|
|
2012-06-14 06:33:20 +08:00
|
|
|
def pp11(self, shortdata, ecc):
|
2011-03-27 10:01:12 +08:00
|
|
|
[datestr, timestr] = self.current_time()
|
2012-06-14 06:33:20 +08:00
|
|
|
[icao24, interrogator, ca] = self.parse11(shortdata, ecc)
|
2011-07-08 02:46:51 +08:00
|
|
|
aircraft_id = self.get_aircraft_id(icao24)
|
2012-09-15 00:06:51 +08:00
|
|
|
return "MSG,8,0,%i,%06X,%i,%s,%s,%s,%s,,,,,,,,,,,,\r\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr)
|
2011-03-27 10:01:12 +08:00
|
|
|
|
2012-06-27 14:27:58 +08:00
|
|
|
def pp17(self, data):
|
|
|
|
icao24 = data["aa"]
|
2011-07-08 02:46:51 +08:00
|
|
|
aircraft_id = self.get_aircraft_id(icao24)
|
2012-07-17 00:16:48 +08:00
|
|
|
bdsreg = data["me"].get_type()
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
retstr = None
|
2011-03-27 10:01:12 +08:00
|
|
|
#we'll get better timestamps later, hopefully with actual VRT time
|
|
|
|
#in them
|
|
|
|
[datestr, timestr] = self.current_time()
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
if bdsreg == 0x08:
|
2011-07-08 02:14:05 +08:00
|
|
|
# Aircraft Identification
|
2012-06-27 14:27:58 +08:00
|
|
|
(msg, typestring) = self.parseBDS08(data)
|
2012-09-15 00:06:51 +08:00
|
|
|
retstr = "MSG,1,0,%i,%06X,%i,%s,%s,%s,%s,%s,,,,,,,,,,,\r\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, msg)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x06:
|
2011-07-08 02:14:05 +08:00
|
|
|
# Surface position measurement
|
2012-06-27 14:27:58 +08:00
|
|
|
[ground_track, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(data)
|
|
|
|
altitude = 0
|
2010-10-18 10:45:19 +08:00
|
|
|
if decoded_lat is None: #no unambiguously valid position available
|
|
|
|
retstr = None
|
|
|
|
else:
|
2012-09-15 00:06:51 +08:00
|
|
|
retstr = "MSG,2,0,%i,%06X,%i,%s,%s,%s,%s,,%i,,,%.5f,%.5f,,,,0,0,0\r\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, altitude, decoded_lat, decoded_lon)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x05:
|
2011-07-08 02:14:05 +08:00
|
|
|
# Airborne position measurements
|
|
|
|
# WRONG (rnge, bearing), is this still true?
|
2012-06-27 14:27:58 +08:00
|
|
|
[altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(data)
|
2010-10-18 10:45:19 +08:00
|
|
|
if decoded_lat is None: #no unambiguously valid position available
|
|
|
|
retstr = None
|
|
|
|
else:
|
2012-09-15 00:06:51 +08:00
|
|
|
retstr = "MSG,3,0,%i,%06X,%i,%s,%s,%s,%s,,%i,,,%.5f,%.5f,,,,0,0,0\r\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, altitude, decoded_lat, decoded_lon)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x09:
|
2011-07-08 02:14:05 +08:00
|
|
|
# Airborne velocity measurements
|
|
|
|
# WRONG (heading, vert_spd), Is this still true?
|
2012-07-17 00:16:48 +08:00
|
|
|
subtype = data["bds09"].get_type()
|
|
|
|
if subtype == 0 or subtype == 1:
|
|
|
|
parser = self.parseBDS09_0 if subtype == 0 else self.parseBDS09_1
|
|
|
|
[velocity, heading, vert_spd] = parser(data)
|
2012-09-15 00:06:51 +08:00
|
|
|
retstr = "MSG,4,0,%i,%06X,%i,%s,%s,%s,%s,,,%.1f,%.1f,,,%i,,,,,\r\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, velocity, heading, vert_spd)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
return retstr
|