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:
@@ -34,10 +34,19 @@ class BusProvider(Thread):
|
|||||||
def run(self):
|
def run(self):
|
||||||
log.debug("enter BusProvider.run")
|
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:
|
try:
|
||||||
self._bus.publish(
|
self._bus.publish(
|
||||||
REVPI_DBUS_NAME,
|
REVPI_DBUS_NAME,
|
||||||
InterfacePiControl(self.picontrol_device, self.config_rsc),
|
*lst_interfaces,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"can not publish dbus {REVPI_DBUS_NAME}: {e}")
|
log.error(f"can not publish dbus {REVPI_DBUS_NAME}: {e}")
|
||||||
@@ -47,6 +56,12 @@ class BusProvider(Thread):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"can not run dbus mainloop: {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")
|
log.debug("leave BusProvider.run")
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|||||||
@@ -11,6 +11,18 @@ REVPI_DBUS_NAME = "com.revolutionpi.middleware1"
|
|||||||
REVPI_DBUS_BASE_PATH = "/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:
|
def extend_interface(*args) -> str:
|
||||||
"""
|
"""
|
||||||
Extends an interface name by appending additional segments to a pre-defined base name.
|
Extends an interface name by appending additional segments to a pre-defined base name.
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ from logging import getLogger
|
|||||||
from pydbus.generic import signal
|
from pydbus.generic import signal
|
||||||
|
|
||||||
from .process_image_helper import PiControlIoctl, ResetDriverWatchdog
|
from .process_image_helper import PiControlIoctl, ResetDriverWatchdog
|
||||||
|
from ..dbus_helper import DbusInterface
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class InterfacePiControl:
|
class InterfacePiControl(DbusInterface):
|
||||||
"""
|
"""
|
||||||
<node>
|
<node>
|
||||||
<interface name='com.revolutionpi.middleware1.picontrol'>
|
<interface name='com.revolutionpi.middleware1.picontrol'>
|
||||||
@@ -32,6 +33,9 @@ class InterfacePiControl:
|
|||||||
self.wd_reset_driver = ResetDriverWatchdog(self.picontrol_device)
|
self.wd_reset_driver = ResetDriverWatchdog(self.picontrol_device)
|
||||||
self.wd_reset_driver.register_call(self.notify_reset_driver)
|
self.wd_reset_driver.register_call(self.notify_reset_driver)
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
self.wd_reset_driver.stop()
|
||||||
|
|
||||||
def notify_reset_driver(self):
|
def notify_reset_driver(self):
|
||||||
self.NotifyDriverReset()
|
self.NotifyDriverReset()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user