add fallback_locations for via_ip

This commit is contained in:
Sean Breckenridge 2023-01-27 11:46:22 -08:00
parent f2c9715dbc
commit 31981b8f34
3 changed files with 62 additions and 10 deletions

View file

@ -10,16 +10,16 @@ class FallbackLocation:
lat: float
lon: float
dt: datetime
duration: int # time in seconds for how long this is valid
duration: float # 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)
@ -31,3 +31,33 @@ class FallbackLocation:
elevation=self.elevation,
datasource=self.datasource,
)
@classmethod
def from_end_date(
cls,
lat: float,
lon: float,
dt: datetime,
end_dt: datetime,
accuracy: Optional[float] = None,
elevation: Optional[float] = None,
datasource: Optional[str] = None,
) -> 'FallbackLocation':
'''
Create FallbackLocation from a start date and an end date
'''
if end_dt < dt:
raise ValueError('end_date must be after dt')
duration = (end_dt - dt).total_seconds()
return cls(
lat=lat,
lon=lon,
dt=dt,
duration=duration,
accuracy=accuracy,
elevation=elevation,
datasource=datasource,
)
# TODO: create estimate location which uses other fallback_locations to estimate a location