Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions flight_aware_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# configure a file logger

FORMAT = '%(asctime)s %(levelname)-8s %(message)s'
logger = logging.getLogger(__name__)
logger = None

FLIGHTS = FlightCollection()
MILES_PER_METER = 0.000621371
Expand Down Expand Up @@ -48,10 +48,10 @@ def publish_rec(message, last_message):
return message
return last_message

def cleanup_flight_collection(max_age=3600):
def cleanup_flight_collection(max_age=3600, sleep_time=600):
"""Remove flights that have not been updated in the last n minutes."""
while True:
time.sleep(max_age / 2)
time.sleep(sleep_time)
logger.info("Starting cleanup of flight collection. size=%d", len(FLIGHTS))
time_now = datetime.datetime.now(datetime.timezone.utc)
time_now = time_now.replace(tzinfo=None)
Expand Down Expand Up @@ -101,33 +101,51 @@ def record_positions_to_redis(redis_client):
distance = 0
with py1090.Connection(host=CONFIG.fa_host) as connection:
for line in connection:
update = False
message = py1090.Message.from_string(line)
if message.on_ground:
continue
# continue if there's none of latitude, longitude or callsign
if not (message.latitude or message.longitude or message.callsign):
continue

if message.callsign:
FLIGHTS.add(message)
continue

if message.latitude and message.longitude:
update = True
distance = distance_between(CONFIG.home_latitude,
CONFIG.home_longitude,
message.latitude,
message.longitude) * MILES_PER_METER

message.distance = distance
if distance <= CONFIG.mqtt_distance_max and message.hexident in FLIGHTS:
call_sign, dist = get_call_sign(message.hexident)
logger.info("Updating %s call_sign='%s'", message.hexident,call_sign)
last_message = publish_rec( call_sign, last_message)
FLIGHTS.add(message)
# publish to redis
redis_client.hset(message.hexident, mapping=to_record(message))
if distance <= CONFIG.mqtt_distance_max and message.hexident in FLIGHTS._dictionary:
call_sign, dist = get_call_sign(message.hexident)
logger.info("Updating %s call_sign='%s'", message.hexident,call_sign)
last_message = publish_rec( call_sign, last_message)
message.notified = True
update = True
FLIGHTS.add(message)

if update:
# publish to redis
redis_client.hset(message.hexident, mapping=to_record(message))
msg_count += 1
if msg_count % 1000 == 0:
if msg_count % 10000 == 0:
call_sign, distance = get_call_sign(message.hexident)
logging.info("%d %s recorded. last_dist=%0.2f call_sign=%s", msg_count, message.hexident, distance, call_sign)

def run_loop():
global logger
# reset any existing logger
logging.getLogger().handlers = []
# setup logging after daemon context is created
logging.basicConfig(level=logging.INFO, format=FORMAT, filename=CONFIG.log_filename)
logging.info("Starting to record positions to Redis")
logger = logging.getLogger(__name__)

logger.info("Starting to record positions to Redis")
# create a background thread to execute cleanup_flight_collection

cleanup_thread = threading.Thread(target=cleanup_flight_collection, daemon=True)
Expand Down