Fixes
This commit is contained in:
parent
ae666a53bd
commit
4f13f56e5b
@ -2,7 +2,7 @@ import dbus
|
|||||||
import dbus.mainloop.glib
|
import dbus.mainloop.glib
|
||||||
from gi.repository import 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
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
class BluetoothState:
|
class BluetoothState:
|
||||||
@ -47,9 +47,12 @@ class Bluetooth:
|
|||||||
self.loop = GLib.MainLoop()
|
self.loop = GLib.MainLoop()
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
|
try:
|
||||||
with ThreadPoolExecutor() as executor:
|
with ThreadPoolExecutor() as executor:
|
||||||
loop = get_event_loop()
|
loop = get_event_loop()
|
||||||
await loop.run_in_executor(executor, self.loop.run)
|
await loop.run_in_executor(executor, self.loop.run)
|
||||||
|
except CancelledError:
|
||||||
|
pass
|
||||||
|
|
||||||
def on_properties_changed(self, interface, changed, invalidated, path):
|
def on_properties_changed(self, interface, changed, invalidated, path):
|
||||||
if interface != "org.bluez.MediaPlayer1":
|
if interface != "org.bluez.MediaPlayer1":
|
||||||
|
@ -4,7 +4,7 @@ from bluetooth import Bluetooth, BluetoothState
|
|||||||
from json import dumps
|
from json import dumps
|
||||||
from fastapi import FastAPI, WebSocket, Request
|
from fastapi import FastAPI, WebSocket, Request
|
||||||
from fastapi.responses import PlainTextResponse, JSONResponse
|
from fastapi.responses import PlainTextResponse, JSONResponse
|
||||||
from asyncio import Event, create_task
|
from asyncio import Event, create_task, CancelledError
|
||||||
from uvicorn import run
|
from uvicorn import run
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@ -23,7 +23,7 @@ async def tunerlist_listener():
|
|||||||
while True:
|
while True:
|
||||||
last_tunerlist_state = await tunerlist.queue.get()
|
last_tunerlist_state = await tunerlist.queue.get()
|
||||||
event.set()
|
event.set()
|
||||||
except asyncio.CancelledError:
|
except CancelledError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def bluetooth_listener():
|
async def bluetooth_listener():
|
||||||
@ -32,7 +32,7 @@ async def bluetooth_listener():
|
|||||||
while True:
|
while True:
|
||||||
last_bluetooth_state = await bluetooth.queue.get()
|
last_bluetooth_state = await bluetooth.queue.get()
|
||||||
event.set()
|
event.set()
|
||||||
except asyncio.CancelledError:
|
except CancelledError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
@ -47,16 +47,21 @@ async def startup_event():
|
|||||||
|
|
||||||
@app.websocket("/ws")
|
@app.websocket("/ws")
|
||||||
async def websocket_endpoint(websocket: WebSocket):
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
|
global state_to_send
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
while True:
|
while True:
|
||||||
await event.wait()
|
await event.wait()
|
||||||
if last_tunerlist_state.is_temp_view and last_bluetooth_state.status == "playing":
|
if not last_tunerlist_state:
|
||||||
state_to_send = last_bluetooth_state.to_dict()
|
continue
|
||||||
state_to_send["screen"] = last_tunerlist_state.text
|
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:
|
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:
|
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))
|
await websocket.send_text(dumps(state_to_send))
|
||||||
event.clear()
|
event.clear()
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
from pigpio import pi
|
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 concurrent.futures import ThreadPoolExecutor
|
||||||
|
from os import getenv
|
||||||
|
|
||||||
GPIO_PIN = 27
|
GPIO_PIN = 27
|
||||||
BAUD_RATE = 4800
|
BAUD_RATE = 4800
|
||||||
|
DEBUG = getenv("TL_DEBUG", "false") == "true"
|
||||||
|
|
||||||
BUTTONS = {
|
BUTTONS = {
|
||||||
"ok": [0x00, 0x00],
|
"ok": [0x00, 0x00],
|
||||||
@ -73,6 +75,7 @@ class TunerList:
|
|||||||
self.pi.stop()
|
self.pi.stop()
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
|
try:
|
||||||
while True:
|
while True:
|
||||||
line = await serial_wait_for_line(self.pi)
|
line = await serial_wait_for_line(self.pi)
|
||||||
if line[0] == 0x0F or line[0] == 0x0C:
|
if line[0] == 0x0F or line[0] == 0x0C:
|
||||||
@ -87,9 +90,12 @@ class TunerList:
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
self.queue.put_nowait(TunerListState(self.text, self.preset))
|
self.queue.put_nowait(TunerListState(self.text, self.preset))
|
||||||
#for c in line:
|
if DEBUG:
|
||||||
# print(hex(c), " ", end="")
|
for c in line:
|
||||||
#print("[",self.text,"]", f" [P {self.preset}]")
|
print(hex(c), " ", end="")
|
||||||
|
print(" [",self.text,"]", f" [P {self.preset}]")
|
||||||
|
except CancelledError:
|
||||||
|
pass
|
||||||
|
|
||||||
async def yield_new_state(self):
|
async def yield_new_state(self):
|
||||||
while True:
|
while True:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user