fix: Refactor D-Bus connection to use a single persistent instance
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
20
main.py
20
main.py
@@ -15,6 +15,8 @@ from dbus_next.service import ServiceInterface, method
|
|||||||
from dbus_next import Variant
|
from dbus_next import Variant
|
||||||
|
|
||||||
|
|
||||||
|
bus = None
|
||||||
|
|
||||||
# --- Bluetooth constants and agent ---
|
# --- Bluetooth constants and agent ---
|
||||||
BLUEZ_SERVICE = 'org.bluez'
|
BLUEZ_SERVICE = 'org.bluez'
|
||||||
ADAPTER_IFACE = 'org.bluez.Adapter1'
|
ADAPTER_IFACE = 'org.bluez.Adapter1'
|
||||||
@@ -74,7 +76,6 @@ class Agent(ServiceInterface):
|
|||||||
async def trust_and_connect_device(device_path):
|
async def trust_and_connect_device(device_path):
|
||||||
logging.info(f'Trusting and connecting to {device_path}')
|
logging.info(f'Trusting and connecting to {device_path}')
|
||||||
try:
|
try:
|
||||||
bus = await MessageBus().connect()
|
|
||||||
introspection = await bus.introspect(BLUEZ_SERVICE, device_path)
|
introspection = await bus.introspect(BLUEZ_SERVICE, device_path)
|
||||||
device_obj = bus.get_proxy_object(BLUEZ_SERVICE, device_path, introspection)
|
device_obj = bus.get_proxy_object(BLUEZ_SERVICE, device_path, introspection)
|
||||||
|
|
||||||
@@ -89,7 +90,6 @@ async def trust_and_connect_device(device_path):
|
|||||||
logging.error(f'Failed to trust/connect to {device_path}: {e}')
|
logging.error(f'Failed to trust/connect to {device_path}: {e}')
|
||||||
|
|
||||||
async def get_adapter():
|
async def get_adapter():
|
||||||
bus = await MessageBus().connect()
|
|
||||||
introspection = await bus.introspect(BLUEZ_SERVICE, '/org/bluez')
|
introspection = await bus.introspect(BLUEZ_SERVICE, '/org/bluez')
|
||||||
manager_obj = bus.get_proxy_object(BLUEZ_SERVICE, '/org/bluez', introspection)
|
manager_obj = bus.get_proxy_object(BLUEZ_SERVICE, '/org/bluez', introspection)
|
||||||
manager_iface = manager_obj.get_interface('org.freedesktop.DBus.ObjectManager')
|
manager_iface = manager_obj.get_interface('org.freedesktop.DBus.ObjectManager')
|
||||||
@@ -102,7 +102,6 @@ async def get_adapter():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
async def register_agent():
|
async def register_agent():
|
||||||
bus = await MessageBus().connect()
|
|
||||||
agent = Agent(AGENT_IFACE)
|
agent = Agent(AGENT_IFACE)
|
||||||
bus.export(AGENT_PATH, agent)
|
bus.export(AGENT_PATH, agent)
|
||||||
|
|
||||||
@@ -182,11 +181,18 @@ async def fetch_mqtt():
|
|||||||
loop.create_task(process_mqtt(message))
|
loop.create_task(process_mqtt(message))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
async def main():
|
||||||
|
global bus
|
||||||
|
bus = await MessageBus().connect()
|
||||||
|
|
||||||
logging.info('')
|
logging.info('')
|
||||||
logging.info('==========================')
|
logging.info('==========================')
|
||||||
logging.info('Booting up...')
|
logging.info('Booting up...')
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
manage_task = asyncio.create_task(manage_bluetooth())
|
||||||
a = loop.create_task(manage_bluetooth())
|
mqtt_task = asyncio.create_task(fetch_mqtt())
|
||||||
loop.run_until_complete(fetch_mqtt())
|
await asyncio.gather(manage_task, mqtt_task)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user