From 8da4a961eac0ddcee348483236e3be2825c41bc1 Mon Sep 17 00:00:00 2001 From: Claudio Scafuri <claudio.scafuri@elettra.eu> Date: Wed, 12 Feb 2025 10:13:33 +0100 Subject: [PATCH] create dynamic attributes with MagnetNames property --- TODO | 4 +-- src/Simulatede2BbaSwitch.py | 69 +++++++++++++++++++++++++++++------- src/Simulatede2BbaSwitch.xmi | 8 +++-- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/TODO b/TODO index 01ede76..452e3dd 100644 --- a/TODO +++ b/TODO @@ -1,3 +1 @@ -- add the possibilty of creating many dynamic attributes with a simple configuration Useful for handling the whole simulator with a single server, like in the real case -- usro un dictiory (map) per salvare i valor accanto al nome dell' attributo. -Useroò un poroerty per la lista dei nomi degli attributi +- configure the MagnetNames property with real E2.0 magnet (sextupole) names diff --git a/src/Simulatede2BbaSwitch.py b/src/Simulatede2BbaSwitch.py index 2102fb8..de46e2e 100644 --- a/src/Simulatede2BbaSwitch.py +++ b/src/Simulatede2BbaSwitch.py @@ -23,6 +23,7 @@ from tango import DebugIt from tango.server import run from tango.server import Device from tango.server import attribute, command +from tango.server import device_property from tango import AttrQuality, DispLevel, DevState from tango import AttrWriteType, PipeWriteType # Additional import @@ -36,15 +37,50 @@ class Simulatede2BbaSwitch(Device): """ Simple boolen r/w variable. The real thing will be a boolen attribute expoter by the Tango MPS interface + + **Properties:** + + - Device Property + MagnetNames + - name of magnets with a BBA switch + - Type:'DevVarStringArray' """ # PROTECTED REGION ID(Simulatede2BbaSwitch.class_variable) ENABLED START # + def add_attr(self, name): + attr = tango.Attr(name, tango.DevBoolean, tango.READ_WRITE) + self.add_attribute(attr, self.read_general, self.write_general) + self._dynamic_attrs[name] = False + dev_m_attr = self.get_device_attr() + wattr = dev_m_attr.get_w_attr_by_name(name) + wattr.set_write_value(False) + + def read_general(self, attr): + name = attr.get_name() + self.debug_stream("Reading attribute %s", name) + attr.set_value(self._dynamic_attrs[name]) + + def write_general(self, attr): + name = attr.get_name() + data = attr.get_write_value() + self._dynamic_attrs[name] = data + self.debug_stream("Writing into attribute %s the value %s", name, data) # PROTECTED REGION END # // Simulatede2BbaSwitch.class_variable + # ----------------- + # Device Properties + # ----------------- + + MagnetNames = device_property( + dtype='DevVarStringArray', + doc="name of magnets with a BBA switch", + mandatory=True + ) + # ---------- # Attributes # ---------- - enable = attribute( + generic = attribute( dtype='DevBoolean', access=AttrWriteType.READ_WRITE, label="bba enable", @@ -58,13 +94,20 @@ class Simulatede2BbaSwitch(Device): def init_device(self): """Initializes the attributes and properties of the Simulatede2BbaSwitch.""" Device.init_device(self) - self._enable = False + self._generic = False # PROTECTED REGION ID(Simulatede2BbaSwitch.init_device) ENABLED START # dev_m_attr = self.get_device_attr() - wattr = dev_m_attr.get_w_attr_by_name("enable") + wattr = dev_m_attr.get_w_attr_by_name("generic") wattr.set_write_value(False) self.set_state(tango.DevState.ON) self.set_status("MPS BBA switches ready") + + + + # add dyn attributes + self._dynamic_attrs = {} + for magname in self.MagnetNames: + self.add_attr(magname) # PROTECTED REGION END # // Simulatede2BbaSwitch.init_device def always_executed_hook(self): @@ -86,16 +129,16 @@ class Simulatede2BbaSwitch(Device): # Attributes methods # ------------------ - def read_enable(self): - # PROTECTED REGION ID(Simulatede2BbaSwitch.enable_read) ENABLED START # - """Return the enable attribute.""" - return self._enable - # PROTECTED REGION END # // Simulatede2BbaSwitch.enable_read - def write_enable(self, value): - # PROTECTED REGION ID(Simulatede2BbaSwitch.enable_write) ENABLED START # - """Set the enable attribute.""" - self._enable = value - # PROTECTED REGION END # // Simulatede2BbaSwitch.enable_write + def read_generic(self): + # PROTECTED REGION ID(Simulatede2BbaSwitch.generic_read) ENABLED START # + """Return the generic attribute.""" + return self._generic + # PROTECTED REGION END # // Simulatede2BbaSwitch.generic_read + def write_generic(self, value): + # PROTECTED REGION ID(Simulatede2BbaSwitch.generic_write) ENABLED START # + """Set the generic attribute.""" + pass + # PROTECTED REGION END # // Simulatede2BbaSwitch.generic_write # -------- # Commands # -------- diff --git a/src/Simulatede2BbaSwitch.xmi b/src/Simulatede2BbaSwitch.xmi index 3f9968e..a394e04 100644 --- a/src/Simulatede2BbaSwitch.xmi +++ b/src/Simulatede2BbaSwitch.xmi @@ -1,13 +1,17 @@ <?xml version="1.0" encoding="ASCII"?> <pogoDsl:PogoSystem xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pogoDsl="http://tango.org/pogo/PogoDsl"> <classes name="Simulatede2BbaSwitch" pogoRevision="9.9"> - <description description="Simple boolen r/w variable.
The real thing will be a boolen attribute expoter by the Tango MPS interface" title="Simulatede2BbaSwitch" sourcePath="/home/claudio/src/gitlab/dt/ds/simulatede2bbaswitch/src" language="PythonHL" filestogenerate="XMI file,Code files,Protected Regions" license="GPL" copyright="Elettra - Sincrotrone Trieste S.C.p.A." hasMandatoryProperty="false" hasConcreteProperty="false" hasAbstractCommand="false" hasAbstractAttribute="false"> + <description description="Simple boolen r/w variable.
The real thing will be a boolen attribute expoter by the Tango MPS interface" title="Simulatede2BbaSwitch" sourcePath="/home/claudio/src/gitlab/dt/ds/simulatede2bbaswitch/src" language="PythonHL" filestogenerate="XMI file,Code files,Protected Regions" license="GPL" copyright="Elettra - Sincrotrone Trieste S.C.p.A." hasMandatoryProperty="true" hasConcreteProperty="true" hasAbstractCommand="false" hasAbstractAttribute="false"> <inheritances classname="Device_Impl" sourcePath=""/> <identification contact="at elettra.eu - claudio.scafuri" author="claudio.scafuri" emailDomain="elettra.eu" classFamily="Simulators" siteSpecific="" platform="All Platforms" bus="Not Applicable" manufacturer="none" reference=""> <keyWords>simulator</keyWords> <keyWords>Eelttra 2.0</keyWords> </identification> </description> + <deviceProperties name="MagnetNames" mandatory="true" description="name of magnets with a BBA switch"> + <type xsi:type="pogoDsl:StringVectorType"/> + <status abstract="false" inherited="false" concrete="true" concreteHere="true"/> + </deviceProperties> <commands name="State" description="This command gets the device state (stored in its device_state data member) and returns it to the caller." execMethod="dev_state" displayLevel="OPERATOR" polledPeriod="0"> <argin description="none"> <type xsi:type="pogoDsl:VoidType"/> @@ -26,7 +30,7 @@ </argout> <status abstract="true" inherited="true" concrete="true"/> </commands> - <attributes name="enable" attType="Scalar" rwType="READ_WRITE" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false"> + <attributes name="generic" attType="Scalar" rwType="READ_WRITE" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false"> <dataType xsi:type="pogoDsl:BooleanType"/> <changeEvent fire="false" libCheckCriteria="false"/> <archiveEvent fire="false" libCheckCriteria="false"/> -- GitLab