update BDS61 decoder

This commit is contained in:
Junzi Sun 2020-05-23 21:07:23 +02:00
parent 1cf4d37e56
commit 0c5539be3a
3 changed files with 53 additions and 67 deletions

View File

@ -28,7 +28,7 @@ from pyModeS.decoder.bds.bds06 import (
) )
from pyModeS.decoder.bds.bds08 import category, callsign from pyModeS.decoder.bds.bds08 import category, callsign
from pyModeS.decoder.bds.bds09 import airborne_velocity, altitude_diff from pyModeS.decoder.bds.bds09 import airborne_velocity, altitude_diff
from pyModeS.decoder.bds.bds61 import is_emergency, emergency, emergency_squawk from pyModeS.decoder.bds.bds61 import is_emergency, emergency_state, emergency_squawk
def df(msg): def df(msg):

View File

@ -4,95 +4,80 @@
# Aircraft Airborne status # Aircraft Airborne status
# ------------------------------------------ # ------------------------------------------
from __future__ import absolute_import, print_function, division
from pyModeS import common from pyModeS import common
def is_emergency(msg): def is_emergency(msg: str) -> bool:
""" """Check if the aircraft is reporting an emergency.
Check if the aircraft is reporting an emergency
Non-emergencies are either a subtype of zero (no information) or Non-emergencies are either a subtype of zero (no information) or
subtype of one and a value of zero (no emergency). subtype of one and a value of zero (no emergency).
Subtype = 2 indicates an ACAS RA broadcast, look in BDS 3,0 Subtype = 2 indicates an ACAS RA broadcast, look in BDS 3,0
:param msg:
:param msg: 28 bytes hexadecimal message string
:return: if the aircraft has declared an emergency :return: if the aircraft has declared an emergency
""" """
if common.typecode(msg) != 28: if common.typecode(msg) != 28:
raise RuntimeError("%s: Not an airborne status message, expecting TC=28" % msg) raise RuntimeError("%s: Not an airborne status message, expecting TC=28" % msg)
mb = common.hex2bin(msg)[32:] mb = common.hex2bin(msg)[32:]
subtype = common.bin2int(mb[5:8]) subtype = common.bin2int(mb[5:8])
emergency_state = common.bin2int(mb[8:11])
if subtype == 2: if subtype == 2:
raise RuntimeError("%s: Emergency message is ACAS-RA, not implemented") raise RuntimeError("%s: Emergency message is ACAS-RA, not implemented")
if subtype == 0: emergency_state = common.bin2int(mb[8:11])
return False
elif subtype == 1 and emergency_state == 0: if subtype == 1 and emergency_state == 1:
return False
else:
return True return True
def emergency(msg):
"""
Return the aircraft emergency
===== =======
Value Meaning
===== =======
1 General emergency
2 Lifeguard/Medical
3 Minimum fuel
4 No communications
5 Unlawful communications
6 Reserved
7 Reserved
:param msg:
:return: If the aircraft has declared an emergency, the type
"""
if not is_emergency(msg):
raise RuntimeError("%s: Aircraft not declared an emergency" % msg)
else: else:
mb = common.hex2bin(msg)[32:] return False
subtype = common.bin2int(mb[5:8])
emergency_state = common.bin2int(mb[8:11])
return emergency_state
def emergency_squawk(msg): def emergency_state(msg: str) -> int:
"""Decode aircraft emergency state.
Value Meaning
----- -----------------------
0 No emergency
1 General emergency
2 Lifeguard/Medical
3 Minimum fuel
4 No communications
5 Unlawful communications
6-7 Reserved
:param msg: 28 bytes hexadecimal message string
:return: emergency state
""" """
Squawk code of the transmitting aircraft
Emergency value 1 will be squawk 7700 mb = common.hex2bin(msg)[32:]
Emergency value 4 will be squawk 7600 subtype = common.bin2int(mb[5:8])
Emergency value 5 will be squawk 7500
:param msg: if subtype == 2:
raise RuntimeError("%s: Emergency message is ACAS-RA, not implemented")
emergency_state = common.bin2int(mb[8:11])
return emergency_state
def emergency_squawk(msg: str) -> str:
"""Decode squawk code.
Emergency value 1: squawk 7700.
Emergency value 4: squawk 7600.
Emergency value 5: squawk 7500.
:param msg: 28 bytes hexadecimal message string
:return: aircraft squawk code :return: aircraft squawk code
""" """
if common.typecode(msg) != 28: if common.typecode(msg) != 28:
raise RuntimeError("%s: Not an airborne status message, expecting TC=28" % msg) raise RuntimeError("%s: Not an airborne status message, expecting TC=28" % msg)
mb = common.hex2bin(msg)[32:] msgbin = common.hex2bin(msg)
C1 = mb[11]
A1 = mb[12]
C2 = mb[13]
A2 = mb[14]
C4 = mb[15]
A4 = mb[16]
B1 = mb[17]
D1 = mb[18]
B2 = mb[19]
D2 = mb[20]
B4 = mb[21]
D4 = mb[22]
byte1 = int(A4 + A2 + A1, 2) # construct the 13 bits Mode A ID code
byte2 = int(B4 + B2 + B1, 2) idcode = msgbin[43:49] + "0" + msgbin[49:55]
byte3 = int(C4 + C2 + C1, 2)
byte4 = int(D4 + D2 + D1, 2) squawk = common.squawk(idcode)
return str(byte1) + str(byte2) + str(byte3) + str(byte4) return squawk

View File

@ -81,8 +81,9 @@ def test_adsb_velocity():
def test_adsb_emergency(): def test_adsb_emergency():
assert not adsb.is_emergency('8DA2C1B6E112B600000000760759') assert not adsb.is_emergency("8DA2C1B6E112B600000000760759")
assert adsb.emergency_squawk('8DA2C1B6E112B600000000760759') == '6615' assert adsb.emergency_state("8DA2C1B6E112B600000000760759") == 0
assert adsb.emergency_squawk("8DA2C1B6E112B600000000760759") == "6615"
# def test_nic(): # def test_nic():