From f216fea70a69bddcf2734155a5928da693dff5ab Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 9 Sep 2023 18:54:56 +0200 Subject: [PATCH] respect timestamps for hfdl locations --- owrx/hfdl/dumphfdl.py | 18 +++++++++++++++++- owrx/map.py | 19 ++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/owrx/hfdl/dumphfdl.py b/owrx/hfdl/dumphfdl.py index 80e1cb9e..52028d6d 100644 --- a/owrx/hfdl/dumphfdl.py +++ b/owrx/hfdl/dumphfdl.py @@ -2,6 +2,7 @@ from pycsdr.modules import ExecModule from pycsdr.types import Format from owrx.aeronautical import AirplaneLocation, AcarsProcessor, IcaoSource from owrx.map import Map, Source +from datetime import datetime, timezone, timedelta import logging @@ -83,4 +84,19 @@ class HFDLMessageParser(AcarsProcessor): source = HfdlSource(hfnpdu["flight_id"]) else: source = IcaoSource(icao, humanReadable=hfnpdu["flight_id"]) - Map.getSharedInstance().updateLocation(source, HfdlAirplaneLocation(msg), "HFDL") + if "utc_time" in hfnpdu: + ts = self.processTimestamp(**hfnpdu["utc_time"]) + elif "time" in hfnpdu: + ts = self.processTimestamp(**hfnpdu["time"]) + else: + ts = None + Map.getSharedInstance().updateLocation(source, HfdlAirplaneLocation(msg), "HFDL", timestamp=ts) + + def processTimestamp(self, hour, min, sec) -> datetime: + now = datetime.now(timezone.utc) + t = now.replace(hour=hour, minute=min, second=sec, microsecond=0) + # if we have moved the time to the future, it's most likely that we're close to midnight and the time + # we received actually refers to yesterday + if t > now: + t -= timedelta(days=1) + return t diff --git a/owrx/map.py b/owrx/map.py index 52a718e9..785c4909 100644 --- a/owrx/map.py +++ b/owrx/map.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from owrx.config import Config from owrx.bands import Band from abc import abstractmethod, ABC, ABCMeta @@ -92,19 +92,24 @@ class Map(object): except ValueError: pass - def updateLocation(self, source: Source, loc: Location, mode: str, band: Band = None): - ts = datetime.now() + def updateLocation(self, source: Source, loc: Location, mode: str, band: Band = None, timestamp: datetime = None): + if timestamp is None: + timestamp = datetime.now(timezone.utc) + else: + # if we get an external timestamp, make sure it's not already expired + if datetime.now(timezone.utc) - loc.getTTL() > timestamp: + return key = source.getKey() with self.positionsLock: if isinstance(loc, IncrementalUpdate) and key in self.positions: loc.update(self.positions[key]["location"]) - self.positions[key] = {"source": source, "location": loc, "updated": ts, "mode": mode, "band": band} + self.positions[key] = {"source": source, "location": loc, "updated": timestamp, "mode": mode, "band": band} self.broadcast( [ { "source": source.__dict__(), "location": loc.__dict__(), - "lastseen": ts.timestamp() * 1000, + "lastseen": timestamp.timestamp() * 1000, "mode": mode, "band": band.getName() if band is not None else None, } @@ -113,7 +118,7 @@ class Map(object): def touchLocation(self, source: Source): # not implemented on the client side yet, so do not use! - ts = datetime.now() + ts = datetime.now(timezone.utc) key = source.getKey() with self.positionsLock: if key in self.positions: @@ -126,7 +131,7 @@ class Map(object): # TODO broadcast removal to clients def removeOldPositions(self): - now = datetime.now() + now = datetime.now(timezone.utc) with self.positionsLock: to_be_removed = [