via_home: add accuracy, cache history
This commit is contained in:
parent
02ea0b26dd
commit
36beab5f17
2 changed files with 17 additions and 4 deletions
|
@ -73,6 +73,7 @@ class location:
|
||||||
# todo ugh, need to think about it... mypy wants the type here to be general, otherwise it can't deduce
|
# todo ugh, need to think about it... mypy wants the type here to be general, otherwise it can't deduce
|
||||||
# and we can't import the types from the module itself, otherwise would be circular. common module?
|
# and we can't import the types from the module itself, otherwise would be circular. common module?
|
||||||
home: Union[LatLon, Sequence[Tuple[DateIsh, LatLon]]] = (1.0, -1.0)
|
home: Union[LatLon, Sequence[Tuple[DateIsh, LatLon]]] = (1.0, -1.0)
|
||||||
|
home_accuracy = 1000.0
|
||||||
|
|
||||||
class via_ip:
|
class via_ip:
|
||||||
accuracy: float
|
accuracy: float
|
||||||
|
|
|
@ -5,7 +5,7 @@ Simple location provider, serving as a fallback when more detailed data isn't av
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, time, timezone
|
from datetime import datetime, time, timezone
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from typing import Sequence, Tuple, Union, cast
|
from typing import Sequence, Tuple, Union, cast, List
|
||||||
|
|
||||||
from my.config import location as user_config
|
from my.config import location as user_config
|
||||||
|
|
||||||
|
@ -21,6 +21,12 @@ class Config(user_config):
|
||||||
LatLon, # the location
|
LatLon, # the location
|
||||||
]]
|
]]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# default ~1km accuracy
|
||||||
|
# this is called 'home_accuracy' since it lives on the base location.config object,
|
||||||
|
# to differentiate it from accuracy for other providers
|
||||||
|
home_accuracy: float = 1000
|
||||||
|
|
||||||
# TODO could make current Optional and somehow determine from system settings?
|
# TODO could make current Optional and somehow determine from system settings?
|
||||||
@property
|
@property
|
||||||
def _history(self) -> Sequence[Tuple[datetime, LatLon]]:
|
def _history(self) -> Sequence[Tuple[datetime, LatLon]]:
|
||||||
|
@ -73,17 +79,22 @@ def get_location(dt: datetime) -> LatLon:
|
||||||
return hist[-1][1]
|
return hist[-1][1]
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: in python3.9, use functools.cached_property instead?
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def homes_cached() -> List[Tuple[datetime, LatLon]]:
|
||||||
|
return list(config._history)
|
||||||
|
|
||||||
|
|
||||||
def estimate_location(dt: Union[datetime, int, float]) -> FallbackLocation:
|
def estimate_location(dt: Union[datetime, int, float]) -> FallbackLocation:
|
||||||
from my.location.fallback.common import _datetime_timestamp
|
from my.location.fallback.common import _datetime_timestamp
|
||||||
d: float = _datetime_timestamp(dt)
|
d: float = _datetime_timestamp(dt)
|
||||||
# TODO: cache this?
|
hist = list(reversed(homes_cached()))
|
||||||
hist = list(reversed(config._history))
|
|
||||||
for pdt, (lat, lon) in hist:
|
for pdt, (lat, lon) in hist:
|
||||||
if d >= pdt.timestamp():
|
if d >= pdt.timestamp():
|
||||||
# TODO: add accuracy?
|
|
||||||
return FallbackLocation(
|
return FallbackLocation(
|
||||||
lat=lat,
|
lat=lat,
|
||||||
lon=lon,
|
lon=lon,
|
||||||
|
accuracy=config.home_accuracy,
|
||||||
dt=datetime.fromtimestamp(d, timezone.utc),
|
dt=datetime.fromtimestamp(d, timezone.utc),
|
||||||
datasource='via_home')
|
datasource='via_home')
|
||||||
else:
|
else:
|
||||||
|
@ -92,5 +103,6 @@ def estimate_location(dt: Union[datetime, int, float]) -> FallbackLocation:
|
||||||
return FallbackLocation(
|
return FallbackLocation(
|
||||||
lat=lat,
|
lat=lat,
|
||||||
lon=lon,
|
lon=lon,
|
||||||
|
accuracy=config.home_accuracy,
|
||||||
dt=datetime.fromtimestamp(d, timezone.utc),
|
dt=datetime.fromtimestamp(d, timezone.utc),
|
||||||
datasource='via_home')
|
datasource='via_home')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue