Streamlined device management by introducing a dedicated `object_path` property in `InterfaceDevice`. Refactored `InterfaceDeviceManager` to use this property and accept a list of `InterfaceDevice` instances, removing direct `RevPiModIO` dependencies. Updated D-Bus publishing to reflect the new structure. Signed-off-by: Sven Sager <s.sager@kunbus.com>
112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
"""D-Bus bus provider for revpi_middleware."""
|
|
|
|
from logging import getLogger
|
|
from threading import Thread
|
|
|
|
import revpimodio2
|
|
from gi.repository import GLib
|
|
from pydbus import SessionBus, SystemBus
|
|
|
|
from . import REVPI_DBUS_NAME
|
|
from .interface_devices import (
|
|
InterfaceDeviceManager,
|
|
InterfaceDevice,
|
|
)
|
|
from .interface_ios import (
|
|
InterfaceIoManager,
|
|
InterfaceInput,
|
|
InterfaceOutput,
|
|
)
|
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
class BusProviderIo(Thread):
|
|
|
|
def __init__(
|
|
self,
|
|
picontrol_device="/dev/piControl0",
|
|
config_rsc="/etc/revpi/config.rsc",
|
|
use_system_bus=True,
|
|
):
|
|
log.debug("enter BusProviderIo.__init__")
|
|
super().__init__()
|
|
|
|
self._bus = SystemBus() if use_system_bus else SessionBus()
|
|
self._loop = GLib.MainLoop()
|
|
self._lst_device_interfaces = []
|
|
self._lst_io_interfaces = []
|
|
self._modio = revpimodio2.RevPiModIO(
|
|
procimg=picontrol_device,
|
|
configrsc=config_rsc,
|
|
shared_procimg=True,
|
|
)
|
|
|
|
self.picontrol_device = picontrol_device
|
|
self.config_rsc = config_rsc
|
|
|
|
def run(self):
|
|
log.debug("enter BusProviderIo.run")
|
|
|
|
self._lst_device_interfaces.clear()
|
|
self._lst_io_interfaces.clear()
|
|
|
|
for device in self._modio.device:
|
|
self._lst_device_interfaces.append(
|
|
InterfaceDevice(self._bus, device),
|
|
)
|
|
|
|
for io in self._modio.io:
|
|
interface = None
|
|
try:
|
|
if io.type == revpimodio2.INP:
|
|
interface = InterfaceInput(self._bus, io)
|
|
elif io.type == revpimodio2.OUT:
|
|
interface = InterfaceOutput(self._bus, io)
|
|
elif io.type == revpimodio2.MEM:
|
|
# todo: Implement memory
|
|
pass
|
|
except Exception as e:
|
|
log.warning(f"can not create dbus interface for {io.name}: {e}")
|
|
|
|
if interface is not None:
|
|
self._lst_io_interfaces.append(interface)
|
|
|
|
lst_interfaces = []
|
|
lst_interfaces += [
|
|
(interface.object_path, interface) for interface in self._lst_device_interfaces
|
|
]
|
|
lst_interfaces += [
|
|
(interface.object_path, interface) for interface in self._lst_io_interfaces
|
|
]
|
|
try:
|
|
self._bus.publish(
|
|
REVPI_DBUS_NAME,
|
|
InterfaceDeviceManager(self._lst_device_interfaces),
|
|
InterfaceIoManager(self._lst_io_interfaces, self._modio),
|
|
*lst_interfaces,
|
|
)
|
|
except Exception as e:
|
|
log.error(f"can not publish dbus {REVPI_DBUS_NAME}: {e}")
|
|
|
|
try:
|
|
self._loop.run()
|
|
except Exception as e:
|
|
log.error(f"can not run dbus mainloop: {e}")
|
|
|
|
self._modio.cleanup()
|
|
|
|
log.debug("leave BusProviderIo.run")
|
|
|
|
def stop(self):
|
|
log.debug("enter BusProviderIo.stop")
|
|
self._loop.quit()
|
|
log.debug("leave BusProviderIo.stop")
|
|
|
|
@property
|
|
def running(self):
|
|
return self._loop.is_running()
|