From 8c70b19a0c2d3e631acc0fc8f9fbf70cf17df5fe Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Thu, 31 Mar 2022 05:20:05 +0200 Subject: [PATCH] Catch ConnectionRefusedError and handle no connection. --- controlpi_plugins/graph.py | 84 ++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/controlpi_plugins/graph.py b/controlpi_plugins/graph.py index e2b77ab..7dbf095 100644 --- a/controlpi_plugins/graph.py +++ b/controlpi_plugins/graph.py @@ -62,46 +62,58 @@ class Graph(BasePlugin): self._messages.append(message) async def _open(self) -> None: - (reader, writer) = await asyncio.open_connection(self._host, - self._port, - ssl=self._ssl_ctx) - self._reader = reader - self._writer = writer - # Read banner: - banner_size_bytes = await self._reader.readexactly(4) - banner_size_int = struct.unpack(' Any: - # Build request: - self._call_id += 1 - request = {'jsonrpc': '2.0', 'method': method, - 'params': params, 'id': self._call_id} - message = msgpack.packb(request) - size_bytes = struct.pack(' None: - # Close connection: - self._writer.close() + if self._writer: + # Close connection: + self._writer.close() + self._reader = None + self._writer = None async def run(self) -> None: """Get coroot instance for name and send connection opened event.""" -- 2.34.1