Add getting car config

This commit is contained in:
SemvdH
2025-11-17 23:06:26 +01:00
parent 3c9eb9fdbc
commit e8c8a60edf
3 changed files with 167 additions and 123 deletions

View File

@@ -3,7 +3,8 @@ import os
import json
import dbus
from urllib.parse import parse_qs
from change_track import change_track, get_all_tracks, get_configs, get_preview_image, get_outline_image
from ac_maps import change_track, get_all_tracks, get_configs, get_preview_image, get_outline_image
from ac_cars import get_all_cars
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
@@ -27,51 +28,76 @@ class Handler(BaseHTTPRequestHandler):
"""
# Serve preview images
if self.path.startswith("/img/"):
parts = self.path.split("/")
parts = [p for p in parts if p] # remove empty
img_type = parts[1] if len(parts) > 1 else ""
track, config = self.extract_track_and_config(parts)
img_path = ""
if (img_type == "preview"):
img_path = get_preview_image(track, config)
elif (img_type == "outline"):
img_path = get_outline_image(track, config)
if img_path == "":
img_path = get_preview_image(track, config)
else:
self.send_error(404)
return True
self.send_image(img_path)
return True
return self.handle_get_img_path()
# JSON track info
if self.path.startswith("/track/"):
track = self.path.replace("/track/", "").strip("/")
configs = get_configs(track)
data = {
"track": track,
"configs": configs,
"image": f"/img/preview/{track}",
"outline": f"/img/outline/{track}"
}
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data).encode())
return True
return self.handle_get_track_path()
if self.path == "/cars":
return self.handle_get_cars_path()
if self.path == "/" or self.path == "/index.html":
with open("index.html", "r") as f:
html = f.read()
return self.handle_get_root_path()
return False
def handle_get_img_path(self):
parts = self.path.split("/")
parts = [p for p in parts if p] # remove empty
img_type = parts[1] if len(parts) > 1 else ""
img_path = ""
if (img_type == "preview"):
track, config = self.extract_track_and_config(parts)
img_path = get_preview_image(track, config)
elif (img_type == "outline"):
track, config = self.extract_track_and_config(parts)
img_path = get_outline_image(track, config)
if img_path == "":
img_path = get_preview_image(track, config)
else:
self.send_error(404)
return True
self.send_image(img_path)
return True
def handle_get_track_path(self):
track = self.path.replace("/track/", "").strip("/")
configs = get_configs(track)
data = {
"track": track,
"configs": configs,
"image": f"/img/preview/{track}",
"outline": f"/img/outline/{track}"
}
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data).encode())
return True
def handle_get_cars_path(self):
cars = get_all_cars()
data = {
"cars": cars
}
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data).encode())
return True
def handle_get_root_path(self):
with open("index.html", "r") as f:
html = f.read()
tracks = get_all_tracks()
track_options = "".join([f'<option value="{t}">{t}</option>' for t in tracks])
@@ -84,8 +110,6 @@ class Handler(BaseHTTPRequestHandler):
self.end_headers()
self.wfile.write(html.encode())
return True
return False
def handle_POST_path(self):
if (self.path.startswith("/changetrack/")):