2023-05-22 10:05:52 +00:00
|
|
|
from PySide6.QtCore import (Qt, QEvent, QObject, QRunnable, QThreadPool, Signal, Slot)
|
2023-05-21 11:38:34 +00:00
|
|
|
from PySide6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget)
|
|
|
|
|
|
|
|
from BIRD import BIRD
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
|
2023-05-22 10:05:52 +00:00
|
|
|
# Async worker thread
|
|
|
|
class AsyncWorker(QRunnable):
|
|
|
|
@Slot()
|
|
|
|
def run(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
self.loop.run_forever()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
asyncio.run_coroutine_threadsafe(self._stop_internal(), self.loop)
|
|
|
|
|
|
|
|
async def _stop_internal(self):
|
|
|
|
self.loop.stop()
|
|
|
|
|
|
|
|
def dispatch(coro):
|
|
|
|
asyncio.run_coroutine_threadsafe(coro, AsyncWorker.worker.loop)
|
|
|
|
|
|
|
|
if not hasattr(AsyncWorker, "worker"):
|
|
|
|
AsyncWorker.worker = AsyncWorker()
|
2023-05-21 11:38:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
|
|
|
|
start_signal = Signal()
|
|
|
|
done_signal = Signal()
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.bird = BIRD("/run/bird/bird.ctl")
|
|
|
|
self.initial_layout()
|
|
|
|
|
|
|
|
def initial_layout(self):
|
|
|
|
widget = QWidget()
|
|
|
|
self.setCentralWidget(widget)
|
|
|
|
|
|
|
|
layout = QVBoxLayout(widget)
|
|
|
|
|
|
|
|
self.text = QLabel("No connection to BIRD.")
|
|
|
|
layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter)
|
|
|
|
|
|
|
|
async_trigger = QPushButton(text="Connect")
|
2023-05-22 10:05:52 +00:00
|
|
|
async_trigger.clicked.connect(self.connect)
|
2023-05-21 11:38:34 +00:00
|
|
|
layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter)
|
|
|
|
|
|
|
|
@Slot()
|
2023-05-22 10:05:52 +00:00
|
|
|
def connect(self):
|
|
|
|
async def f():
|
|
|
|
async with self.bird as b:
|
|
|
|
await b.version.update()
|
|
|
|
await b.status.update()
|
2023-05-21 11:38:34 +00:00
|
|
|
|
2023-05-22 10:05:52 +00:00
|
|
|
self.text.setText(f"Connected to {b.version.name} {b.version.version}")
|
2023-05-21 11:38:34 +00:00
|
|
|
|
2023-05-22 10:05:52 +00:00
|
|
|
AsyncWorker.dispatch(f())
|
2023-05-21 11:38:34 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app = QApplication(sys.argv)
|
2023-05-22 10:05:52 +00:00
|
|
|
threadpool = QThreadPool()
|
|
|
|
threadpool.start(AsyncWorker.worker)
|
2023-05-21 11:38:34 +00:00
|
|
|
|
2023-05-22 10:05:52 +00:00
|
|
|
mainwindow = MainWindow()
|
|
|
|
mainwindow.show()
|
2023-05-21 11:38:34 +00:00
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
app.exec()
|
2023-05-22 10:05:52 +00:00
|
|
|
AsyncWorker.worker.stop()
|