add fallback model

This commit is contained in:
Sean Breckenridge 2023-01-27 11:17:21 -08:00
parent a4468a42da
commit 138afb68a7
3 changed files with 38 additions and 3 deletions

View file

@ -1,5 +1,5 @@
from datetime import date, datetime
from typing import Union, Tuple, NamedTuple, Optional
from typing import Union, Tuple, Optional
from dataclasses import dataclass
from my.core import __NOT_HPI_MODULE__
@ -11,8 +11,10 @@ LatLon = Tuple[float, float]
# TODO: add timezone to this? can use timezonefinder in tz provider instead though
# converted from namedtuple to a dataclass so datasource field can be added optionally
# if we want, can eventually be converted back to a namedtuple when all datasources are compliant
@dataclass
class Location(NamedTuple):
class Location:
lat: float
lon: float
dt: datetime

View file

@ -0,0 +1,33 @@
from dataclasses import dataclass
from typing import Optional
from datetime import datetime, timedelta
from ..common import Location
@dataclass
class FallbackLocation:
lat: float
lon: float
dt: datetime
duration: int # time in seconds for how long this is valid
accuracy: Optional[float] = None
elevation: Optional[float] = None
datasource: Optional[str] = None # which module provided this, useful for debugging
def to_location(self, end: bool = False) -> Location:
"""
by default the start date is used for the location
If end is True, the start date + duration is used
"""
dt: datetime = self.dt
if end:
dt += timedelta(self.duration)
return Location(
lat=self.lat,
lon=self.lon,
dt=dt,
accuracy=self.accuracy,
elevation=self.elevation,
datasource=self.datasource,
)

View file

@ -16,7 +16,7 @@ class config(location.via_ip):
from typing import Iterator
from .common import Location
from ..common import Location
from my.ip.all import ips