From 64c587d5bd5519ca79aa048dd4985116b2102f96 Mon Sep 17 00:00:00 2001 From: Gultak <2469830+Gultak@users.noreply.github.com> Date: Wed, 23 Aug 2023 23:00:14 +0100 Subject: [PATCH] Add HTTP server. --- arrivals_server.py | 33 +++++++++++++++++++++++++++++++++ config.py | 4 ++++ config.yaml | 3 +++ main.py | 15 +++++++++++++-- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 arrivals_server.py diff --git a/arrivals_server.py b/arrivals_server.py new file mode 100644 index 0000000..4225464 --- /dev/null +++ b/arrivals_server.py @@ -0,0 +1,33 @@ +import jsonpickle +import threading + +from http.server import BaseHTTPRequestHandler, HTTPServer + +PORT=8080 + +class ArrivalsServer: + def __init__(self, get_arrivals): + global _get_arrivals + _get_arrivals = get_arrivals + + class Server(BaseHTTPRequestHandler): + def do_GET(self): + request_path = self.path + arrivals = _get_arrivals() + + print("Request: ", request_path) + + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() + self.wfile.write(jsonpickle.encode(arrivals).encode("utf-8")) + + do_POST = do_GET + do_PUT = do_GET + do_DELETE = do_GET + + def start(self) -> None: + print('HTTP server listening on Port %s' % PORT) + server = HTTPServer(('', PORT), self.Server) + _thread = threading.Thread(target = server.serve_forever) + _thread.start() diff --git a/config.py b/config.py index be39bc7..0ebbbe8 100644 --- a/config.py +++ b/config.py @@ -27,6 +27,10 @@ class Config: def update_interval_seconds(self) -> int: return self.config.get("update-interval-seconds") + @property + def http_server(self) -> bool: + return self.config.get("http-server") + @property def stop_codes(self) -> list[str]: return [str(s["stop_id"]) for s in self.config.get("stops")] diff --git a/config.yaml b/config.yaml index 26b9927..7956d2a 100644 --- a/config.yaml +++ b/config.yaml @@ -12,6 +12,9 @@ gtfs-r-api_key: "API KEY GOES HERE" # It must be strictly larger than 60 because the GTFS-R API will throttle us otherwise update-interval-seconds: 62 +# Start a HTTP server emitting the current data as JSON or HTTP. +http-server: true + stops: [ { # Route 15A diff --git a/main.py b/main.py index 3a6df1a..bcf083f 100755 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from time import sleep import queue from arrival_times import ArrivalTime from gtfs_client import GTFSClient +from arrivals_server import ArrivalsServer # Constants # The font is JD LCD Rounded by Jecko Development @@ -35,6 +36,7 @@ INTER_LINE_SPACE = -15 window : pygame.Surface = None font: pygame.font.Font = None update_queue = queue.Queue(maxsize=10) +arrivals : list[ArrivalTime] = [] def get_line_offset(line: int) -> int: """ Calculate the Y offset within the display for a given text line """ @@ -68,9 +70,14 @@ def write_line(line: int, text: str, text_color: Color = COLOR_LCD_AMBER): vertical_offset = get_line_offset(line) window.blit(text_img, dest=(XOFFSET_ROUTE, vertical_offset)) +def get_arrivals() -> list[ArrivalTime]: + """ Retrieves the current departures list. """ + return arrivals def update_screen(config: Config, updates: list[ArrivalTime]) -> None: """ Repaint the screen with the new arrival times """ + global arrivals + arrivals = updates updates = updates[0:LINE_COUNT] # take the first X lines for line_num, update in enumerate(updates): # Find what color we need to use for the ETA @@ -116,7 +123,7 @@ def main(): global font global window global update_queue - + config = Config() # Initialise graphics context @@ -141,9 +148,13 @@ def main(): routes_for_stops=config.routes_for_stops(), update_queue=update_queue, update_interval_seconds=config.update_interval_seconds) - scheduler.start() + # Start HTTP server + if config.http_server: + server = ArrivalsServer(get_arrivals=get_arrivals) + server.start() + # Main event loop running = True while running: