Rest API for Historian data
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / HistorySamplerItem2.java
1 package org.simantics.history;
2
3 import java.util.Arrays;
4
5 import org.simantics.databoard.accessor.StreamAccessor;
6 import org.simantics.databoard.accessor.error.AccessorException;
7 import org.simantics.history.util.StreamIterator;
8
9 public class HistorySamplerItem2 implements Comparable<HistorySamplerItem2> {
10
11         public static class LevelItem implements Comparable<LevelItem> {
12                 public final String id;
13                 public final double samplingInterval;
14
15                 public LevelItem(String id, double samplingInterval) {
16                         this.id = id;
17                         this.samplingInterval = samplingInterval;
18                 }
19
20                 @Override
21                 public int compareTo(LevelItem o) {
22                         int i = id.compareTo(o.id);
23                         return i != 0 ? i : Double.compare(samplingInterval, o.samplingInterval);
24                 }
25         }
26
27         Collector collector;
28         HistoryManager history;         // History source for this item
29         LevelItem[] items;
30
31         // State data
32         StreamAccessor accessor;                        // Stream accessor
33         public StreamIterator iter;
34         public int chartDataId;
35
36         public HistorySamplerItem2(Collector collector, HistoryManager history, LevelItem[] items, int identityHashCode) {
37                 if (items.length == 0)
38                         throw new IllegalArgumentException("Must have at least one existing history item to sample, zero provided");
39                 this.collector = collector;
40                 this.history = history;
41                 this.items = items;
42                 this.chartDataId = identityHashCode;
43         }
44         
45         public void flush() throws HistoryException {
46                 if (collector != null)
47                         collector.flush();
48         }
49
50         public void open() throws HistoryException {
51                 accessor = history.openStream(items[0].id, "r");
52                 iter = new StreamIterator( accessor );
53         }
54
55         public void open(double pixelsPerSecond) throws HistoryException {
56                 LevelItem f = getFormat(pixelsPerSecond);
57                 accessor = history.openStream(f.id, "r");
58                 iter = new StreamIterator( accessor );
59         }
60
61         public void close() {
62                 if (accessor!=null) {
63                         try {
64                                 accessor.close();
65                         } catch (AccessorException e) {
66                         }
67                 }
68                 accessor = null;
69                 iter = null;
70         }
71
72         @Override
73         public int compareTo(HistorySamplerItem2 o) {
74                 int m = Math.min(items.length, o.items.length);
75                 for (int j = 0; j < m; ++j) {
76                         int i = items[j].compareTo(o.items[j]);
77                         if (i != 0)
78                                 return i;
79                 }
80                 return 0;
81         }
82
83         @Override
84         public int hashCode() {
85                 int code = 0x2304;
86                 code = 13*code + Arrays.hashCode(items);
87                 code = 13*code + history.hashCode();
88                 return code;
89         }
90
91         @Override
92         public boolean equals(Object obj) {
93                 if ( obj == null ) return false;
94                 if ( obj instanceof HistorySamplerItem2 == false ) return false;
95                 HistorySamplerItem2 other = (HistorySamplerItem2) obj;
96                 if ( !other.history.equals(history) ) return false;
97                 if ( !Arrays.equals(other.items, items) ) return false;
98                 return true;
99         }
100
101         private LevelItem getFormat(double secondsPerPixel) throws HistoryException {
102                 LevelItem result = null;
103                 for (LevelItem format : items) {
104                         double interval = format.samplingInterval;
105                         if (Double.isNaN( interval ) || interval <= secondsPerPixel) {
106                                 result = format;
107                         } else {
108                                 break;
109                         }
110                 }
111                 return result;
112         }
113
114 }