mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-11 11:56:18 +00:00
Fix issue where transports were not disconnecting
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
@@ -46,8 +48,6 @@ 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
|
||||
|
||||
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,8 +87,18 @@ 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"""
|
||||
if self.dispatcher._task and not self.dispatcher._task.done():
|
||||
|
||||
@@ -87,3 +87,10 @@ class SerialConnection:
|
||||
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 serial connection."""
|
||||
if self.transport:
|
||||
self.transport.close()
|
||||
self.transport = None
|
||||
logger.info("Serial Connection closed")
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user