Another round of improvement. v0.3.3
authorBenjamin Braatz <bb@bbraatz.eu>
Mon, 31 Jul 2023 02:01:32 +0000 (04:01 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Mon, 31 Jul 2023 02:01:32 +0000 (04:01 +0200)
controlpi_plugins/wsclient.py
setup.py

index fd81680cb3517122fa371b5a9f6299bcd0dbce55..686e3ee94bac705a333d591d6befb543934cb677 100644 (file)
@@ -196,14 +196,16 @@ class WSClient(BasePlugin):
         """Connect to wsserver and process messages from it."""
         async for websocket in connect(self.conf['url'],
                                        ping_interval=1, ping_timeout=5):
-            print(f"WSClient '{self.name}': Connection opened"
-                  f" to {self.conf['url']}.")
-            await self.bus.send(Message(self.name,
-                                        {'event':
-                                         'connection opened'}))
             try:
+                # Open connection:
+                print(f"WSClient '{self.name}': Connection opened"
+                      f" to {self.conf['url']}.")
+                await self.bus.send(Message(self.name,
+                                            {'event':
+                                             'connection opened'}))
+                # Configure name, MAC, and filter:
                 conf_command: Dict[str, Any] = \
-                        {'command': 'configure websocket', 'target': ''}
+                    {'command': 'configure websocket', 'target': ''}
                 if 'client' in self.conf:
                     conf_command['name'] = self._client
                 else:
@@ -216,22 +218,24 @@ class WSClient(BasePlugin):
                 conf_command['down filter'] = self._down_filter
                 json_command = json.dumps(conf_command)
                 await websocket.send(json_command)
+                print(f"WSClient '{self.name}': Connection configured"
+                      f" to {self.conf['url']} as '{conf_command['name']}'.")
                 await self.bus.send(Message(self.name,
                                             {'event':
                                              'connection configured'}))
+                # Read incoming messages in loop:
                 self._websocket = websocket
-                try:
-                    async for json_message in websocket:
-                        assert isinstance(json_message, str)
-                        await self._send(json_message)
-                except ConnectionClosed:
-                    self._websocket = None
-                    continue
-                await self.bus.send(Message(self.name,
-                                            {'event':
-                                             'connection closed'}))
+                async for json_message in websocket:
+                    assert isinstance(json_message, str)
+                    await self._send(json_message)
             except Exception as e:
                 print(f"WSClient '{self.name}': Exception in connection: {e}")
+            self._websocket = None
+            print(f"WSClient '{self.name}': Connection closed"
+                  f" to {self.conf['url']}.")
+            await self.bus.send(Message(self.name,
+                                        {'event':
+                                         'connection closed'}))
             await asyncio.sleep(1)
 
     async def _send(self, json_message: str) -> None:
index 8d691029f568a75e36d7c6934aa9e9602c9c12ed..3f6cfef82cf2a9694cb9df19a0f1684ce5185e72 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as readme_file:
 
 setuptools.setup(
     name="controlpi-wsclient",
-    version="0.3.2",
+    version="0.3.3",
     author="Graph-IT GmbH",
     author_email="info@graph-it.com",
     description="ControlPi Plugin for Websocket Clients",