Skip to content

Commit

Permalink
Device object Definition
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 27, 2023
1 parent b85bce3 commit 8909907
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 59 deletions.
6 changes: 2 additions & 4 deletions platform/panduza_platform/core/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


85 changes: 85 additions & 0 deletions platform/panduza_platform/core/platform_device.py
Original file line number Diff line number Diff line change
@@ -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()

30 changes: 16 additions & 14 deletions platform/panduza_platform/core/platform_device_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}")

Expand All @@ -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


23 changes: 0 additions & 23 deletions platform/panduza_platform/core/platform_device_model.py

This file was deleted.

7 changes: 4 additions & 3 deletions platform/panduza_platform/devices/ftdi/ft232h_jtag.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -11,15 +11,16 @@
USBID_MODEL="6014"
USBID_SERIAL_SHORT="null"

class DeviceFtdiFt232h_jtag(PlatformDeviceModel):
class DeviceFtdiFt232h_jtag(PlatformDevice):
""" FTDI
"""

def _PZA_DEV_config(self):
"""
"""
return {
"model": "Ftdi.Ft232h_jtag",
"model": "Ft232h_jtag",
"manufacturer": "Ftdi"
}

def _PZA_DEV_interfaces(self):
Expand Down
7 changes: 4 additions & 3 deletions platform/panduza_platform/devices/hanmatek/hm310t.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@

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
"""

def _PZA_DEV_config(self):
"""
"""
return {
"model": "Hanmatek.Hm310t",
"model": "Hm310t",
"manufacturer": "Hanmatek"
}

def _PZA_DEV_interfaces(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
6 changes: 3 additions & 3 deletions tests/pza_platform/tree.json
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit 8909907

Please sign in to comment.