File: scan.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (401 lines) | stat: -rw-r--r-- 11,336 bytes parent folder | download | duplicates (4)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// NOLINTBEGIN(*)

#include "old_common.h"

class SlowCallBack : public Tango::CallBack
{
    void push_event(Tango::EventData *);

  public:
    int cb_executed;
    int cb_err;
    int old_sec, old_usec;
    short val;
    AttrQuality qua;
};

class FastCallBack : public Tango::CallBack
{
    void push_event(Tango::EventData *);

  public:
    int cb_executed;
    int cb_err;
    int old_sec, old_usec;
    double val;
    AttrQuality qua;
};

void SlowCallBack::push_event(Tango::EventData *event_data)
{
    std::vector<short> value;
    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 << "SlowCallBack::push_event(): called attribute " << event_data->attr_name << " event "
                 << event_data->event << "\n";
        qua = event_data->attr_value->get_quality();
        if((!event_data->err) && (qua != ATTR_INVALID))
        {
            *(event_data->attr_value) >> value;
            val = value[0];
            TEST_LOG << "Callback value " << val << std::endl;
        }
        else
        {
            TEST_LOG << "Error send to callback" << std::endl;
            //            Tango::Except::print_error_stack(event_data->errors);
        }
    }
    catch(...)
    {
        TEST_LOG << "SlowCallBack::push_event(): could not extract data !\n";
    }
}

