From ce363a15b0a53ee224efc00ac28677b724c26c98 Mon Sep 17 00:00:00 2001 From: jens Date: Tue, 24 Nov 2020 21:11:48 +0100 Subject: [PATCH] - provide loop at ws::connect --- brewpi_gui.py | 12 +++++++++--- ws/client/ws_client.py | 2 +- ws/connection.py | 2 +- ws/message.py | 6 +++--- ws/server/ws_server_multi_user.py | 1 + 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/brewpi_gui.py b/brewpi_gui.py index 689e448..05dd359 100644 --- a/brewpi_gui.py +++ b/brewpi_gui.py @@ -5,7 +5,7 @@ from ws.client.ws_client import WsClient from ws.message import MessageDispatcher -class Window(QtWidgets.QMainWindow): +class Window(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.msg_dispatch = MessageDispatcher() @@ -18,8 +18,16 @@ class Window(QtWidgets.QMainWindow): self.msg_sensor.set_recv_handler(self.on_sensor_changed) self.msg_heater.set_recv_handler(self.on_heater_changed) + self.setupUi(self) + self.actionStart.triggered.connect(self.connect) + self.actionStop.triggered.connect(self.disconnect) + + def connect(self): self.ws_client.connect(uri="ws://localhost:8765") + def disconnect(self): + self.ws_client.disconnect() + def on_pot_changed(self, msg): print("on_pot_changed") @@ -51,8 +59,6 @@ class Window(QtWidgets.QMainWindow): if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) win = Window() - ui = Ui_MainWindow() - ui.setupUi(win) win.show() sys.exit(app.exec_()) diff --git a/ws/client/ws_client.py b/ws/client/ws_client.py index e1fe40b..72052c3 100644 --- a/ws/client/ws_client.py +++ b/ws/client/ws_client.py @@ -44,7 +44,7 @@ class WsClient: return print("handler: got connection to {}".format(websocket.remote_address)) - self.listener.on_connect() + self.listener.on_connect(self.loop) try: path = "/" consumer_task = asyncio.ensure_future(self.handler_recv(websocket, path), loop=self.loop) diff --git a/ws/connection.py b/ws/connection.py index 062aaa4..47f049f 100644 --- a/ws/connection.py +++ b/ws/connection.py @@ -3,7 +3,7 @@ import abc class IConnection: @abc.abstractmethod - def on_connect(self): + def on_connect(self, loop): pass @abc.abstractmethod diff --git a/ws/message.py b/ws/message.py index 3e21dad..49a4ea8 100644 --- a/ws/message.py +++ b/ws/message.py @@ -47,7 +47,7 @@ class Value: class MessageDispatcher(IConnection): def __init__(self): self.msg_handlers: MsgIo = [] - self.state = asyncio.Queue() + self.state = None def msgio_get(self, key): obj = MsgIo(key, self.send) @@ -56,8 +56,8 @@ class MessageDispatcher(IConnection): return obj return None - def on_connect(self): - pass + def on_connect(self, loop): + self.state = asyncio.Queue(loop=loop) def on_disconnect(self): pass diff --git a/ws/server/ws_server_multi_user.py b/ws/server/ws_server_multi_user.py index c7d5f20..51626f7 100644 --- a/ws/server/ws_server_multi_user.py +++ b/ws/server/ws_server_multi_user.py @@ -21,6 +21,7 @@ class WsServerMultiUser(WsServer): self.listener = listener self.USERS = UserSet() self.global_state = {} + self.listener.on_connect(self.loop) async def notify_state(self, data): if self.USERS: # asyncio.wait doesn't accept an empty list