mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
Fix issue where transports were not disconnecting
This commit is contained in:
parent
3a26acee93
commit
9563a871f1
4 changed files with 35 additions and 9 deletions
|
|
@ -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")
|
||||
|
|
@ -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"""
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue