refactor(dbus): D-Bus interface management with cleanup support.

Introduced a `DbusInterface` base class with a `cleanup` method to
standardize resource cleanup for D-Bus interfaces. Modified
`InterfacePiControl` to inherit from it and implemented `cleanup` logic
for proper watchdog resource handling. Adjusted `BusProvider` to manage
multiple interfaces and ensure cleanup on shutdown.
This commit is contained in:
2025-04-19 15:55:49 +02:00
parent f8bc1532e3
commit 26c3ac0afb
3 changed files with 33 additions and 2 deletions

View File

@@ -34,10 +34,19 @@ class BusProvider(Thread):
def run(self):
log.debug("enter BusProvider.run")
# The 2nd, 3rd, ... arguments can be objects or tuples of a path and an object
# Example(),
# ("Subdir1", Example()),
# ("Subdir2", Example()),
# ("Subdir2/Whatever", Example())
lst_interfaces = [
InterfacePiControl(self.picontrol_device, self.config_rsc),
]
try:
self._bus.publish(
REVPI_DBUS_NAME,
InterfacePiControl(self.picontrol_device, self.config_rsc),
*lst_interfaces,
)
except Exception as e:
log.error(f"can not publish dbus {REVPI_DBUS_NAME}: {e}")
@@ -47,6 +56,12 @@ class BusProvider(Thread):
except Exception as e:
log.error(f"can not run dbus mainloop: {e}")
# Clean up all interfaces
for interface in lst_interfaces:
if type(interface) is tuple:
_, interface = interface
interface.cleanup()
log.debug("leave BusProvider.run")
def stop(self):

View File

@@ -11,6 +11,18 @@ REVPI_DBUS_NAME = "com.revolutionpi.middleware1"
REVPI_DBUS_BASE_PATH = "/com/revolutionpi/middleware1"
class DbusInterface:
def cleanup(self):
"""
Represents a method responsible for performing cleanup operations. This method is executed to properly
release resources, close connections, or perform other necessary finalization tasks.
This method does not take any arguments or return a value.
"""
pass
def extend_interface(*args) -> str:
"""
Extends an interface name by appending additional segments to a pre-defined base name.

View File

@@ -7,11 +7,12 @@ from logging import getLogger
from pydbus.generic import signal
from .process_image_helper import PiControlIoctl, ResetDriverWatchdog
from ..dbus_helper import DbusInterface
log = getLogger(__name__)
class InterfacePiControl:
class InterfacePiControl(DbusInterface):
"""
<node>
<interface name='com.revolutionpi.middleware1.picontrol'>
@@ -32,6 +33,9 @@ class InterfacePiControl:
self.wd_reset_driver = ResetDriverWatchdog(self.picontrol_device)
self.wd_reset_driver.register_call(self.notify_reset_driver)
def cleanup(self):
self.wd_reset_driver.stop()
def notify_reset_driver(self):
self.NotifyDriverReset()