From 8909907db41d9646372b012ebf4f3e2589ace1e5 Mon Sep 17 00:00:00 2001 From: XdoctorwhoZ Date: Sat, 26 Aug 2023 12:02:22 +0200 Subject: [PATCH] Device object Definition --- platform/panduza_platform/core/platform.py | 6 +- .../panduza_platform/core/platform_device.py | 85 +++++++++++++++++++ .../core/platform_device_factory.py | 30 ++++--- .../core/platform_device_model.py | 23 ----- .../devices/ftdi/ft232h_jtag.py | 7 +- .../devices/hanmatek/hm310t.py | 7 +- .../devices/panduza/fake_bps_controller.py | 7 +- .../devices/panduza/fake_dio_controller.py | 7 +- .../devices/panduza/fake_relay_controller.py | 7 +- tests/pza_platform/tree.json | 6 +- 10 files changed, 126 insertions(+), 59 deletions(-) create mode 100644 platform/panduza_platform/core/platform_device.py delete mode 100644 platform/panduza_platform/core/platform_device_model.py diff --git a/platform/panduza_platform/core/platform.py b/platform/panduza_platform/core/platform.py index a710cad..7ba239d 100644 --- a/platform/panduza_platform/core/platform.py +++ b/platform/panduza_platform/core/platform.py @@ -523,11 +523,9 @@ def __load_devices(self): # device == class type for device device = self.device_factory.produce_device(device_cfg) - device_name = device_cfg["model"].replace(".", "_") - interfaces = device._PZA_DEV_interfaces() - self.log.info(f"{device_name} => {interfaces}") + self.log.info(f"{device.get_name()} => {interfaces}") for interface_config in interfaces: - self.load_interface("default", device_name, interface_config) + self.load_interface("default", device.get_name(), interface_config) diff --git a/platform/panduza_platform/core/platform_device.py b/platform/panduza_platform/core/platform_device.py new file mode 100644 index 0000000..bf863cc --- /dev/null +++ b/platform/panduza_platform/core/platform_device.py @@ -0,0 +1,85 @@ +import abc +from .platform_errors import InitializationError + +class PlatformDevice: + """This class is a static and generic device builder + It does not hold any attributes or members + """ + + def __init__(self, settings = {}) -> None: + """Constructor + """ + # Settings json provided by the user with the tree.json + self._initial_settings = settings + + # --- + + def initialize(self): + pass + + # --- + + def get_config_field(self, field): + config = self._PZA_DEV_config() + if not field in config: + raise InitializationError(f"\"{field}\" field is not provided in the device builder config {config}") + return config.get(field) + + # --- + + def get_ref(self): + """Unique Identifier for this device model + Different from the name that must be unique for each instance of this device + """ + return self.get_manufacturer() + "." + self.get_model() + + # --- + + def get_name(self): + """Unique Identifier for this device instance + Return a function that can be overloaded by the device implementation to match specific needs + """ + return self._PZA_DEV_unique_name_generator() + + # --- + + def get_base_name(self): + return self.get_manufacturer() + "_" + self.get_model() + + # --- + + def get_model(self): + return self.get_config_field("model") + + # --- + + def get_manufacturer(self): + return self.get_config_field("manufacturer") + + # --- + + @abc.abstractmethod + def _PZA_DEV_config(self): + """ + """ + pass + + # --- + + @abc.abstractmethod + def _PZA_DEV_interfaces(self): + """ + """ + return {} + + # --- + + @abc.abstractmethod + def _PZA_DEV_unique_name_generator(self): + """Must provide a unique and determinist name for the new device + + By default this function does not support multiple instance of the same device on the smae bench. + Because with this simple method, they will have the same name. + """ + return self.get_base_name() + diff --git a/platform/panduza_platform/core/platform_device_factory.py b/platform/panduza_platform/core/platform_device_factory.py index 182e8de..8ea2a54 100644 --- a/platform/panduza_platform/core/platform_device_factory.py +++ b/platform/panduza_platform/core/platform_device_factory.py @@ -11,27 +11,29 @@ class PlatformDeviceFactory: def __init__(self, parent_platform): """ Constructor """ - self.__devices = {} + # The factory is composed of builders + self.__device_templates = {} + self.__platform = parent_platform self.__log = self.__platform.log # --- def produce_device(self, config): - """Try to produce the given device model + """Try to produce the device corresponding the ref """ - # Get model name and control it exists in the config provided by the user - if not "model" in config: - raise InitializationError(f"\"model\" field is not provided in the config {config}") - model = config["model"] + # Get ref and control it exists in the config provided by the user + if not "ref" in config: + raise InitializationError(f"Device \"ref\" field is not provided in the config {config}") + ref = config["ref"] - # Control the model exists in the database - if not model in self.__devices: - raise InitializationError(f"\"{model}\" is not found in this platform") + # Control the ref exists in the database + if not ref in self.__device_templates: + raise InitializationError(f"\"{ref}\" is not found in this platform") # Produce the device try: - return self.__devices[model](config.get("settings", {})) + return self.__device_templates[ref](config.get("settings", {})) except Exception as e: raise InitializationError(f"{traceback.format_exc()}") @@ -47,11 +49,11 @@ def discover(self): # --- - def register_device(self, dev): + def register_device(self, device_builder): """Register a new device model """ - model = dev()._PZA_DEV_config()['model'] - self.__log.info(f"Register device model: '{model}'") - self.__devices[model] = dev + id = device_builder().get_ref() + self.__log.info(f"Register device builder: '{id}'") + self.__device_templates[id] = device_builder diff --git a/platform/panduza_platform/core/platform_device_model.py b/platform/panduza_platform/core/platform_device_model.py deleted file mode 100644 index 23aab3f..0000000 --- a/platform/panduza_platform/core/platform_device_model.py +++ /dev/null @@ -1,23 +0,0 @@ -import abc - -class PlatformDeviceModel: - """Mother class for device models - """ - - def __init__(self, settings = {}) -> None: - """Constructor - """ - self._initial_settings = settings - - @abc.abstractmethod - def _PZA_DEV_config(self): - """ - """ - pass - - @abc.abstractmethod - def _PZA_DEV_interfaces(self): - """ - """ - return {} - diff --git a/platform/panduza_platform/devices/ftdi/ft232h_jtag.py b/platform/panduza_platform/devices/ftdi/ft232h_jtag.py index e421228..cd96ab6 100644 --- a/platform/panduza_platform/devices/ftdi/ft232h_jtag.py +++ b/platform/panduza_platform/devices/ftdi/ft232h_jtag.py @@ -1,5 +1,5 @@ -from core.platform_device_model import PlatformDeviceModel +from core.platform_device import PlatformDevice from connectors.boundary_scan_ftdi import ConnectorBoundaryScanFtdi from extlibs.bsdl_reader import read_Bsdl @@ -11,7 +11,7 @@ USBID_MODEL="6014" USBID_SERIAL_SHORT="null" -class DeviceFtdiFt232h_jtag(PlatformDeviceModel): +class DeviceFtdiFt232h_jtag(PlatformDevice): """ FTDI """ @@ -19,7 +19,8 @@ def _PZA_DEV_config(self): """ """ return { - "model": "Ftdi.Ft232h_jtag", + "model": "Ft232h_jtag", + "manufacturer": "Ftdi" } def _PZA_DEV_interfaces(self): diff --git a/platform/panduza_platform/devices/hanmatek/hm310t.py b/platform/panduza_platform/devices/hanmatek/hm310t.py index 475be11..4a25c4a 100644 --- a/platform/panduza_platform/devices/hanmatek/hm310t.py +++ b/platform/panduza_platform/devices/hanmatek/hm310t.py @@ -1,11 +1,11 @@ -from core.platform_device_model import PlatformDeviceModel +from core.platform_device import PlatformDevice USBID_VENDOR="1a86" USBID_MODEL="7523" TTY_BASE="/dev/ttyUSB" -class DeviceHanmatekHm310t(PlatformDeviceModel): +class DeviceHanmatekHm310t(PlatformDevice): """Power Supply From Hanmatek """ @@ -13,7 +13,8 @@ def _PZA_DEV_config(self): """ """ return { - "model": "Hanmatek.Hm310t", + "model": "Hm310t", + "manufacturer": "Hanmatek" } def _PZA_DEV_interfaces(self): diff --git a/platform/panduza_platform/devices/panduza/fake_bps_controller.py b/platform/panduza_platform/devices/panduza/fake_bps_controller.py index 11f06ff..c625ff9 100644 --- a/platform/panduza_platform/devices/panduza/fake_bps_controller.py +++ b/platform/panduza_platform/devices/panduza/fake_bps_controller.py @@ -1,12 +1,13 @@ -from core.platform_device_model import PlatformDeviceModel +from core.platform_device import PlatformDevice -class DevicePanduzaFakeBps(PlatformDeviceModel): +class DevicePanduzaFakeBps(PlatformDevice): def _PZA_DEV_config(self): """ """ return { - "model": "Panduza.FakeBps", + "model": "FakeBps", + "manufacturer": "Panduza" } def _PZA_DEV_interfaces(self): diff --git a/platform/panduza_platform/devices/panduza/fake_dio_controller.py b/platform/panduza_platform/devices/panduza/fake_dio_controller.py index fdd63a5..e625f3d 100644 --- a/platform/panduza_platform/devices/panduza/fake_dio_controller.py +++ b/platform/panduza_platform/devices/panduza/fake_dio_controller.py @@ -1,13 +1,14 @@ -from core.platform_device_model import PlatformDeviceModel +from core.platform_device import PlatformDevice -class DevicePanduzaFakeDioController(PlatformDeviceModel): +class DevicePanduzaFakeDioController(PlatformDevice): def _PZA_DEV_config(self): """ """ return { - "model": "Panduza.FakeDioController", + "model": "FakeDioController", + "manufacturer": "Panduza" } def _PZA_DEV_interfaces(self): diff --git a/platform/panduza_platform/devices/panduza/fake_relay_controller.py b/platform/panduza_platform/devices/panduza/fake_relay_controller.py index e7c1be3..f372a87 100644 --- a/platform/panduza_platform/devices/panduza/fake_relay_controller.py +++ b/platform/panduza_platform/devices/panduza/fake_relay_controller.py @@ -1,12 +1,13 @@ -from core.platform_device_model import PlatformDeviceModel +from core.platform_device import PlatformDevice -class DevicePanduzaFakeRelayController(PlatformDeviceModel): +class DevicePanduzaFakeRelayController(PlatformDevice): def _PZA_DEV_config(self): """ """ return { - "model": "Panduza.FakeRelayController", + "model": "FakeRelayController", + "manufacturer": "Panduza" } def _PZA_DEV_interfaces(self): diff --git a/tests/pza_platform/tree.json b/tests/pza_platform/tree.json index 130ce46..e21a8eb 100644 --- a/tests/pza_platform/tree.json +++ b/tests/pza_platform/tree.json @@ -1,19 +1,19 @@ { "devices": [ { - "model": "Panduza.FakeBps", + "ref": "Panduza.FakeBps", "settings": { "number_of_channel": 2 } }, { - "model": "Panduza.FakeDioController", + "ref": "Panduza.FakeDioController", "settings": { "number_of_dio": 2 } }, { - "model": "Panduza.FakeRelayController", + "ref": "Panduza.FakeRelayController", "settings": { "number_of_dio": 1 }