First attempt at SSE backend #3

This commit is contained in:
Ian Renton
2025-12-22 13:02:11 +00:00
parent befaceb2f5
commit fd2986f310
4 changed files with 37 additions and 30 deletions

View File

@@ -3,7 +3,6 @@ from datetime import datetime
import pytz
from core.config import MAX_SPOT_AGE
from spothole import add_spot
# Generic spot provider class. Subclasses of this query the individual APIs for data.
@@ -17,10 +16,12 @@ class SpotProvider:
self.last_spot_time = datetime.min.replace(tzinfo=pytz.UTC)
self.status = "Not Started" if self.enabled else "Disabled"
self.spots = None
self.web_server = None
# Set up the provider, e.g. giving it the spot list to work from
def setup(self, spots):
def setup(self, spots, web_server):
self.spots = spots
self.web_server = web_server
# Start the provider. This should return immediately after spawning threads to access the remote resources
def start(self):
@@ -35,7 +36,7 @@ class SpotProvider:
if datetime.fromtimestamp(spot.time, pytz.UTC) > self.last_spot_time:
# Fill in any blanks and add to the list
spot.infer_missing()
add_spot(spot)
self.add_spot(spot)
self.last_spot_time = datetime.fromtimestamp(max(map(lambda s: s.time, spots)), pytz.UTC)
# Submit a single spot retrieved from the provider. This will be added to the list regardless of its age. Spots
@@ -44,9 +45,16 @@ class SpotProvider:
def submit(self, spot):
# Fill in any blanks and add to the list
spot.infer_missing()
add_spot(spot)
self.add_spot(spot)
self.last_spot_time = datetime.fromtimestamp(spot.time, pytz.UTC)
def add_spot(self, spot):
if not spot.expired():
self.spots.add(spot.id, spot, expire=MAX_SPOT_AGE)
# Ping the web server in case we have any SSE connections that need to see this immediately
if self.web_server:
self.web_server.notify_new_spot(spot)
# Stop any threads and prepare for application shutdown
def stop(self):
raise NotImplementedError("Subclasses must implement this method")