This commit is contained in:
marios8543 2025-04-26 20:14:10 +03:00
parent ae666a53bd
commit 4f13f56e5b
3 changed files with 43 additions and 29 deletions

View File

@ -2,7 +2,7 @@ import dbus
import dbus.mainloop.glib
from gi.repository import GLib
from asyncio import Queue, get_event_loop
from asyncio import Queue, get_event_loop, CancelledError
from concurrent.futures import ThreadPoolExecutor
class BluetoothState:
@ -47,9 +47,12 @@ class Bluetooth:
self.loop = GLib.MainLoop()
async def run(self):
with ThreadPoolExecutor() as executor:
loop = get_event_loop()
await loop.run_in_executor(executor, self.loop.run)
try:
with ThreadPoolExecutor() as executor:
loop = get_event_loop()
await loop.run_in_executor(executor, self.loop.run)
except CancelledError:
pass
def on_properties_changed(self, interface, changed, invalidated, path):
if interface != "org.bluez.MediaPlayer1":

View File

@ -4,7 +4,7 @@ from bluetooth import Bluetooth, BluetoothState
from json import dumps
from fastapi import FastAPI, WebSocket, Request
from fastapi.responses import PlainTextResponse, JSONResponse
from asyncio import Event, create_task
from asyncio import Event, create_task, CancelledError
from uvicorn import run
app = FastAPI()
@ -23,7 +23,7 @@ async def tunerlist_listener():
while True:
last_tunerlist_state = await tunerlist.queue.get()
event.set()
except asyncio.CancelledError:
except CancelledError:
pass
async def bluetooth_listener():
@ -32,7 +32,7 @@ async def bluetooth_listener():
while True:
last_bluetooth_state = await bluetooth.queue.get()
event.set()
except asyncio.CancelledError:
except CancelledError:
pass
@app.on_event("startup")
@ -47,16 +47,21 @@ async def startup_event():
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
global state_to_send
await websocket.accept()
while True:
await event.wait()
if last_tunerlist_state.is_temp_view and last_bluetooth_state.status == "playing":
state_to_send = last_bluetooth_state.to_dict()
state_to_send["screen"] = last_tunerlist_state.text
if not last_tunerlist_state:
continue
if not last_bluetooth_state:
state_to_send= {"radio": last_tunerlist_state.to_dict()}
elif last_tunerlist_state.is_temp_view and last_bluetooth_state.status == "playing":
state_to_send = {"bluetooth": last_bluetooth_state.to_dict()}
state_to_send["bluetooth"]["screen"] = last_tunerlist_state.text
elif last_tunerlist_state.is_playing_ext:
state_to_send = last_bluetooth_state.to_dict()
state_to_send = {"bluetooth": last_bluetooth_state.to_dict()}
else:
state_to_send = last_tunerlist_state.to_dict()
state_to_send = {"radio": last_tunerlist_state.to_dict()}
await websocket.send_text(dumps(state_to_send))
event.clear()

View File

@ -1,9 +1,11 @@
from pigpio import pi
from asyncio import Queue, get_event_loop, sleep
from asyncio import Queue, get_event_loop, sleep, CancelledError
from concurrent.futures import ThreadPoolExecutor
from os import getenv
GPIO_PIN = 27
BAUD_RATE = 4800
DEBUG = getenv("TL_DEBUG", "false") == "true"
BUTTONS = {
"ok": [0x00, 0x00],
@ -73,23 +75,27 @@ class TunerList:
self.pi.stop()
async def run(self):
while True:
line = await serial_wait_for_line(self.pi)
if line[0] == 0x0F or line[0] == 0x0C:
self.text = line[(8 if line[0] == 0x0F else 5):].decode("ascii")
if (line[3] != 0x20):
self.preset = line[3] & 0x0F
else:
try:
while True:
line = await serial_wait_for_line(self.pi)
if line[0] == 0x0F or line[0] == 0x0C:
self.text = line[(8 if line[0] == 0x0F else 5):].decode("ascii")
if (line[3] != 0x20):
self.preset = line[3] & 0x0F
else:
self.preset = None
elif line[0] == 0x39:
self.preset = None
elif line[0] == 0x39:
self.preset = None
self.text = ""
else:
continue
self.queue.put_nowait(TunerListState(self.text, self.preset))
#for c in line:
# print(hex(c), " ", end="")
#print("[",self.text,"]", f" [P {self.preset}]")
self.text = ""
else:
continue
self.queue.put_nowait(TunerListState(self.text, self.preset))
if DEBUG:
for c in line:
print(hex(c), " ", end="")
print(" [",self.text,"]", f" [P {self.preset}]")
except CancelledError:
pass
async def yield_new_state(self):
while True: