0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 21:53:54 +01:00

get to play

This commit is contained in:
2025-12-06 21:58:54 +01:00
parent a9e6e834e7
commit 0b8c4d78ba

View File

@@ -1,6 +1,6 @@
import multiprocessing import multiprocessing
import json import json
import threading import threading, uuid, time
from functools import partial from functools import partial
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn from socketserver import ThreadingMixIn
@@ -16,15 +16,36 @@ class APIHandler(BaseHTTPRequestHandler):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def do_GET(self): def do_GET(self):
self.send_response(200) code = 200
self.send_header("Content-Type", "application/json")
self.end_headers()
if self.path == "/api/playlist": rdata = json.loads(self.data.get("playlist", "[]")) if self.path == "/api/playlist": rdata = json.loads(self.data.get("playlist", "[]"))
elif self.path == "/api/track": rdata = json.loads(self.data.get("track", "{}")) elif self.path == "/api/track": rdata = json.loads(self.data.get("track", "{}"))
elif self.path == "/api/progress": rdata = json.loads(self.data.get("progress", "{}")) elif self.path == "/api/progress": rdata = json.loads(self.data.get("progress", "{}"))
elif self.path == "/api/put":
id = str(uuid.uuid4())
self.imc_q.put({"name": "activemod", "data": {"action": "get_toplay"}, "key": id})
start_time = time.monotonic()
response_json = None
while time.monotonic() - start_time < 2:
if id in self.data:
response_json = self.data.pop(id) # Read and remove the entry
break
time.sleep(0.05) # Wait briefly to avoid a busy loop
if response_json:
try:
rdata = json.loads(response_json)
if "error" in rdata: code = 500 # Server error if module reported one
except (json.JSONDecodeError, TypeError):
rdata = {"error": "Invalid data format from module"}
code = 500
else:
rdata = {"error": "Request to active module timed out"}
code = 504 # Gateway Timeout
else: rdata = {"error": "not found"} else: rdata = {"error": "not found"}
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(rdata).encode('utf-8')) self.wfile.write(json.dumps(rdata).encode('utf-8'))
def do_POST(self): def do_POST(self):
@@ -88,9 +109,10 @@ class Module(PlayerModule):
def _ipc_worker(self): def _ipc_worker(self):
while self.ipc_thread_running: while self.ipc_thread_running:
try: try:
message = self.imc_q.get() message: dict | None = self.imc_q.get()
if message is None: break if message is None: break
self._imc.send(self, message["name"], message["data"]) out = self._imc.send(self, message["name"], message["data"])
if key := message.get("key", None): self.data[f"res_{key}"] = out
except Exception: pass except Exception: pass
def on_new_playlist(self, playlist: list[Track]) -> None: def on_new_playlist(self, playlist: list[Track]) -> None: