From 9563a871f143177ac49d928ab9e9d0af4704c2c1 Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Mon, 12 May 2025 21:51:25 -0700 Subject: [PATCH] Fix issue where transports were not disconnecting --- src/meshcore/ble_cx.py | 6 ++++++ src/meshcore/meshcore.py | 22 ++++++++++++++-------- src/meshcore/serial_cx.py | 9 ++++++++- src/meshcore/tcp_cx.py | 7 +++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/meshcore/ble_cx.py b/src/meshcore/ble_cx.py index fbdca89..9a9b321 100644 --- a/src/meshcore/ble_cx.py +++ b/src/meshcore/ble_cx.py @@ -91,3 +91,9 @@ class BLEConnection: logger.error("RX characteristic not found") return False await self.client.write_gatt_char(self.rx_char, bytes(data), response=False) + + async def disconnect(self): + """Disconnect from the BLE device.""" + if self.client and self.client.is_connected: + await self.client.disconnect() + logger.info("BLE Connection closed") diff --git a/src/meshcore/meshcore.py b/src/meshcore/meshcore.py index e52959d..ac335cd 100644 --- a/src/meshcore/meshcore.py +++ b/src/meshcore/meshcore.py @@ -5,7 +5,9 @@ from typing import Optional, Dict, Any, Union from .events import EventDispatcher, EventType from .reader import MessageReader from .commands import CommandHandler - +from .ble_cx import BLEConnection +from .tcp_cx import TCPConnection +from .serial_cx import SerialConnection # Setup default logger logger = logging.getLogger("meshcore") @@ -45,9 +47,7 @@ class MeshCore: @classmethod async def create_tcp(cls, host: str, port: int, debug: bool = False, default_timeout=None) -> 'MeshCore': - """Create and connect a MeshCore instance using TCP connection""" - from .tcp_cx import TCPConnection - + """Create and connect a MeshCore instance using TCP connection""" connection = TCPConnection(host, port) await connection.connect() @@ -58,9 +58,6 @@ class MeshCore: @classmethod async def create_serial(cls, port: str, baudrate: int = 115200, debug: bool = False, default_timeout=None) -> 'MeshCore': """Create and connect a MeshCore instance using serial connection""" - from .serial_cx import SerialConnection - import asyncio - connection = SerialConnection(port, baudrate) await connection.connect() await asyncio.sleep(0.1) # Time for transport to establish @@ -75,7 +72,6 @@ class MeshCore: If address is None, it will scan for and connect to the first available MeshCore device. """ - from .ble_cx import BLEConnection connection = BLEConnection(address) result = await connection.connect() @@ -91,7 +87,17 @@ class MeshCore: return await self.commands.send_appstart() async def disconnect(self): + """Disconnect from the device and clean up resources.""" + # First stop the dispatcher to prevent any new events await self.dispatcher.stop() + + # Stop auto message fetching if it's running + if hasattr(self, '_auto_fetch_subscription') and self._auto_fetch_subscription: + await self.stop_auto_message_fetching() + + # Disconnect the connection object + if self.cx: + await self.cx.disconnect() def stop(self): """Synchronously stop the event dispatcher task""" diff --git a/src/meshcore/serial_cx.py b/src/meshcore/serial_cx.py index b7b07c6..b2284e2 100644 --- a/src/meshcore/serial_cx.py +++ b/src/meshcore/serial_cx.py @@ -86,4 +86,11 @@ class SerialConnection: size = len(data) pkt = b"\x3c" + size.to_bytes(2, byteorder="little") + data logger.debug(f"sending pkt : {pkt}") - self.transport.write(pkt) \ No newline at end of file + self.transport.write(pkt) + + async def disconnect(self): + """Close the serial connection.""" + if self.transport: + self.transport.close() + self.transport = None + logger.info("Serial Connection closed") \ No newline at end of file diff --git a/src/meshcore/tcp_cx.py b/src/meshcore/tcp_cx.py index e90fb2a..3bd4694 100644 --- a/src/meshcore/tcp_cx.py +++ b/src/meshcore/tcp_cx.py @@ -85,3 +85,10 @@ class TCPConnection: pkt = b"\x3c" + size.to_bytes(2, byteorder="little") + data logger.debug(f"sending pkt : {pkt}") self.transport.write(pkt) + + async def disconnect(self): + """Close the TCP connection.""" + if self.transport: + self.transport.close() + self.transport = None + logger.info("TCP Connection closed")