Add HTTP server.

This commit is contained in:
Gultak 2023-08-23 23:00:14 +01:00
parent b0a2c6545c
commit 64c587d5bd
4 changed files with 53 additions and 2 deletions

33
arrivals_server.py Normal file
View File

@ -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()

View File

@ -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")]

View File

@ -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

13
main.py
View File

@ -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
@ -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: