Merge 2e4c70b636 into 8bf7503a39
This commit is contained in:
commit
923ccb5765
|
|
@ -0,0 +1,38 @@
|
||||||
|
import jsonpickle
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
ts = time.time_ns()
|
||||||
|
data = jsonpickle.encode(arrivals)
|
||||||
|
|
||||||
|
self.wfile.write(f'{{ "timestamp": {ts}, "data": {data}}}'.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()
|
||||||
|
|
@ -27,6 +27,10 @@ class Config:
|
||||||
def update_interval_seconds(self) -> int:
|
def update_interval_seconds(self) -> int:
|
||||||
return self.config.get("update-interval-seconds")
|
return self.config.get("update-interval-seconds")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def http_server(self) -> bool:
|
||||||
|
return self.config.get("http-server")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def stop_codes(self) -> list[str]:
|
def stop_codes(self) -> list[str]:
|
||||||
return [str(s["stop_id"]) for s in self.config.get("stops")]
|
return [str(s["stop_id"]) for s in self.config.get("stops")]
|
||||||
|
|
|
||||||
|
|
@ -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
|
# It must be strictly larger than 60 because the GTFS-R API will throttle us otherwise
|
||||||
update-interval-seconds: 62
|
update-interval-seconds: 62
|
||||||
|
|
||||||
|
# Start a HTTP server emitting the current data as JSON or HTTP.
|
||||||
|
http-server: false
|
||||||
|
|
||||||
stops: [
|
stops: [
|
||||||
{
|
{
|
||||||
# Route 15A
|
# Route 15A
|
||||||
|
|
|
||||||
13
main.py
13
main.py
|
|
@ -11,6 +11,7 @@ from time import sleep
|
||||||
import queue
|
import queue
|
||||||
from arrival_times import ArrivalTime
|
from arrival_times import ArrivalTime
|
||||||
from gtfs_client import GTFSClient
|
from gtfs_client import GTFSClient
|
||||||
|
from arrivals_server import ArrivalsServer
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
# The font is JD LCD Rounded by Jecko Development
|
# The font is JD LCD Rounded by Jecko Development
|
||||||
|
|
@ -35,6 +36,7 @@ INTER_LINE_SPACE = -15
|
||||||
window : pygame.Surface = None
|
window : pygame.Surface = None
|
||||||
font: pygame.font.Font = None
|
font: pygame.font.Font = None
|
||||||
update_queue = queue.Queue(maxsize=10)
|
update_queue = queue.Queue(maxsize=10)
|
||||||
|
arrivals : list[ArrivalTime] = []
|
||||||
|
|
||||||
def get_line_offset(line: int) -> int:
|
def get_line_offset(line: int) -> int:
|
||||||
""" Calculate the Y offset within the display for a given text line """
|
""" 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)
|
vertical_offset = get_line_offset(line)
|
||||||
window.blit(text_img, dest=(XOFFSET_ROUTE, vertical_offset))
|
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:
|
def update_screen(config: Config, updates: list[ArrivalTime]) -> None:
|
||||||
""" Repaint the screen with the new arrival times """
|
""" Repaint the screen with the new arrival times """
|
||||||
|
global arrivals
|
||||||
|
arrivals = updates
|
||||||
updates = updates[0:LINE_COUNT] # take the first X lines
|
updates = updates[0:LINE_COUNT] # take the first X lines
|
||||||
for line_num, update in enumerate(updates):
|
for line_num, update in enumerate(updates):
|
||||||
# Find what color we need to use for the ETA
|
# Find what color we need to use for the ETA
|
||||||
|
|
@ -141,9 +148,13 @@ def main():
|
||||||
routes_for_stops=config.routes_for_stops(),
|
routes_for_stops=config.routes_for_stops(),
|
||||||
update_queue=update_queue,
|
update_queue=update_queue,
|
||||||
update_interval_seconds=config.update_interval_seconds)
|
update_interval_seconds=config.update_interval_seconds)
|
||||||
|
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
|
|
||||||
|
# Start HTTP server
|
||||||
|
if config.http_server:
|
||||||
|
server = ArrivalsServer(get_arrivals=get_arrivals)
|
||||||
|
server.start()
|
||||||
|
|
||||||
# Main event loop
|
# Main event loop
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue