2 Collector is an utility that writes to history the data that is read from a data source.
3 Collector is instantiated to write to a specific history.
5 Collector collector = History.createCollector(history, FlushPolicy.NoFlush);
8 Collector reads its configuration from collector items.
9 As convenience, due to overlapping fields, the collector item configuration is often passed with same instance as with history item. There are two classes that are suitable for configuring both history and collector: HistoryAndCollectorItem and SubscriptionItem. User provided classes can also be used as long as they contain the minimum set of fields (See HistoryAndCollectorItem).
12 // Create collector item
13 HistoryAndCollectorItem ci = new HistoryAndCollectorItem();
14 ci.id = "history-item-0";
15 ci.format = sampleFormat;
20 ci.variableId = "MyDatasourceVariable";
22 // Initialize Collector (Item)
23 collector.addItem( ci );
26 The data is written in steps. If flush policy was FlushOnEveryStep then disc buffers are flushed
31 for ( int i = 0; i<100; i++ ) {
33 // Our "simulation" results
34 double time = i * 0.1;
35 double myDatasourceVariable = -20 + i*0.1;
37 // Begin collector writing step
38 collector.beginStep(Bindings.DOUBLE, time);
41 collector.setValue("MyDatasourceVariable", Bindings.DOUBLE, myDatasourceVariable);
54 When collector is closed, the initial parameters and state data are written to and persisted in the history.
55 A field called "collectorState" is created, if did not exist before-hand.
58 If there is interval or deadband in the collector, the collector records the data in banded mode (start and end time).
59 But in case the sample format doesn't support banded mode, then the collector writes the data in a way where it puts a sample at the start of a band, and another at the end of the band.
61 For example if datasource produces value every 0.1 second and interval value was 1.0 seconds; then there would be sample
62 at 0.0, 0.9 (to represent a band), and then a new sample at 1.0 to represent a start of a new band.
64 ==How to collect with multiple subscription parameters==
65 The collector is often used for recording one variable to multiple history items with different collecting parameters; such as interval and deadband. As the user passes collector items to the collector, the user gives and "id" and "variableId" values. "id" determines history item where the data is written to, and "variableId" where associated from in the Collector#setValue() method.
69 /// Configure items for the history and collector
70 // Create a sample format
71 RecordType sampleFormat = new RecordType();
72 sampleFormat.addComponent("time", Datatypes.DOUBLE );
73 sampleFormat.addComponent("value", Datatypes.DOUBLE );
74 sampleFormat.addComponent("quality", Datatypes.BYTE );
76 // Create two item configurations for the history and the collector
77 HistoryAndCollectorItem i1 = new HistoryAndCollectorItem();
78 i1.id = "MyVariable-interval=0.5";
79 i1.format = sampleFormat;
81 i1.variableId = "MyVariable";
83 HistoryAndCollectorItem i2 = new HistoryAndCollectorItem();
84 i2.id = "MyVariable-deadband=5";
85 i2.format = sampleFormat;
87 i2.variableId = "MyVariable";
89 /// Create History Manager and add Items
90 HistoryManager history = History.createMemoryHistory();
94 /// Create Collector and initialize items
95 Collector collector = History.createCollector(history, FlushPolicy.NoFlush);
96 collector.addItem( i1 );
97 collector.addItem( i2 );
101 // Simulate 100 steps
102 for ( int i = 0; i<100; i++ ) {
104 // Our "simulation" results
105 double time = i * 0.1;
106 double myItemValue = -20 + i*0.1;
108 // Begin collector writing step
109 collector.beginStep(Bindings.DOUBLE, time);
111 // Enter values for one variable.
112 collector.setValue("MyVariable", Bindings.DOUBLE, myItemValue);
114 // End collector step - MyVariable is written to two HistoryItems
121 // Write the collector state back to the history