Hi,
I am writing a PyTango device which consists of two parts. One is a device class which adds new dynamic attributes with a command, call it ‘TheDevice’. The second is a device which creates instances of the previously mentioned dynamic attribute device, call it ‘DeviceCreator’. Please see below for a minimal working example.
The setup is behaving mostly as I would like it to, but I have one problem.
Current behaviour
When creating a device with DeviceCreator, it retains all the dynamically created attributes from all instances of TheDevice. For example, I create two instances of TheDevice. I add the attribute ‘a’ to one and the attribute ‘b’ to the other. If I now create a new instance of TheDevice using DeviceCreator, this new instance has both attribute ‘a’ and attribute ‘b’.
If I restart the DeviceCreator, the previously remembered attributes are forgotten.
Desired behaviour
I would like every newly created device to have no dynamic attributes after creation.
Possible explanation
According to the documentation for tango.LatestDeviceImpl.add_attribute
It seems like the dynamically added attributes are added to the device class attribute list. This also explains why restarting the DeviceCreator forgets the remembered attributes, since the device class is freshly added to the database.
However, the attributes are not created at the device creation time. They are added with a command, which is always executed after the device has been Init’d.
Minimal working example
I hope this can function as a minimal working example.
TheDevice.py
from tango.server import Device, command
from tango import DevDouble, AttrWriteType
class TheDevice(Device):
@command(dtype_in=str)
def AddAttribute(self, name):
attribute = Attr(name, DevDouble, AttrWriteType.READ)
self.add_attribute(attribute, r_meth=self.read)
def read(self, attribute):
attribute.set_value(10.0) # Example value
def init_device(self):
Device.init_device(self)
DeviceCreator.py
from tango import Util
from tango.server import Device, command
from TheDevice import TheDevice
import sys
class DeviceCreator(Device):
@command(dtype_in=str)
def CreateDevice(self, name):
Util.instance().create_device("TheDevice", name)
def init_device(self):
Device.init_device(self)
def run():
util = Util(sys.argv)
util.add_class(TheDevice.TangoClassClass, TheDevice)
DeviceCreator.run_server()
if __name__ == "__main__":
run()
If anyone knows a solution or work-around for this problem, it would be very much appreciated!