1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// NOLINTBEGIN(*)
#include <thread>
#include <chrono>
#include "old_common.h"
class EventCallBack : public Tango::CallBack
{
void push_event(Tango::EventData *);
public:
int cb_executed;
int cb_err;
int old_sec, old_usec;
DevState sta;
std::string status;
};
void EventCallBack::push_event(Tango::EventData *event_data)
{
struct timeval now_timeval = Tango::make_timeval(std::chrono::system_clock::now());
TEST_LOG << "date : tv_sec = " << now_timeval.tv_sec;
TEST_LOG << ", tv_usec = " << now_timeval.tv_usec << std::endl;
auto delta_msec = ((now_timeval.tv_sec - old_sec) * 1000) + ((now_timeval.tv_usec - old_usec) / 1000);
old_sec = now_timeval.tv_sec;
old_usec = now_timeval.tv_usec;
TEST_LOG << "delta_msec = " << delta_msec << std::endl;
cb_executed++;
try
{
TEST_LOG << "StateEventCallBack::push_event(): called attribute " << event_data->attr_name << " event "
<< event_data->event << "\n";
if(!event_data->err)
{
std::string::size_type pos = event_data->attr_name.find_last_of('/');
pos++;
std::string att_name = event_data->attr_name.substr(pos);
if(att_name == "state")
{
*(event_data->attr_value) >> sta;
TEST_LOG << "State in callback: " << DevStateName[sta] << std::endl;
}
else if(att_name == "status")
{
*(event_data->attr_value) >> status;
TEST_LOG << "Status in callback: " << status << std::endl;
}
else
{
TEST_LOG << "Received callback for an unknown attribute : " << event_data->attr_name << std::endl;
}
}
else
{
TEST_LOG << "Error send to callback" << std::endl;
}
}
catch(...)
{
cb_err++;
TEST_LOG << "EventCallBack::push_event(): could not extract data !\n";
}
}
int main(int argc, char **argv)
{
DeviceProxy *device;
if(argc == 1)
{
TEST_LOG << "usage: %s device" << std::endl;
exit(-1);
}
std::string device_name = argv[1];
try
{
device = new DeviceProxy(device_name);
}
catch(CORBA::Exception &e)
{
Except::print_exception(e);
exit(1);
}
TEST_LOG << std::endl << "new DeviceProxy(" << device->name() << ") returned" << std::endl << std::endl;
try
{
std::string att_name("State");
//
// Test set up (stop polling)
//
if(device->is_attribute_polled(att_name))
{
device->stop_poll_attribute(att_name);
}
DeviceProxy adm_dev(device->adm_name().c_str());
DeviceData di;
di << device_name;
adm_dev.command_inout("DevRestart", di);
delete device;
device = new DeviceProxy(device_name);
std::this_thread::sleep_for(std::chrono::seconds(1));
//
// subscribe to a state change event
//
int eve_id;
std::vector<std::string> filters;
EventCallBack cb;
cb.cb_executed = 0;
cb.cb_err = 0;
cb.old_sec = cb.old_usec = 0;
// start the polling first!
device->poll_attribute(att_name, 1000);
eve_id = device->subscribe_event(att_name, Tango::CHANGE_EVENT, &cb, filters);
//
// Check that the attribute is now polled at 1000 mS
//
bool po = device->is_attribute_polled(att_name);
TEST_LOG << "attribute polled : " << po << std::endl;
assert(po == true);
po = device->is_command_polled(att_name);
assert(po == true);
int poll_period = device->get_attribute_poll_period(att_name);
TEST_LOG << "att polling period : " << poll_period << std::endl;
assert(poll_period == 1000);
TEST_LOG << " subscribe_event on state attribute --> OK" << std::endl;
//
// Check that first point has been received
//
assert(cb.cb_executed == 1);
assert(cb.sta == Tango::ON);
assert(cb.cb_err == 0);
TEST_LOG << " first point received --> OK" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
//
// Change the device state and check that an event has been received
//
DeviceData dd;
DevState d_state = Tango::OFF;
dd << d_state;
device->command_inout("IOState", dd);
std::this_thread::sleep_for(std::chrono::seconds(1));
d_state = Tango::ON;
dd << d_state;
device->command_inout("IOState", dd);
std::this_thread::sleep_for(std::chrono::seconds(2));
assert(cb.cb_executed == 4);
assert(cb.cb_err == 0);
assert(cb.sta == Tango::ON);
TEST_LOG << " Event on state change --> OK" << std::endl;
//
// Execute command manually push a change event on state
//
device->command_inout("PushStateStatusChangeEvent");
std::this_thread::sleep_for(std::chrono::seconds(1));
assert(cb.cb_executed >= 4);
assert(cb.cb_err == 0);
assert(cb.sta == Tango::ON);
TEST_LOG << " Event on state manually pushed --> OK" << std::endl;
//
// unsubscribe to the event
//
device->unsubscribe_event(eve_id);
//
// Stop polling
//
device->stop_poll_attribute(att_name);
TEST_LOG << " unsubscribe_event --> OK" << std::endl;
//
// Clear data in callback object and subscribe to status event
//
cb.cb_executed = 0;
att_name = "status";
// start the polling first!
device->poll_attribute(att_name, 1000);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
eve_id = device->subscribe_event(att_name, Tango::CHANGE_EVENT, &cb, filters);
TEST_LOG << " subscribe_event on status attribute --> OK" << std::endl;
//
// Check that first point has been received
//
assert(cb.cb_executed == 1);
assert(cb.cb_err == 0);
TEST_LOG << " first point received --> OK" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
//
// Change the device state and check that an event has been received
// Changing the device state obviously change the device status
//
d_state = Tango::OFF;
dd << d_state;
device->command_inout("IOState", dd);
std::this_thread::sleep_for(std::chrono::seconds(1));
d_state = Tango::ON;
dd << d_state;
device->command_inout("IOState", dd);
std::this_thread::sleep_for(std::chrono::seconds(2));
std::string::size_type pos = cb.status.find("ON");
assert(cb.cb_executed == 4);
assert(pos != std::string::npos);
assert(cb.cb_err == 0);
TEST_LOG << " Event on status change --> OK" << std::endl;
//
// Execute command manually push a change event on status
//
device->command_inout("PushStateStatusChangeEvent");
std::this_thread::sleep_for(std::chrono::seconds(1));
assert(cb.cb_executed >= 4);
assert(cb.cb_err == 0);
TEST_LOG << " Event on status manually pushed --> OK" << std::endl;
//
// unsubscribe to the event
//
device->unsubscribe_event(eve_id);
TEST_LOG << " unsubscribe_event --> OK" << std::endl;
//
// Stop polling
//
device->stop_poll_attribute(att_name);
}
catch(Tango::DevFailed &e)
{
Except::print_exception(e);
exit(-1);
}
catch(CORBA::Exception &ex)
{
Except::print_exception(ex);
exit(-1);
}
delete device;
Tango::ApiUtil::cleanup();
return 0;
}
// NOLINTEND(*)
|