Browse Source

Move AdsbType class population into the class itself

pull/13/head
squeaky otter 2 years ago
parent
commit
e4c5557261
Signed by: dashie GPG Key ID: C2D57B325840B755
4 changed files with 37 additions and 40 deletions
  1. +4
    -3
      airwaves_adsb_client.py
  2. +1
    -33
      libPyAirwaves/adsb.py
  3. +29
    -2
      libPyAirwaves/structs.py
  4. +3
    -2
      simulator.py

+ 4
- 3
airwaves_adsb_client.py View File

@ -3,7 +3,6 @@
blah
"""
import logging
import traceback
import config
import socket
@ -11,7 +10,8 @@ import time
import datetime
from flask_socketio import SocketIO
import config as cfg
from libPyAirwaves.adsb import is_valid_adsb_message, get_adsb_message
from libPyAirwaves.adsb import is_valid_adsb_message
from libPyAirwaves.structs import AdsbType
count_failed_connection = 0
max_failed_connection = 10
@ -84,7 +84,8 @@ if __name__ == "__main__":
# we need 22 items to be a valid looking message
if len(line) == 22:
if is_valid_adsb_message(line):
adsb_message = get_adsb_message(line)
adsb_message = AdsbType()
adsb_message.populate_from_list(line)
# Emit Socket.IO message only if altitude, latitude and longitude are set
# AKA a "MSG,3" message and perhaps a "MSG,2" (Surface position)
if adsb_message.has_location():


+ 1
- 33
libPyAirwaves/adsb.py View File

@ -1,10 +1,9 @@
from typing import List
from libPyAirwaves.structs import AdsbType
import datetime
# List Of String type
list_of_strings = List[str]
# Do some basic checks on the ADS-B received message
# http://woodair.net/sbs/article/barebones42_socket_data.htm
def is_valid_adsb_message(fields: list_of_strings):
@ -37,34 +36,3 @@ def is_valid_adsb_message(fields: list_of_strings):
print(f"Invalid ADSB Message: '{fields}'")
return is_valid
def get_adsb_message(fields: list_of_strings):
"""
Use the splitted ADS-B message and returns an AdsbType structure containing it
:param fields: List of strings containing the ADS-B message, splitted on ','
:return: an AdsbType structure
"""
# TODO: Move that to a function in AdsbType class
# Remember that fields start at 1 but python array at 0, so field 3 (session ID) is at position 2
msg = AdsbType()
msg.addr = fields[4] # int(fields[4], 16)
msg.idInfo = fields[3]
msg.aSquawk = fields[17]
msg.alt = int(fields[11]) if fields[11] else None
msg.lon = float(fields[15]) if fields[15] else None
msg.lat = float(fields[14]) if fields[14] else None
msg.dts = str(datetime.datetime.utcnow())
msg.data = ",".join(fields)
msg.srcPos = False # TODO add position support
if fields[21] == 0 or fields[21] == "0":
msg.vertStat = "air"
else:
msg.vertStat = "gnd"
msg.vertRate = int(fields[16]) if fields[16] else None
msg.category = None
msg.icaoAACC = None
msg.velo = None
msg.heading = None
msg.supersonic = None
return msg

+ 29
- 2
libPyAirwaves/structs.py View File

@ -93,17 +93,42 @@ class AdsbType(DefaultType):
# True if aircraft is moving at supersonic speeds
self.supersonic: bool = None
# Populate class with datas
# Remember that fields start at 1 but python array at 0, so field 3 (session ID) is at position 2
def populate_from_list(self, fields):
pass
self.addr = fields[4] # int(fields[4], 16)
self.idInfo = fields[3]
self.aSquawk = fields[17]
self.alt = int(fields[11]) if fields[11] else None
self.lon = float(fields[15]) if fields[15] else None
self.lat = float(fields[14]) if fields[14] else None
self.dts = str(datetime.datetime.utcnow())
self.data = ",".join(fields)
self.srcPos = False # TODO add position support
if fields[21] == 0 or fields[21] == "0":
self.vertStat = "air"
else:
self.vertStat = "gnd"
self.vertRate = int(fields[16]) if fields[16] else None
self.category = None
self.icaoAACC = None
self.velo = None
self.heading = None
self.supersonic = None
def populate_from_string(self, msg):
self.populate_from_list(msg.split(","))
# Various functions
def to_dict(self):
"""
:return: All variables except `__thoses__` ones, and transform `_things` into `things`
"""
return {k.replace("_", ""): v for k, v in self.__dict__.items() if not (k.startswith("__") and k.endswith("__"))}
return {
k.replace("_", ""): v for k, v in self.__dict__.items() if not (k.startswith("__") and k.endswith("__"))
}
def has_location(self):
if self.lat and self.lon and self.alt:
@ -111,6 +136,8 @@ class AdsbType(DefaultType):
else:
return False
# Setters and Getters
def set_src(self, value):
self._src = value
self.lastSrc = value


+ 3
- 2
simulator.py View File

@ -1,7 +1,7 @@
#!/usr/bin/env python
from real_datas_test import REAL_DATA_ADSB
from libPyAirwaves.adsb import get_adsb_message
from libPyAirwaves.structs import AdsbType
import time
from flask_socketio import SocketIO
import config as cfg
@ -13,7 +13,8 @@ socketio = SocketIO(message_queue=cfg.SOCKETIO_MESSAGE_QUEUE)
print("Sending messages...")
for msg in REAL_DATA_ADSB:
adsb_msg = get_adsb_message(msg.split(","))
adsb_msg = AdsbType()
adsb_msg.populate_from_string(msg)
adsb_msg.entryPoint = "simulator"
adsb_msg.src = cfg.PYAW_HOSTNAME
adsb_msg.clientName = "sim_host"


Loading…
Cancel
Save