From 4f13f56e5bb6a6a5f626ee5f9c46d14cfd910693 Mon Sep 17 00:00:00 2001 From: marios8543 Date: Sat, 26 Apr 2025 20:14:10 +0300 Subject: [PATCH] Fixes --- tunerlistd/bluetooth.py | 11 +++++++---- tunerlistd/main.py | 21 +++++++++++++-------- tunerlistd/tunerlist.py | 40 +++++++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/tunerlistd/bluetooth.py b/tunerlistd/bluetooth.py index cbe4dc1..54ad22e 100644 --- a/tunerlistd/bluetooth.py +++ b/tunerlistd/bluetooth.py @@ -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": diff --git a/tunerlistd/main.py b/tunerlistd/main.py index f722262..8093050 100644 --- a/tunerlistd/main.py +++ b/tunerlistd/main.py @@ -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() diff --git a/tunerlistd/tunerlist.py b/tunerlistd/tunerlist.py index 6023c03..e76ba00 100644 --- a/tunerlistd/tunerlist.py +++ b/tunerlistd/tunerlist.py @@ -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: