source: branches/FACT++_lidctrl_usb/scripts/tests_n_examples/example_Access_of_a_service.py

Last change on this file was 14887, checked in by neise, 12 years ago
I think I moved the tests and examples into its own folder, in order to keep the scripts folder tidy
File size: 2.9 KB
Line 
1'use strict';
2
3// Subscribe to the the services (returns a handle to each of them)
4var w = new Subscription("MAGIC_WEATHER/DATA");
5var x = new Subscription("TNG_WEATHER/DUST");
6var y = new Subscription("TNG_WEATHER/CLIENT_LIST");
7
8// Name which corresponds to handle
9console.out(w.name);
10
11// Wait until a valid service object is in the internal buffer
12while (!w.get(-1))
13 v8.sleep(100);
14
15// Make sure that the service description for this service is available
16// This allows to access the service values by name (access by index
17// is always possible)
18while (!w.get().obj)
19 v8.sleep(100);
20
21console.out("have data");
22
23// get the current service data
24var d = w.get();
25
26// Here is a summary:
27// d.obj===undefined: no data received yet
28// d.obj!==undefined, d.obj.length==0: valid names are available, received data empty (d.data===null)
29// obj!==undefined, obj.length>0: valid names are available, data received
30//
31// d.data===undefined: no data received yet
32// d.data===null: event received, but contains no data
33// d.data.length>0: event received, contains data
34
35console.out("Format: "+d.format); // Dim format string
36console.out("Counter: "+d.counter); // How many service object have been received so far?
37console.out("Time: "+d.time); // Which time is attached to the data?
38console.out("QoS: "+d.qos); // Quality-of-Service parameter
39console.out("Length: "+d.data.length); // Number of entries in data array
40console.out("Data: "+d.data); // Print array
41
42// Or to plot the whole contents, you can do
43console.out(JSON.stringify(d));
44console.out(JSON.stringify(d.data));
45console.out(JSON.stringify(d.obj));
46
47// Loop over all service properties by name
48for (var name in d.obj)
49{
50 console.out("obj." + name + "=" + d.obj[name]);
51}
52
53// Loop over all service properties by index
54for (var i=0; i<d.data.length; i++)
55{
56 console.out("data["+ i +"]="+ d.data[i]);
57}
58
59// Note that in case of formats like F:160, the entries in data
60// might be arrays themselves
61
62var cnt = d.counter;
63
64// Print counter and time
65console.out("Time: "+d.counter+" - "+d.time);
66
67// Wait until at least one new event has been received
68while (cnt==d.counter)
69{
70 v8.sleep(1000);
71 d = w.get();
72}
73
74// Print counter and time of new event (usually counter+1, but this is
75// not guranteed) We can have missed service objects
76console.out("Time: "+d.counter+" - "+d.time);
77
78// Access the Wind property of the weather data
79console.out("Wind: "+d.obj.v);
80console.out("Wind: "+d.obj['v']);
81
82// Get the dust and client_list
83var xx = x.get();
84var yy = y.get();
85
86// data contains only a single value. No array indexing required
87console.out("Dust: "+xx.data);
88console.out("CL: "+yy.data);
89
90// Service is still open
91console.out(w.isOpen);
92
93// Close the subscription (unsubscribe from the dim-service)
94// Tells you if the service was still subscribed or not
95var rc = w.close();
96
97// Service is not subscribed anymore
98console.out(w.isOpen);
99console.out(rc)
100
101
102rc = x.close();
103console.out(rc)
104rc = y.close();
105console.out(rc)
106
107
Note: See TracBrowser for help on using the repository browser.