void FastCallBack::push_event(Tango::EventData *event_data)
{
    std::vector<double> value;
    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 << "FastCallBack::push_event(): called attribute " << event_data->attr_name << " event "
                 << event_data->event << "\n";
        qua = event_data->attr_value->get_quality();
        if((!event_data->err) && (qua != ATTR_INVALID))
        {
            *(event_data->attr_value) >> value;
            val = value[0];
            TEST_LOG << "Callback value " << val << std::endl;
        }
        else
        {
            TEST_LOG << "Error send to callback" << std::endl;
            //            Tango::Except::print_error_stack(event_data->errors);
        }
    }
    catch(...)
    {
        TEST_LOG << "FastCallBack::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("slow_actuator");

        //
        // Test set up (stop polling and clear abs_change and rel_change attribute
        // properties but restart device to take this into account)
        // Set the abs_change to 1
        //

        if(device->is_attribute_polled(att_name))
        {
            device->stop_poll_attribute(att_name);
        }
        {
            DbAttribute dba(att_name, device_name);
            DbData dbd;
            DbDatum a(att_name);
            a << (short) 2;
            dbd.push_back(a);
            dbd.push_back(DbDatum("abs_change"));
            dbd.push_back(DbDatum("rel_change"));
            dba.delete_property(dbd);

            dbd.clear();
            a << (short) 1;
            dbd.push_back(a);
            DbDatum ch("abs_change");
            ch << (short) 1;
            dbd.push_back(ch);
            dba.put_property(dbd);

            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));

        //
        // switch on the polling first!
        //
        device->poll_attribute(att_name, 250);

        //
        // 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);

        int poll_period = device->get_attribute_poll_period(att_name);
        TEST_LOG << "att polling period : " << poll_period << std::endl;
        assert(poll_period == 250);

        //
        // subscribe to a change event and filter only on a quality change
        //

        int eve_id;
        SlowCallBack cb;
        cb.cb_executed = 0;
        cb.cb_err = 0;
        cb.old_sec = cb.old_usec = 0;

        std::vector<std::string> filters;
        filters.push_back("$quality == 1");
        eve_id = device->subscribe_event(att_name, Tango::CHANGE_EVENT, &cb, filters);

        std::this_thread::sleep_for(std::chrono::milliseconds(250));

        //
        // Check that first point has been received
        //

        assert(cb.cb_executed == 1);
        assert(cb.qua == ATTR_VALID);
        TEST_LOG << "   (Slow actuator) first point received --> OK" << std::endl;
        //
        // Write a value into the actuator
        //

        short new_val = 10;
        DeviceAttribute da(att_name, new_val);

        device->write_attribute(da);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        //
        // Check that the event has been received
        //

        assert(cb.cb_executed == 2);
        assert(cb.qua == ATTR_CHANGING);
        TEST_LOG << "   (Slow actuator) Event quality from VALID->CHANGING --> OK" << std::endl;

        //
        // Read value from device which should have quality factor still set to
        // CHANGING
        //

        DeviceAttribute da_r;
        device->set_source(DEV);
        da_r = device->read_attribute(att_name);
        device->set_source(CACHE_DEV);

        assert(da_r.get_quality() == ATTR_CHANGING);
        TEST_LOG << "   (Slow actuator) Attribute quality still CHANGING after read --> OK" << std::endl;

        //
        // Wait for end of movement
        //

        std::this_thread::sleep_for(std::chrono::seconds(3));

        //
        // Check that the event has been received
        //

        assert(cb.cb_executed == 3);
        assert(cb.qua == ATTR_VALID);
        TEST_LOG << "   (Slow actuator) event quality from CHANGING->VALID --> OK" << std::endl;

        //
        // unsubscribe to the event
        //

        device->unsubscribe_event(eve_id);

        TEST_LOG << "   (Slow actuator) unsubscribe_event --> OK" << std::endl;

        //---------------------------------------------------------------------------------
        //
        //            Now, check a fast actuator scan
        //
        //---------------------------------------------------------------------------------

        att_name = "fast_actuator";

        //
        // Test set up (stop polling and clear abs_change and rel_change attribute
        // properties but restart device to take this into account)
        // Set the abs_change to 1
        //

        if(device->is_attribute_polled(att_name))
        {
            device->stop_poll_attribute(att_name);
        }
        {
            DbAttribute dba(att_name, device_name);
            DbData dbd;
            DbDatum a(att_name);
            a << (short) 2;
            dbd.push_back(a);
            dbd.push_back(DbDatum("abs_change"));
            dbd.push_back(DbDatum("rel_change"));
            dba.delete_property(dbd);

            dbd.clear();
            a << (short) 1;
            dbd.push_back(a);
            DbDatum ch("abs_change");
            ch << (short) 1;
            dbd.push_back(ch);
            dba.put_property(dbd);

            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));

        //
        // switch on the polling first!
        //
        device->poll_attribute(att_name, 250);

        //
        // Check that the attribute is now polled at 100 mS
        //
        po = device->is_attribute_polled(att_name);
        TEST_LOG << "attribute polled : " << po << std::endl;
        assert(po == true);

        poll_period = device->get_attribute_poll_period(att_name);
        TEST_LOG << "att polling period : " << poll_period << std::endl;
        assert(poll_period == 250);

        //
        // subscribe to a change event and filter only on a quality change
        //

        FastCallBack fcb;
        fcb.cb_executed = 0;
        fcb.cb_err = 0;
        fcb.old_sec = cb.old_usec = 0;

        std::vector<std::string> ffilters;
        ffilters.push_back("$quality == 1");
        eve_id = device->subscribe_event(att_name, Tango::CHANGE_EVENT, &fcb, ffilters);

        TEST_LOG << "   (Fast actuator) subscribe_event --> OK" << std::endl;

        std::this_thread::sleep_for(std::chrono::milliseconds(250));

        //
        // Check that first point has been received
        //

        assert(fcb.cb_executed == 1);
        assert(fcb.qua == ATTR_VALID);
        TEST_LOG << "   (Fast actuator) first point received --> OK" << std::endl;

        //
        // Write a value into the actuator
        //

        double val = 100.333;
        DeviceAttribute fda(att_name, val);

        device->write_attribute(fda);

        //  Only sleep a 100ms. The events should be fired immediately

        std::this_thread::sleep_for(std::chrono::milliseconds(250));

        //
        // Check that the events has been received
        //

        assert(fcb.cb_executed == 3);
        assert(fcb.qua == ATTR_VALID);
        TEST_LOG << "   (Fast actuator) Two quality events received --> OK" << std::endl;

        //
        // unsubscribe to the event
        //

        device->unsubscribe_event(eve_id);
        TEST_LOG << "   (Fast actuator) Unsubscribe_event --> OK" << std::endl;

        device->stop_poll_attribute(att_name);
        device->stop_poll_attribute("slow_actuator");
    }
    catch(Tango::DevFailed &e)
    {
        Except::print_exception(e);
        exit(-1);
    }
    catch(CORBA::Exception &ex)
    {
        Except::print_exception(ex);
        exit(-1);
    }

    delete device;

    return 0;
}

// NOLINTEND(*)