From df08720cabd6ad063af4200847fc5af7b75c06a6 Mon Sep 17 00:00:00 2001 From: Rodriguez Date: Wed, 15 Nov 2023 08:26:24 +0100 Subject: [PATCH] append function to format grouped interface names --- old/old_client/ftdi_spi_client.py | 55 ------ old/old_client/modbus_client.py | 70 ------- .../aardvark/__init__.py | 6 - .../aardvark/driver_aardvark_spi_master.py | 84 --------- .../driver_ka005p.py | 174 ------------------ old/old_drivers_need_to_be_reworked/io.py | 42 ----- .../panduza_platform/core/platform_device.py | 9 + .../devices/panduza/fake_bps/dev_fake_bps.py | 15 +- platform/tests/manual/load_config.py | 18 +- 9 files changed, 32 insertions(+), 441 deletions(-) delete mode 100644 old/old_client/ftdi_spi_client.py delete mode 100644 old/old_client/modbus_client.py delete mode 100644 old/old_drivers_need_to_be_reworked/aardvark/__init__.py delete mode 100644 old/old_drivers_need_to_be_reworked/aardvark/driver_aardvark_spi_master.py delete mode 100644 old/old_drivers_need_to_be_reworked/driver_ka005p.py delete mode 100644 old/old_drivers_need_to_be_reworked/io.py diff --git a/old/old_client/ftdi_spi_client.py b/old/old_client/ftdi_spi_client.py deleted file mode 100644 index 0b99657b..00000000 --- a/old/old_client/ftdi_spi_client.py +++ /dev/null @@ -1,55 +0,0 @@ -import json -import threading -from ..core import Interface, Attribute, EnsureError, RoField, RwField - -from dataclasses import dataclass - -@dataclass -class Ftdi_Spi(Interface): - """Interface to manage ftdi chip for spi - """ - - interface : Interface = None - - def __post_init__(self): - if self.alias: - pass - elif self.interface: - # Build from an other interface - self.alias = self.interface.alias - self.addr = self.interface.addr - self.port = self.interface.port - self.topic = self.interface.topic - self.client = self.interface.client - - super().__post_init__() - - # TODO should have two RwField - # one for the data and one for the slave selector cs - # === WRITE === - self.add_attribute( - Attribute( - name = "write" - ) - ).add_field( - RwField( - name = "values" - ) - ) - - # TODO should have three RwField - # one for the cs, one for the size and one for the data - # cs is the slave selector so that the user can chose whichever slave they want to read/write - # size id the number of byte the user wants to read - # data is the data where the slave response is (RoField better ?) - # how to add a second field to an attribute ? - # === READ === - self.add_attribute( - Attribute( - name = "read" - ) - ).add_field( - RwField( - name = "values" - ) - ) \ No newline at end of file diff --git a/old/old_client/modbus_client.py b/old/old_client/modbus_client.py deleted file mode 100644 index f1f9135e..00000000 --- a/old/old_client/modbus_client.py +++ /dev/null @@ -1,70 +0,0 @@ -import json -import threading -from ..core import Interface -from ..core import Interface, Attribute, EnsureError, RoField, RwField - -class ModbusClient(Interface): - """Interface to manage power supplies - """ - - # Used when the user want to create a specialized interface from a generic one - interface:Interface = None - - ########################################################################### - def __post_init__(self): - - if self.alias: - pass - elif self.interface: - # Build from an other interface - self.alias = self.interface.alias - self.addr = self.interface.addr - self.port = self.interface.port - self.topic = self.interface.topic - self.client = self.interface.client - - # === HOLDING REGISTERS === - self.add_attribute( - Attribute( - name = "holding_regs" - ) - ).add_field( - RwField( - name = "values" - ) - ) - - # === WATCHLIST === - self.add_attribute( - Attribute( - name = "watchlist" - ) - ).add_field( - RwField( - name = "configs" - ) - ) - - # Init the watchlist - self._watchlist = [] - - ########################################################################### - def watch_holding_regs(self, addr, size, unit, polling_time_s = 2): - """Append an entry in the watchlist - """ - # Append the new entry - self._watchlist.append({ - "type": "holding_regs", - "address": addr, "size": size, "unit": unit,"polling_time_s": polling_time_s - }) - # Set the new list - self.watchlist.configs.set(self._watchlist) - - ########################################################################### - def clear_watchlist(): - """Clear the watchlist - """ - self._watchlist = [] - self.watchlist.configs.set(self._watchlist) - - diff --git a/old/old_drivers_need_to_be_reworked/aardvark/__init__.py b/old/old_drivers_need_to_be_reworked/aardvark/__init__.py deleted file mode 100644 index 5939cbbd..00000000 --- a/old/old_drivers_need_to_be_reworked/aardvark/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -from .driver_aardvark_spi_master import DriverAardvarkSpiMaster - -PZA_DRIVERS_LIST=[ - DriverAardvarkSpiMaster -] - diff --git a/old/old_drivers_need_to_be_reworked/aardvark/driver_aardvark_spi_master.py b/old/old_drivers_need_to_be_reworked/aardvark/driver_aardvark_spi_master.py deleted file mode 100644 index b2b20c37..00000000 --- a/old/old_drivers_need_to_be_reworked/aardvark/driver_aardvark_spi_master.py +++ /dev/null @@ -1,84 +0,0 @@ -from meta_drivers.spi_master import MetaDriverSpiMaster -from ...connectors.spi_master_aardvark import ConnectorSPIMasterAardvark -from connectors.udev_tty import HuntUsbDevs -from collections import ChainMap - -class DriverAardvarkSpiMaster(MetaDriverSpiMaster): - """ - Driver for drive SPI master - """ - - ########################################################################### - ########################################################################### - - # must match with tree.json content - def _PZA_DRV_config(self): - # Extend the common bpc config - return ChainMap( - super()._PZA_DRV_config(), - { - "name": "Aardvark_spi_master", - "description": "SPI master over Aardvark chip", - "compatible": ["Aardvark_spi_master", "py.Aardvark_spi_master"], - }, - ) - - ########################################################################### - ########################################################################### - - def _PZA_DRV_loop_init(self): - # self.log.debug(f"{tree}") - settings = tree["settings"] - - # Get the gate - self.spi_connector = ConnectorSPIMasterAardvark.Get(**settings) - - super()._PZA_DRV_loop_init(tree) - - - - ########################################################################### - ########################################################################### - - def _PZA_DRV_hunt_instances(self): - instances = [] # ConnectorSPIMasterAardvark.hunt() - - # 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC - # 0403:e0d0 Future Technology Devices International, Ltd Total Phase Aardvark I2C/SPI Host Adapter - # note: also detect aardvark probe - Aardvark_UART_VENDOR = "0403" - usb_pieces = HuntUsbDevs(vendor=Aardvark_UART_VENDOR, subsystem="usb") - for p in usb_pieces: - iss = p.get("ID_SERIAL_SHORT") - instances.append( - MetaDriverSpiMaster.__tgen( - "py.Aardvark_spi_master", - Aardvark_UART_VENDOR, - p.get("ID_MODEL_ID", "DEVICE ID NOT AVAILABLE"), - iss, - iss, - ) - ) - - return instances - - - ########################################################################### - ########################################################################### - - def _PZA_DRV_loop_run(self, loop): - """ """ - pass - - ########################################################################### - ########################################################################### - - def _PZA_DRV_loop_err(self): - """ """ - pass - - ########################################################################### - ########################################################################### - - def _PZA_DRV_SPIM_transfer(self, data): - return self.spi_connector.transfer(data, len(data)) diff --git a/old/old_drivers_need_to_be_reworked/driver_ka005p.py b/old/old_drivers_need_to_be_reworked/driver_ka005p.py deleted file mode 100644 index c5eb0361..00000000 --- a/old/old_drivers_need_to_be_reworked/driver_ka005p.py +++ /dev/null @@ -1,174 +0,0 @@ -import time -#from hamcrest import empty -import serial -from panduza_platform import MetaDriverBpc - -class DriverKA005P(MetaDriverBpc): - """ Driver to manage the HM7044 power supply - """ - - ########################################################################### - ########################################################################### - - def __init__(self): - self.ovp = False - self.ocp = False - self.silent = False - super().__init__() - - def config(self): - """ FROM MetaDriver - """ - - return { - "compatible": "ka3005p", - "info": { "type": "bpc", "version": "1.0" }, - } - - ########################################################################### - ########################################################################### - - def setup(self, tree): - """ FROM MetaDriver - """ - # Initialize variables - self.supported_settings = self.api_settings.copy() - - self.api_attributes["voltage"]["max"] = 30 - self.api_attributes["voltage"]["scale"] = 0.01 - - self.api_attributes["current"]["max"] = 5 - self.api_attributes["current"]["scale"] = 0.001 - - self.api_attributes["model_name"] = "KA3005P" - - if "settings" not in tree or "serial_port" not in tree["settings"]: - logger.error("Setting serial_port is mandatory for this driver") - return False - self.serial_port = tree["settings"]["serial_port"] - self.tree_settings = tree["settings"].copy() - - self.__serial = serial.Serial(self.serial_port, 9600, timeout=1) - - # Register commands - self.bpc_register_command("state", self.__set_state) - self.bpc_register_command("voltage", self.__set_voltage) - self.bpc_register_command("current", self.__set_current) - self.bpc_register_command("settings", self.__set_settings) - - for key in self.tree_settings.copy(): - if key not in self.supported_settings: - logger.warning("Driver ka3005p does not support setting " + key + " and will ignore it.") - self.remove_setting(self.supported_settings, key) - else: - self.supported_settings[key] = self.tree_settings[key] - - self.api_attributes["settings"] = self.supported_settings - - ########################################################################### - ########################################################################### - - def on_start(self): - super().on_start() - pass - - ########################################################################### - ########################################################################### - - def loop(self): - """ FROM MetaDriver - """ - return False - - ########################################################################### - ########################################################################### - - def __set_state(self, payload): - """ - """ - req = self.payload_to_dict(payload) - req_state = req["state"] - # Update enable - self.state = req_state - if self.state == "on": - cmd = bytearray(b'OUT1') - elif self.state == "off": - cmd = bytearray(b'OUT0') - self.__serial.write(cmd) - self.bpc_push_attribute("state", self.state) - logger.info(f"new state :" + str(payload)) - - ########################################################################### - ########################################################################### - - def __set_voltage(self, payload): - """ - """ - req = self.payload_to_dict(payload) - self.api_attributes["voltage"]["value"] = req["voltage"] - print(req["voltage"]) - cmd = bytearray(b'VSET1:') + bytearray(str(req["voltage"]), encoding='utf8') - self.__serial.write(cmd) - # Update state - self.bpc_push_attribute("voltage", self.api_attributes["voltage"]) - logger.info(f"new voltage :" + str(payload)) - - ########################################################################### - ########################################################################### - - def __set_current(self, payload): - """ - """ - req = self.payload_to_dict(payload) - self.api_attributes["current"]["value"] = req["current"] - print(req["current"]) - cmd = bytearray(b'ISET1:') + bytearray(str(req["current"]), encoding='utf8') - self.__serial.write(cmd) - # Update state - self.bpc_push_attribute("current", self.api_attributes["current"]) - logger.info(f"new current :" + str(payload)) - pass - - def __set_settings(self, payload): - """ - """ - # Parse request - req = self.payload_to_dict(payload) - req_settings = req["settings"] - if req_settings["ovp"] is not self.ovp: - self.__set_ovp(req_settings["ovp"]) - if req_settings["ocp"] is not self.ocp: - self.__set_ocp(req_settings["ocp"]) - if req_settings["silent"] is not self.silent: - self.__set_silent(req_settings["silent"]) - # Update state - self.settings = req_settings - self.bpc_push_attribute("settings", self.settings) - logger.info(f"new settings:" + str(payload)) - - def __set_ovp(self, value): - print("Will set OVP to " + str(value)) - if value == True: - cmd = bytearray(b'OVP1') - else: - cmd = bytearray(b'OVP0') - self.__serial.write(cmd) - self.ovp = value - - def __set_ocp(self, value): - print("Will set OCP to " + str(value)) - if value == True: - cmd = bytearray(b'OCP1') - else: - cmd = bytearray(b'OCP0') - self.__serial.write(cmd) - self.ocp = value - - def __set_silent(self, value): - print("Will set Silent to " + str(value)) - if value == True: - cmd = bytearray(b'BEEP1') - else: - cmd = bytearray(b'BEEP0') - self.__serial.write(cmd) - self.silent = value diff --git a/old/old_drivers_need_to_be_reworked/io.py b/old/old_drivers_need_to_be_reworked/io.py deleted file mode 100644 index 5c4b6746..00000000 --- a/old/old_drivers_need_to_be_reworked/io.py +++ /dev/null @@ -1,42 +0,0 @@ -import json -from core.platform_driver import PlatformDriver - -class MetaDriverIo(PlatformDriver): - """ Abstract Driver with helper class to manage io interface - """ - - - - ########################################################################### - ########################################################################### - - def _handle_cmds_set(self, payload): - """From MetaDriver - """ - print(payload) - - ########################################################################### - ########################################################################### - - def push_io_value(self, int_value): - """ To publish the value - """ - payload_dict = { - "value": int_value - } - self.push_attribute("value", json.dumps(payload_dict), retain=True) - - ########################################################################### - ########################################################################### - - def push_io_direction(self, str_direction): - """ To publish the value - """ - if str_direction != 'in' and str_direction != 'out': - raise Exception("Direction must be 'in' or 'out'") - - payload_dict = { - "direction": str_direction - } - self.push_attribute("direction", json.dumps(payload_dict), retain=True) - diff --git a/platform/panduza_platform/core/platform_device.py b/platform/panduza_platform/core/platform_device.py index ddbd243d..d2d14b44 100644 --- a/platform/panduza_platform/core/platform_device.py +++ b/platform/panduza_platform/core/platform_device.py @@ -5,6 +5,15 @@ from log.device import device_logger +# --- + +def array_name(array_name, index, suffix): + """To properly format names of grouped interfaces + """ + return f":{array_name}_{index}:_{suffix}" + +# --- + class PlatformDevice: """Represent a Device """ diff --git a/platform/panduza_platform/devices/panduza/fake_bps/dev_fake_bps.py b/platform/panduza_platform/devices/panduza/fake_bps/dev_fake_bps.py index b82441c6..2702eb46 100644 --- a/platform/panduza_platform/devices/panduza/fake_bps/dev_fake_bps.py +++ b/platform/panduza_platform/devices/panduza/fake_bps/dev_fake_bps.py @@ -1,5 +1,4 @@ -from core.platform_device import PlatformDevice - +from core.platform_device import PlatformDevice, array_name from .itf_bpc import InterfacePanduzaFakeBpc from .itf_ammeter import InterfacePanduzaFakeAmmeter @@ -10,8 +9,6 @@ class DevicePanduzaFakeBps(PlatformDevice): # --- def _PZA_DEV_config(self): - """ - """ return { "family": "BPS", "model": "FakeBps", @@ -35,12 +32,14 @@ async def _PZA_DEV_mount_interfaces(self): for chan in range(0, number_of_channel): self.mount_interface( - InterfacePanduzaFakeBpc(name=f":channel_{chan}:_ctrl") + InterfacePanduzaFakeBpc(name=array_name("channel", chan, "ctrl")) ) self.mount_interface( - InterfacePanduzaFakeAmmeter(name=f":channel_{chan}:_am") + InterfacePanduzaFakeAmmeter(name=array_name("channel", chan, "am")) ) self.mount_interface( - InterfacePanduzaFakeVoltmeter(name=f":channel_{chan}:_vm") + InterfacePanduzaFakeVoltmeter(name=array_name("channel", chan, "vm")) ) - + + # --- + diff --git a/platform/tests/manual/load_config.py b/platform/tests/manual/load_config.py index 3c1334dc..65c6691e 100644 --- a/platform/tests/manual/load_config.py +++ b/platform/tests/manual/load_config.py @@ -29,10 +29,24 @@ # ] # }) +# plat.dtree.content.set({ +# "devices": [ +# {'ref': 'Hanmatek.Hm310t', 'settings': {}}, +# {'ref': 'Tenma.72-2710', 'settings': {'usb_serial_short': '00321FCC0454'}} +# ] +# }) + +# plat.dtree.content.set({ +# "devices": [ +# {'ref': 'Hanmatek.Hm310t', 'settings': {}}, +# {'ref': 'Tenma.72-2710', 'settings': {'usb_serial_short': '00321FCC0454'}} +# ] +# }) + plat.dtree.content.set({ "devices": [ - {'ref': 'Hanmatek.Hm310t', 'settings': {}}, - {'ref': 'Tenma.72-2710', 'settings': {'usb_serial_short': '00321FCC0454'}} + {'ref': 'Panduza.FakeBps', 'settings': {'number_of_channel': 1}}, ] }) +