adding dynamic attributes in init_device

Dear all,

I'm having some problems while adding dynamic attributes to a device.
In short I have a base device class with a method for creating and adding dynamic attributes parsed from a config file. In an inherited device I want to create the dynamic attributes (using the base class method) and use them inside the init_device. Here's the sample code of the init_device:


/*—– PROTECTED REGION END —–*/	//	SPFDynEmulator::init_device_before
if (Tango::Util::instance()->is_svr_starting() == false  &&
Tango::Util::instance()->is_device_restarting(device_name)==false)
{
  //If not starting up call init device for inherited object
  SEEmulatorBase_ns::SEEmulatorBase::init_device();
}

// Get the device properties from database
get_device_property();

/*—– PROTECTED REGION ID(SPFDynEmulator::init_device) ENABLED START —–*/
//Remove any existing dyn attribute (not sure is really needed…)
//…

//Create and add dynamic attributes parsed from config file
//…

//Use dyn attributes (e.g. find attr and set value, etc)
//…

//Crash occurs here (at init_device end)!

When I do that I get this fault at the end of the method:

SPFDynEmulator::init_device() - SPF startup completed with success…
1479288297 [139917439063808] INFO dserver/SPFDynEmulator/1 spf_mode is a UNWANTED dynamic attribute for device mid_dish_0001/spf/controller
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 In get_monitor() mid_dish_0001/spf/controller, thread = 7, ctr = 0
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 In rem_obj_polling method
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Input string = mid_dish_0001/spf/controller
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Input string = attribute
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Input string = spf_mode
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 In rel_monitor() mid_dish_0001/spf/controller, ctr = 1, thread = 7
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Signalling !
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Entering DeviceClass destructor for class SPFDynEmulator
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Entering DeviceClas::delete_dev method for device with index 0
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Leaving DeviceClass delete_dev
1479288297 [139917439063808] DEBUG dserver/SPFDynEmulator/1 Leaving DeviceClass destructor for class SPFDynEmulator
terminate called after throwing an instance of 'Tango::DevFailed'
Aborted (core dumped)

If however I move the code to create the dynamic attributes in the add_dynamic_attributes() method of the device I get no faults, but on the other hand I cannot use the created dynamic attributes in the init_device method since the add_dynamic_attributes() is inkoved when init_device completes.
Furthermore am I supposed to manually remove the created attributes at the init_device beginning?
Do you have suggestions? I think I'm missing something here.

Thanks a lot,

Simone
****************************************************************
Simone Riggi
INAF, Osservatorio Astrofisico di Catania
Via S. Sofia 78
95123, Catania - Italy
phone: +39 095 7332 extension 282
e-mail: simone.riggi@gmail.com,
sriggi@oact.inaf.it
skype: simone.riggi
****************************************************************
Hi again,

with respect to my previous question I tried to move the code to set dynamic attribute values in the device_factory method after dev->add_dynamic_attributes() has been invoked.
Seems to work now, in case could you confirm this is the correct style of managing dynamical attributes?

Thanks again,

Simone
****************************************************************
Simone Riggi
INAF, Osservatorio Astrofisico di Catania
Via S. Sofia 78
95123, Catania - Italy
phone: +39 095 7332 extension 282
e-mail: simone.riggi@gmail.com,
sriggi@oact.inaf.it
skype: simone.riggi
****************************************************************
Hi Simone,

To better understand the crash you get in the code from your first post, the best is to surround your code in init_device with a try/catch and to display the Tango exception like that:
try
{
    // … your code …
}
catch(const Tango::DevFailed &e)
{
    Tango::Except::print_exception(e);
}
Then we will be able to see what was the exception you've got.

Why don't you add the code to set the values of the attributes in the add_dynamic_attributes() method?
Could you please send us the code that crashes and the backtrace of the crash?

Regarding your questions related to whether you should erase the previously created attributes in your init_device() or not, I would say it depends on your use case.
If you want that the dynamic attributes are re-created when the user execute the Init command (after the device has been exported), then I would say you should take care of removing these attributes yourself.
Otherwise, the common practice is to let Tango deal with it and avoid the INIT command when the dynamic attributes config has changed. Then, in this specific case, if you know the dynamic attributes config has changed, you should use the restart device feature which will re-create your device, erasing the previously created dynamic attributes before creating the dynamic attributes corresponding to your new configuration.

Hoping this helps a bit.

Cheers,
Reynald

Rosenberg's Law: Software is easy to make, except when you want it to do something new.
Corollary: The only software that's worth making is software that does something new.
Dear Reynald,

many thanks for answering my question. At the end I end up doing as you suggested, e.g. creating and initializing the attrs in the add_dynamic_attributes method and worked fine. The sample code was already inserted in try-catch block (not shown for brevity).
Thanks also for clarifications on the dynamic attr management.

Cheers,

Simone
****************************************************************
Simone Riggi
INAF, Osservatorio Astrofisico di Catania
Via S. Sofia 78
95123, Catania - Italy
phone: +39 095 7332 extension 282
e-mail: simone.riggi@gmail.com,
sriggi@oact.inaf.it
skype: simone.riggi
****************************************************************
 
Register or login to create to post a reply.