ogn-python/ogn_python/backend/liveglidernet.py

104 wiersze
3.6 KiB
Python
Czysty Zwykły widok Historia

2017-12-29 15:30:50 +00:00
from datetime import datetime, timedelta, timezone, date
from ogn_python.model import AircraftBeacon, Device, Receiver
2017-12-29 15:30:50 +00:00
2019-03-13 07:34:15 +00:00
from ogn_python import db
2019-05-04 19:32:43 +00:00
from ogn_python.model.receiver_beacon import ReceiverBeacon
2019-03-13 07:34:15 +00:00
2017-12-29 15:30:50 +00:00
def utc_to_local(utc_dt):
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
def encode(address):
return 'xx' + address
def decode(code):
return code[2:9]
2019-06-09 09:04:45 +00:00
def rec(min_timestamp, min_online_timestamp):
2019-05-04 19:32:43 +00:00
last_seen_query = db.session.query(ReceiverBeacon) \
2019-06-09 09:04:45 +00:00
.filter(ReceiverBeacon.timestamp > min_timestamp) \
2019-05-04 19:32:43 +00:00
.order_by(ReceiverBeacon.receiver_id, ReceiverBeacon.timestamp) \
.distinct(ReceiverBeacon.receiver_id)
lines = []
lines.append('<?xml version="1.0" encoding="UTF-8"?>')
lines.append('<markers>')
lines.append('<m e="0"/>')
2019-05-04 19:32:43 +00:00
for receiver_beacon in last_seen_query:
if receiver_beacon.location == None or receiver_beacon.name.startswith('FNB'):
continue
lines.append('<m a="{0}" b="{1:.7f}" c="{2:.7f}" d="{3:1d}"/>'
2019-05-04 19:32:43 +00:00
.format(receiver_beacon.name, receiver_beacon.location.latitude, receiver_beacon.location.longitude, receiver_beacon.timestamp < min_online_timestamp))
lines.append('</markers>')
xml = '\n'.join(lines)
return xml
2019-03-13 07:34:15 +00:00
def lxml(show_offline=False, lat_max=90, lat_min=-90, lon_max=180, lon_min=-180):
2019-05-04 19:32:43 +00:00
timestamp_range_filter = [db.between(AircraftBeacon.timestamp, datetime(2018, 7, 31, 11, 55, 0), datetime(2018, 7, 31, 12, 5, 0))]
2019-05-04 19:32:43 +00:00
last_seen_query = db.session.query(AircraftBeacon) \
.filter(*timestamp_range_filter) \
.order_by(AircraftBeacon.device_id, AircraftBeacon.timestamp) \
.distinct(AircraftBeacon.device_id) \
lines = list()
lines.append('<?xml version="1.0" encoding="UTF-8"?>')
lines.append('<markers>')
2019-05-04 19:32:43 +00:00
for aircraft_beacon in last_seen_query:
device = aircraft_beacon.device
2017-12-29 15:30:50 +00:00
code = encode(device.address)
2019-04-29 20:22:45 +00:00
if device.info:
if (not device.info.tracked or not device.info.identified):
2017-12-29 15:30:50 +00:00
continue
2019-04-29 20:22:45 +00:00
if not device.info.competition:
competition = device.info.registration[-2:]
else:
2019-04-29 20:22:45 +00:00
competition = device.info.competition
2019-04-29 20:22:45 +00:00
if not device.info.registration:
registration = '???'
else:
2019-04-29 20:22:45 +00:00
registration = device.info.registration
2017-12-29 15:30:50 +00:00
address = device.address
else:
competition = ('_' + code[-2:]).lower()
registration = code
address = 0
elapsed_time = datetime.utcnow() - aircraft_beacon.timestamp
elapsed_seconds = int(elapsed_time.total_seconds())
2017-12-29 15:30:50 +00:00
lines.append(' <m a="{0:.7f},{1:.7f},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}"/>'
.format(aircraft_beacon.location.latitude,
aircraft_beacon.location.longitude,
competition,
registration,
2019-01-04 11:19:27 +00:00
int(aircraft_beacon.altitude),
utc_to_local(aircraft_beacon.timestamp).strftime("%H:%M:%S"),
elapsed_seconds,
int(aircraft_beacon.track),
int(aircraft_beacon.ground_speed),
2017-12-29 15:30:50 +00:00
int(aircraft_beacon.climb_rate * 10) / 10,
aircraft_beacon.aircraft_type,
aircraft_beacon.receiver_name,
address,
code))
lines.append('</markers>')
xml = '\n'.join(lines)
2019-05-04 19:32:43 +00:00
return xml