diff --git a/db-updater/main.py b/db-updater/main.py index 30c9e8f..9a40b5e 100644 --- a/db-updater/main.py +++ b/db-updater/main.py @@ -144,8 +144,11 @@ sa.Column("wind_direction", sa.Integer, key="wind_dir"), sa.Column("wind_quality", sa.Integer), sa.Column("wind_speed", sa.Integer), + # New columns from ground_positions + sa.Column("air_ground", sa.String), + sa.Column("airport", sa.String), ) - VALID_EVENTS = {"position"} + VALID_EVENTS = {"position", "ground_position"} engine_args: dict = {} db_url: str = os.environ["DB_URL"] @@ -480,6 +483,11 @@ def process_keepalive_message(data: dict) -> None: print(f'Based on keepalive["pitr"], we are {behind} behind realtime') +def process_ground_position_message(data: dict) -> None: + """Groundposition message type""" + return add_to_cache(data) + + def disambiguate_altitude(data: dict): """Replaces the alt field in the passed dict with an unambiguous field name""" @@ -534,6 +542,7 @@ def main(): "flightplan": process_flightplan_message, "keepalive": process_keepalive_message, "position": process_position_message, + "ground_position": process_ground_position_message, } consumer = None diff --git a/fids/app.py b/fids/app.py index d6a18c6..ab40e9a 100644 --- a/fids/app.py +++ b/fids/app.py @@ -48,6 +48,15 @@ UTC = timezone.utc +def _get_ground_positions(flight_id: str) -> Iterable: + return ( + positions_engine.execute( + positions.select().where(and_(positions.c.id == flight_id, positions.c.air_ground == "G")).order_by(positions.c.time.desc()) + ) + or [] + ) + + def _get_positions(flight_id: str) -> Iterable: return ( positions_engine.execute( @@ -57,6 +66,15 @@ def _get_positions(flight_id: str) -> Iterable: ) +@app.route("/ground_positions/") +def get_ground_positions(flight_id: str) -> Response: + """Get ground positions for a specific flight_id""" + result = _get_ground_positions(flight_id) + if not result: + abort(404) + return jsonify([dict(e) for e in result]) + + @app.route("/positions/") def get_positions(flight_id: str) -> Response: """Get positions for a specific flight_id""" @@ -240,18 +258,38 @@ def get_map(flight_id: str) -> bytes: positions = list(_get_positions(flight_id)) if not positions: abort(404) + + # Do ground positions + ground_positions = list(_get_ground_positions(flight_id)) + do_gp = True + if not ground_positions: + do_gp = False bearing = 0 if len(positions) > 1: - coord1 = (float(positions[1].latitude), float(positions[1].longitude)) - coord2 = (float(positions[0].latitude), float(positions[0].longitude)) + if do_gp: + result = min(ground_positions[:2], positions[:2], key=lambda x: x[0].time) + # result = ground_positions[:2] + else: + result = positions[:2] + + coord1 = (float(result[1].latitude), float(result[1].longitude)) + coord2 = (float(result[0].latitude), float(result[0].longitude)) bearing = trig.get_cardinal_for_angle(trig.get_bearing_degrees(coord1, coord2)) + else: + result = positions coords = "|".join(f"{pos.latitude},{pos.longitude}" for pos in positions) + gp_coords = "|".join(f"{pos.latitude},{pos.longitude}" for pos in ground_positions) google_maps_url = "https://maps.googleapis.com/maps/api/staticmap" google_maps_params = { "size": "640x400", - "markers": f"anchor:center|icon:https://github.com/flightaware/fids_frontend/raw/master/images/aircraft_{bearing}.png|{positions[0].latitude},{positions[0].longitude}", - "path": f"color:0x0000ff|weight:5|{coords}", + "markers": [ + f"anchor:center|icon:https://github.com/flightaware/fids_frontend/raw/master/images/aircraft_{bearing}.png|{result[0].latitude},{result[0].longitude}", + ], + "path": [ + f"color:0x0000ff|weight:5|{coords}", + f"color:0xff0000|weight:5|{gp_coords}" if gp_coords else "", + ], "key": google_maps_api_key, } response = requests.get(google_maps_url, google_maps_params)