| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | // Subscribe to the the services (returns a handle to each of them)
|
|---|
| 4 | var w = new Subscription("MAGIC_WEATHER/DATA");
|
|---|
| 5 | var x = new Subscription("TNG_WEATHER/DUST");
|
|---|
| 6 | var y = new Subscription("TNG_WEATHER/CLIENT_LIST");
|
|---|
| 7 |
|
|---|
| 8 | // Name which corresponds to handle
|
|---|
| 9 | console.out(w.name);
|
|---|
| 10 |
|
|---|
| 11 | // Wait until a valid service object is in the internal buffer
|
|---|
| 12 | while (!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)
|
|---|
| 18 | while (!w.get().obj)
|
|---|
| 19 | v8.sleep(100);
|
|---|
| 20 |
|
|---|
| 21 | console.out("have data");
|
|---|
| 22 |
|
|---|
| 23 | // get the current service data
|
|---|
| 24 | var 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 |
|
|---|
| 35 | console.out("Format: "+d.format); // Dim format string
|
|---|
| 36 | console.out("Counter: "+d.counter); // How many service object have been received so far?
|
|---|
| 37 | console.out("Time: "+d.time); // Which time is attached to the data?
|
|---|
| 38 | console.out("QoS: "+d.qos); // Quality-of-Service parameter
|
|---|
| 39 | console.out("Length: "+d.data.length); // Number of entries in data array
|
|---|
| 40 | console.out("Data: "+d.data); // Print array
|
|---|
| 41 |
|
|---|
| 42 | // Or to plot the whole contents, you can do
|
|---|
| 43 | console.out(JSON.stringify(d));
|
|---|
| 44 | console.out(JSON.stringify(d.data));
|
|---|
| 45 | console.out(JSON.stringify(d.obj));
|
|---|
| 46 |
|
|---|
| 47 | // Loop over all service properties by name
|
|---|
| 48 | for (var name in d.obj)
|
|---|
| 49 | {
|
|---|
| 50 | console.out("obj." + name + "=" + d.obj[name]);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | // Loop over all service properties by index
|
|---|
| 54 | for (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 |
|
|---|
| 62 | var cnt = d.counter;
|
|---|
| 63 |
|
|---|
| 64 | // Print counter and time
|
|---|
| 65 | console.out("Time: "+d.counter+" - "+d.time);
|
|---|
| 66 |
|
|---|
| 67 | // Wait until at least one new event has been received
|
|---|
| 68 | while (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
|
|---|
| 76 | console.out("Time: "+d.counter+" - "+d.time);
|
|---|
| 77 |
|
|---|
| 78 | // Access the Wind property of the weather data
|
|---|
| 79 | console.out("Wind: "+d.obj.v);
|
|---|
| 80 | console.out("Wind: "+d.obj['v']);
|
|---|
| 81 |
|
|---|
| 82 | // Get the dust and client_list
|
|---|
| 83 | var xx = x.get();
|
|---|
| 84 | var yy = y.get();
|
|---|
| 85 |
|
|---|
| 86 | // data contains only a single value. No array indexing required
|
|---|
| 87 | console.out("Dust: "+xx.data);
|
|---|
| 88 | console.out("CL: "+yy.data);
|
|---|
| 89 |
|
|---|
| 90 | // Service is still open
|
|---|
| 91 | console.out(w.isOpen);
|
|---|
| 92 |
|
|---|
| 93 | // Close the subscription (unsubscribe from the dim-service)
|
|---|
| 94 | // Tells you if the service was still subscribed or not
|
|---|
| 95 | var rc = w.close();
|
|---|
| 96 |
|
|---|
| 97 | // Service is not subscribed anymore
|
|---|
| 98 | console.out(w.isOpen);
|
|---|
| 99 | console.out(rc)
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | rc = x.close();
|
|---|
| 103 | console.out(rc)
|
|---|
| 104 | rc = y.close();
|
|---|
| 105 | console.out(rc)
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|