Skip to content
Snippets Groups Projects
Commit 8d49749b authored by Nicolas Leclercq's avatar Nicolas Leclercq
Browse files

Simplified version of the threaded device example

parent 7d436572
Branches
Tags
No related merge requests found
......@@ -41,12 +41,10 @@
<dependency>
<groupId>fr.soleil.lib</groupId>
<artifactId>YAT4Tango-${aol}-${library}-${mode}</artifactId>
<version>1.5.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>fr.soleil.lib</groupId>
<artifactId>YAT-${aol}-${library}-${mode}</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
<developers>
......
......@@ -173,9 +173,6 @@ void GroupManager::init_device()
{
//- task configuration
GroupManagerTask::Config cfg;
cfg.managed_devices = this->managedDevices;
cfg.excluded_devices = this->excludedDevices;
cfg.monitored_attributes = this->monitoredAttributes;
cfg.task_activity_period_ms = kTASK_PERIOD_MS;
cfg.host_device = this;
......
......@@ -119,7 +119,7 @@ public:
//
class
#ifdef WIN32
#ifdef _TG_WINDOWS_
__declspec(dllexport)
#endif
GroupManagerClass : public Tango::DeviceClass
......
......@@ -33,9 +33,7 @@ namespace GroupManager_ns
// ============================================================================
GroupManagerTask::GroupManagerTask (const Config & cfg)
: yat4tango::DeviceTask(cfg.host_device),
m_cfg(cfg),
m_dam(cfg.host_device),
m_grp("managed_group")
m_cfg(cfg)
{
//- configure optional msg handling
this->enable_timeout_msg(false);
......@@ -77,6 +75,7 @@ void GroupManagerTask::process_message (yat::Message& msg)
//- THREAD_PERIODIC ------------------
case yat::TASK_PERIODIC:
{
DEBUG_STREAM << "GroupManagerTask::handle_message::THREAD_PERIODIC" << std::endl;
this->periodic_job_i();
}
break;
......@@ -189,7 +188,7 @@ void GroupManagerTask::post_some_data_to_the_task (double the_data, size_t tmo_m
this->wait_msg_handled(kDATA_MSG, the_data, tmo_ms);
//- asynchronous approach: "post the data then returm immediately"
else
this->post(kDATA_MSG, the_data);
this->post(kDATA_MSG, the_data, 100);
}
catch (const Tango::DevFailed&)
{
......@@ -209,346 +208,5 @@ void GroupManagerTask::periodic_job_i ()
//- TODO
}
// ============================================================================
// GroupManagerTask::go
// ============================================================================
void GroupManagerTask::go (size_t tmo_ms)
throw (Tango::DevFailed)
{
//- a timer
yat::Timer t;
//- be sure we have something to do...
if (! this->m_cfg.managed_devices.size())
{
THROW_DEVFAILED(_CPTC("DEVICE_ERROR"),
_CPTC("the managed devices list is empty - check associated property"),
_CPTC("GroupManagerTask::go"));
}
//- instanciate the dynamic attributes
try
{
//- populate the group of managed devices
this->m_grp.add(this->m_cfg.managed_devices);
//- remove excluded devices
if (this->m_cfg.excluded_devices.size())
this->m_grp.remove(this->m_cfg.excluded_devices);
DEBUG_STREAM << "GroupManagerTask::go::group contains "
<< this->m_grp.get_size()
<< " devices"
<< std::endl;
//- in order to be able to instanciate the proper dynamic attribute (in terms of data type),
//- we need to obtain the Tango::AttributeInfo of each monitored attribute. so...
Tango::AttributeInfoList ail;
this->get_attributes_info_i(ail);
//- can now instanciate the dynamic attributes...
std::vector<yat4tango::DynamicAttributeInfo> dai(ail.size());
for (size_t i = 0; i < ail.size(); i++)
{
//- the Tango device to which tha dynamic attribute will be attached to
dai[i].dev = this->m_cfg.host_device;
//- copy the Tango::AttributeInfo info
dai[i].tai = ail[i];
//- let's decide (for example purpose) that we want all dynamic attributes to be associated
//- to the same "generic" read and write callblack, except for the images, which will be
//- connected to a "specific" callback...
if (dai[i].tai.data_format != Tango::IMAGE)
{
dai[i].rcb = yat4tango::DynamicAttributeReadCallback::instanciate(*this,
&GroupManagerTask::generic_rd_cb);
//- instanciate the <write callback> (can be useless but not a problem)
dai[i].wcb = yat4tango::DynamicAttributeWriteCallback::instanciate(*this,
&GroupManagerTask::generic_wt_cb);
}
else
{
dai[i].rcb = yat4tango::DynamicAttributeReadCallback::instanciate(*this,
&GroupManagerTask::specific_rd_cb);
//- instanciate the <write callback> (can be useless but not a problem)
dai[i].wcb = yat4tango::DynamicAttributeWriteCallback::instanciate(*this,
&GroupManagerTask::specific_wt_cb);
}
}
//- add all dynamic attributes to the device interface
this->m_dam.add_attributes(dai);
//- add a forwarded attribute
this->m_dam.add_attribute("tgvbox:10000/sys/tg_test/1/boolean_scalar", "test_lan");
this->m_dam.add_attribute("sys/tg_test/1/uchar_scalar", "my_uchar_scalar");
this->m_dam.add_attribute("sys/tg_test/1/short_scalar");
this->m_dam.add_attribute("sys/tg_test/1/ushort_scalar");
this->m_dam.add_attribute("sys/tg_test/1/long_scalar");
this->m_dam.add_attribute("sys/tg_test/1/ulong_scalar");
this->m_dam.add_attribute("sys/tg_test/1/long64_scalar");
this->m_dam.add_attribute("sys/tg_test/1/ulong64_scalar");
this->m_dam.add_attribute("sys/tg_test/1/float_scalar");
this->m_dam.add_attribute("sys/tg_test/1/double_scalar");
this->m_dam.add_attribute("sys/tg_test/1/string_scalar");
this->m_dam.add_attribute("sys/tg_test/1/boolean_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/uchar_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/short_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/ushort_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/long_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/ulong_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/long64_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/ulong64_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/float_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/double_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/string_spectrum_ro");
this->m_dam.add_attribute("sys/tg_test/1/boolean_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/uchar_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/short_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/ushort_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/long_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/ulong_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/long64_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/ulong64_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/float_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/double_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/string_image_ro");
this->m_dam.add_attribute("sys/tg_test/1/uchar_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/short_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/ushort_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/long_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/float_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/double_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/string_spectrum");
this->m_dam.add_attribute("sys/tg_test/1/uchar_image");
this->m_dam.add_attribute("sys/tg_test/1/short_image");
this->m_dam.add_attribute("sys/tg_test/1/ushort_image");
this->m_dam.add_attribute("sys/tg_test/1/long_image");
this->m_dam.add_attribute("sys/tg_test/1/float_image");
this->m_dam.add_attribute("sys/tg_test/1/double_image");
this->m_dam.add_attribute("sys/tg_test/1/string_image");
//- add "State" to the monitored attributes list (minimum required)
this->add_state_to_monitored_attributes_list_i();
}
catch (Tango::DevFailed & df)
{
RETHROW_DEVFAILED(df,
_CPTC("DEVICE_ERROR"),
_CPTC("dynamic attributes initialization failed [Tango exception caught]"),
_CPTC("GroupManager::go"));
}
catch (...)
{
THROW_DEVFAILED(_CPTC("DEVICE_ERROR"),
_CPTC("dynamic attributes initialization failed [unknown exception caught]"),
_CPTC("GroupManager::go"));
}
//- finally call yat4tango::DeviceTask::go
long remaining_tmo = static_cast<long>(tmo_ms) - static_cast<long>(t.elapsed_msec());
this->yat4tango::DeviceTask::go(remaining_tmo > 1 ? remaining_tmo : 1);
}
// ============================================================================
// GroupManagerTask::exit
// ============================================================================
void GroupManagerTask::exit ()
throw (Tango::DevFailed)
{
//- stop the periodic activity
try
{
this->stop_periodic_activity(3000);
}
catch (...)
{
//- ignore errors cause we need to exit!
}
//- removes all dynamic attributes from de device interface
try
{
this->m_dam.remove_attributes();
}
catch (...)
{
//- ignore errors cause we need to exit!
}
//- finally call call yat4tango::DeviceTask::exit
this->yat4tango::DeviceTask::exit();
}
// ============================================================================
// GroupManagerTask::generic_rd_cb (yat4tango::DynamicAttributeReadCallback impl)
// ============================================================================
void GroupManagerTask::generic_rd_cb (yat4tango::DynamicAttributeReadCallbackData& d)
throw (Tango::DevFailed)
{
if (d.dya)
{
d.dya->dump();
}
}
// ============================================================================
// GroupManagerTask::specific_rd_cb (yat4tango::DynamicAttributeReadCallback impl)
// ============================================================================
void GroupManagerTask::specific_rd_cb (yat4tango::DynamicAttributeReadCallbackData& d)
throw (Tango::DevFailed)
{
if (d.dya)
{
d.dya->dump();
}
}
// ============================================================================
// GroupManagerTask::generic_wt_cb (yat4tango::DynamicAttributeWriteCallback impl)
// ============================================================================
void GroupManagerTask::generic_wt_cb (yat4tango::DynamicAttributeWriteCallbackData& d)
throw (Tango::DevFailed)
{
if (d.dya)
{
d.dya->dump();
}
}
// ============================================================================
// GroupManagerTask::specific_wt_cb (yat4tango::DynamicAttributeWriteCallback impl)
// ============================================================================
void GroupManagerTask::specific_wt_cb (yat4tango::DynamicAttributeWriteCallbackData& d)
throw (Tango::DevFailed)
{
if (d.dya)
{
d.dya->dump();
}
}
// ============================================================================
// GroupManagerTask::get_attributes_info_i
// ============================================================================
void GroupManagerTask::get_attributes_info_i (Tango::AttributeInfoList& attr_info)
throw (Tango::DevFailed)
{
//- this method tries to obtain the Tango::AttributeInfo of all monitored attributes.
//- in our case, the Tango::AttributeInfo is required to instanciate a dynamic attribute.
//- this is purely application specific cause, in this example, the dynamic attribute caracteristics
//- (format, data type, ...) are obtained from a "remote" attribute
//-
//- an exception is thrown in case:
//- -> none of the managed devices currently running (can't obtain Tango::AttributeInfo in this case)
//- -> (at least) one of managed devices doesn't support (at least) one of monitored attributes
//- -> attempt to obtain the Tango::AttributeInfoList failed on all managed devices
//- clear the returned vector
attr_info.clear();
//- get group size (i.e. num of devices in the group)
size_t g_size = this->m_grp.get_size();
//- get any running device from the group
//- then obtain its AttributeInfoList
Tango::DeviceProxy * dp = 0;
Tango::AttributeInfoList * ail = 0;
//- warning: Tango::Group index is in [1, num of devices in the group]
for (size_t i = 1; i <= g_size; i++)
{
dp = this->m_grp.get_device(i);
if (dp)
{
DEBUG_STREAM << "GroupManagerTask::get_attributes_info::trying "
<< dp->dev_name()
<< std::endl;
//- be sure the device is running
try {
dp->ping();
ail = dp->attribute_list_query();
break;
}
catch (...) {
delete ail;
}
}
}
if (! dp || ! ail)
{
ERROR_STREAM << "failed to obtain attributes description from one of the <managed device>" << std::endl;
ERROR_STREAM << "be sure that (at least) one of the managed devices is running" << std::endl;
ERROR_STREAM << "otherwise, check porperties providing the managed devices excluded devices list" << std::endl;
THROW_DEVFAILED(_CPTC("DEVICE_ERROR"),
_CPTC("failed to obtain attributes description from one of the <managed device>"),
_CPTC("GroupManagerTask::get_attributes_info_i"));
}
//- use <dp> as our "reference device" - must support all monitored attributes
for (size_t i = 0; i < this->m_cfg.monitored_attributes.size(); i++)
{
std::string & attr_name = this->m_cfg.monitored_attributes[i];
//- search <attr_name> in <ail>
bool attr_found = false;
size_t j = 0;
for (; j < ail->size(); j++)
{
if (attr_name == (*ail)[j].name)
{
attr_found = true;
break;
}
}
if (! attr_found)
{
delete ail;
attr_info.clear();
ERROR_STREAM << "(at least) one of the managed devices does support the monitored attribute <" << attr_name << ">" << std::endl;
ERROR_STREAM << "be sure that ALL managed devices support ALL monitored attributes" << std::endl;
ERROR_STREAM << "Tango attribute names are case sensistive - check the associated property" << std::endl;
THROW_DEVFAILED(_CPTC("DEVICE_ERROR"),
_CPTC("(at least) one of the managed devices does support one of the monitored attributes"),
_CPTC("GroupManagerTask::get_attributes_info"));
}
//- ok, ail[j] is monitoredAttributes[i]
DEBUG_STREAM << "GroupManagerTask::get_attributes_info::successfully obtained attribute info for "
<< attr_name
<< std::endl;
attr_info.push_back((*ail)[j]);
}
//- avoid memory leaks...
delete ail;
}
// ============================================================================
// GroupManagerTask::add_state_to_monitored_attributes_list_i
// ============================================================================
void GroupManagerTask::add_state_to_monitored_attributes_list_i ()
throw (Tango::DevFailed)
{
const std::string & state_attr_name = "State";
bool state_attr_found = false;
for (size_t i = 0; i < this->m_cfg.monitored_attributes.size(); i++)
{
const std::string & attr_name = this->m_cfg.monitored_attributes[i];
if (attr_name == state_attr_name)
{
state_attr_found = true;
break;
}
}
if (! state_attr_found)
this->m_cfg.monitored_attributes.push_back("State");
}
} // namespace
......@@ -52,19 +52,10 @@ public:
{
if (&src == this)
return *this;
managed_devices = src.managed_devices;
excluded_devices = src.excluded_devices;
monitored_attributes= src.monitored_attributes;
task_activity_period_ms = src.task_activity_period_ms;
host_device = src.host_device;
return *this;
}
//- managed devices
std::vector<std::string> managed_devices;
//- excluded devices
std::vector<std::string> excluded_devices;
//- monitored attributes
std::vector<std::string> monitored_attributes;
//- period of task's periodic activity in msecs
size_t task_activity_period_ms;
//- the Tango device hoting this task
......@@ -77,22 +68,6 @@ public:
//- dtor
virtual ~GroupManagerTask ();
//- specialization of yat4tango::DeviceTask::go
//---------------------------------------------------------------------------------------
//- TANGO THREADING CONSTRAINT: THIS METHOD IS EXPECTED TO BE CALLED FROM <INIT_DEVICE>
//- OR (AT LEAST) FROM A TANGO DEVICE THREAD (SEE THE TANGO DEVICE MONITOR FOR DETAILS)
//---------------------------------------------------------------------------------------
virtual void go (size_t tmo_ms = kDEFAULT_MSG_TMO_MSECS)
throw (Tango::DevFailed);
//- specialization of yat4tango::DeviceTask::exit
//---------------------------------------------------------------------------------------
//- TANGO THREADING CONSTRAINT: THIS METHOD IS EXPECTED TO BE CALLED FROM <INIT_DEVICE>
//- OR (AT LEAST) FROM A TANGO DEVICE THREAD (SEE THE TANGO DEVICE MONITOR FOR DETAILS)
//---------------------------------------------------------------------------------------
virtual void exit ()
throw (Tango::DevFailed);
//- starts task's periodic activity (a <tmo_ms> of 0 means asynchronous exec)
void start_periodic_activity (size_t tmo_ms = 0)
throw (Tango::DevFailed);
......@@ -115,42 +90,8 @@ private:
void periodic_job_i ()
throw (Tango::DevFailed);
//- if not already present, adds <State> at the end of the monitored attributes list
void add_state_to_monitored_attributes_list_i ()
throw (Tango::DevFailed);
//- this method tries to obtain the Tango::AttributeInfo of all monitored attributes
//- an exception is thrown in case:
//- -> none of the managed devices currently running (can't obtain Tango::AttributeInfo in this case)
//- -> (at least) one of managed devices doesn't support (at least) one of monitored attributes
//- -> attempt to obtain the Tango::AttributeInfoList failed on all managed devices
void get_attributes_info_i (Tango::AttributeInfoList& attr_info)
throw (Tango::DevFailed);
//- generic read callback for dynamic attributes (yat4tango example)
void generic_rd_cb (yat4tango::DynamicAttributeReadCallbackData& d)
throw (Tango::DevFailed);
//- generic write callback for dynamic attributes (yat4tango example)
void generic_wt_cb (yat4tango::DynamicAttributeWriteCallbackData& d)
throw (Tango::DevFailed);
//- a specific read callback for some of our dynamic attributes (yat4tango example)
void specific_rd_cb (yat4tango::DynamicAttributeReadCallbackData& d)
throw (Tango::DevFailed);
//- specific write callback for some of our dynamic attributes (yat4tango example)
void specific_wt_cb (yat4tango::DynamicAttributeWriteCallbackData& d)
throw (Tango::DevFailed);
//- the task's configuration
Config m_cfg;
//- the dynamic attribute manager
yat4tango::DynamicAttributeManager m_dam;
//- the managed Tango::Group
Tango::Group m_grp;
};
} // namespace GroupManager_ns
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment