2019-07-07 13:52:24 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import threading, time
|
|
|
|
from owrx.config import PropertyManager
|
2019-07-14 17:32:48 +00:00
|
|
|
from owrx.bands import Band
|
2019-07-07 13:52:24 +00:00
|
|
|
|
|
|
|
import logging
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-07 13:52:24 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-07-01 19:20:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Location(object):
|
|
|
|
def __dict__(self):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
class Map(object):
|
|
|
|
sharedInstance = None
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-01 19:20:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def getSharedInstance():
|
|
|
|
if Map.sharedInstance is None:
|
|
|
|
Map.sharedInstance = Map()
|
|
|
|
return Map.sharedInstance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.clients = []
|
|
|
|
self.positions = {}
|
2019-07-07 13:52:24 +00:00
|
|
|
|
|
|
|
def removeLoop():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.removeOldPositions()
|
|
|
|
except Exception:
|
|
|
|
logger.exception("error while removing old map positions")
|
|
|
|
time.sleep(60)
|
|
|
|
|
|
|
|
threading.Thread(target=removeLoop, daemon=True).start()
|
2019-07-01 19:20:53 +00:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def broadcast(self, update):
|
|
|
|
for c in self.clients:
|
|
|
|
c.write_update(update)
|
|
|
|
|
|
|
|
def addClient(self, client):
|
|
|
|
self.clients.append(client)
|
2019-07-21 17:40:28 +00:00
|
|
|
client.write_update(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"callsign": callsign,
|
|
|
|
"location": record["location"].__dict__(),
|
|
|
|
"lastseen": record["updated"].timestamp() * 1000,
|
|
|
|
"mode": record["mode"],
|
|
|
|
"band": record["band"].getName() if record["band"] is not None else None,
|
|
|
|
}
|
|
|
|
for (callsign, record) in self.positions.items()
|
|
|
|
]
|
|
|
|
)
|
2019-07-01 19:20:53 +00:00
|
|
|
|
|
|
|
def removeClient(self, client):
|
|
|
|
try:
|
|
|
|
self.clients.remove(client)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2019-07-14 17:32:48 +00:00
|
|
|
def updateLocation(self, callsign, loc: Location, mode: str, band: Band = None):
|
2019-07-07 13:52:24 +00:00
|
|
|
ts = datetime.now()
|
2019-07-14 17:32:48 +00:00
|
|
|
self.positions[callsign] = {"location": loc, "updated": ts, "mode": mode, "band": band}
|
2019-07-21 17:40:28 +00:00
|
|
|
self.broadcast(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"callsign": callsign,
|
|
|
|
"location": loc.__dict__(),
|
|
|
|
"lastseen": ts.timestamp() * 1000,
|
|
|
|
"mode": mode,
|
|
|
|
"band": band.getName() if band is not None else None,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2019-07-07 13:52:24 +00:00
|
|
|
|
|
|
|
def removeLocation(self, callsign):
|
|
|
|
self.positions.pop(callsign, None)
|
|
|
|
# TODO broadcast removal to clients
|
|
|
|
|
|
|
|
def removeOldPositions(self):
|
|
|
|
pm = PropertyManager.getSharedInstance()
|
|
|
|
retention = timedelta(seconds=pm["map_position_retention_time"])
|
|
|
|
cutoff = datetime.now() - retention
|
2019-07-01 19:20:53 +00:00
|
|
|
|
2019-07-07 13:52:24 +00:00
|
|
|
to_be_removed = [callsign for (callsign, pos) in self.positions.items() if pos["updated"] < cutoff]
|
|
|
|
for callsign in to_be_removed:
|
|
|
|
self.removeLocation(callsign)
|
2019-07-01 19:20:53 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-01 19:20:53 +00:00
|
|
|
class LatLngLocation(Location):
|
2019-09-18 16:50:48 +00:00
|
|
|
def __init__(self, lat: float, lon: float):
|
2019-07-01 19:20:53 +00:00
|
|
|
self.lat = lat
|
|
|
|
self.lon = lon
|
|
|
|
|
|
|
|
def __dict__(self):
|
2019-08-11 16:42:41 +00:00
|
|
|
res = {"type": "latlon", "lat": self.lat, "lon": self.lon}
|
|
|
|
return res
|
2019-07-01 19:20:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LocatorLocation(Location):
|
|
|
|
def __init__(self, locator: str):
|
|
|
|
self.locator = locator
|
|
|
|
|
|
|
|
def __dict__(self):
|
2019-07-21 17:40:28 +00:00
|
|
|
return {"type": "locator", "locator": self.locator}
|