]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.history/src/org/simantics/history/impl/CollectorImpl.java
Add more databoard reading utilities to ByteFileReader
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / impl / CollectorImpl.java
index 4f916868ecda2c4c2c4282b7e9b609c1071e37b5..c202f733ca6af88c284351de3cfadea3eee9899d 100644 (file)
@@ -87,6 +87,15 @@ public class CollectorImpl implements Collector {
        
        /** True when all items are open */
        boolean itemsOpen = true;
+
+       /** True when history collection will write data to disk */
+       boolean enabled = true;
+
+       /**
+        * Transient info only set in {@link #beginStep(NumberBinding, Object)} based on
+        * {@link #enabled}
+        */
+       private transient boolean stepEnabled = true;
        
        public FlushPolicy flushPolicy = FlushPolicy.FlushOnEveryStep;
        
@@ -122,7 +131,16 @@ public class CollectorImpl implements Collector {
                this.history = history;
                this.state = (CollectorState) previousState.clone();
        }
-       
+
+       public void setEnabled(boolean enabled) {
+               this.enabled = enabled;
+       }
+
+       @Override
+       public boolean isEnabled() {
+               return enabled;
+       }
+
        @Override
        public synchronized void addItem(Bean item) throws HistoryException {
                try {
@@ -494,13 +512,20 @@ public class CollectorImpl implements Collector {
        
        @Override
        public void beginStep(NumberBinding binding, Object time) throws HistoryException {
-               openAllItems();
+               stepEnabled = enabled;
                updateDt(binding, time);
                state.time.setValue(binding, time);
+               if (enabled) {
+                       openAllItems();
+               }
        }
 
        @Override
        public void setValue(String id, Binding binding, Object value) throws HistoryException {
+               // Short-circuit calls when collection not enabled
+               if (!stepEnabled)
+                       return;
+
                VariableState vs = state.values.get( id );
                if (vs == null) {
                        vs = new VariableState();
@@ -554,6 +579,8 @@ public class CollectorImpl implements Collector {
 
        @Override
        public void endStep() throws HistoryException {
+               if (!stepEnabled)
+                       return;
                // Write values to streams
                for (ActiveStream i : items)
                {
@@ -811,66 +838,79 @@ public class CollectorImpl implements Collector {
                
                boolean condition = makeNewBand || (!i.hasEndTime && i.itemState.count==2); 
 
-               if(i.stream.size() > 0 && !i.itemState.ooDeadband) {
+               if (i.stream.size() > 0) {
 
                        /// Extend existing band
                        // Extend endTime value
-                       if (!i.hasEndTime) {
-                               i.current.setTime( time.getBinding(), time.getValue() );
-                       } else {
-                               i.current.setEndTime( time.getBinding(), time.getValue() );
-                       }
 
-                       if (isValidValue) {
-                               // Set last value
-                               if (i.current.hasLastValue()) {
-                                       i.current.setLastValue( value.getBinding(), value.getValue() );
+                       // This ensures that a value band's end time isn't the same
+                       // as the next band's start time which makes it possible to
+                       // know what are the timestamps between which the item value
+                       // changed from a to b. This allows us to more correctly render
+                       // steps and sloped lines accoring to the actual data.
+                       if (!i.itemState.ooDeadband || isMinMaxFormat) {
+                               if (!i.hasEndTime) {
+                                       i.current.setTime( time.getBinding(), time.getValue() );
+                               } else {
+                                       i.current.setEndTime( time.getBinding(), time.getValue() );
                                }
+                       }
 
-                               // Add sum
-                               if (i.current.hasAvg() && i.isNumeric && !isNanValue && !restep) {
-                                       double duration = (currentTime - i.itemState.firstTime);
-                                       if(duration < 1e-9) {
-                                               i.current.setAvg( Bindings.DOUBLE, 0.0 );                                               
-                                       } else {
-                                               i.current.setAvg( Bindings.DOUBLE, i.itemState.sum / duration);                                         
+                       if (i.itemState.ooDeadband || isMinMaxFormat) {
+                               if (isValidValue) {
+                                       // Set last value
+                                       if (i.current.hasLastValue()) {
+                                               i.current.setLastValue( value.getBinding(), value.getValue() );
                                        }
-                               }
 
-                               // Update min-max
-                               if (i.current.hasMin() && i.isNumeric && !isNanValue) {
-                                       Binding minBinding = i.current.getMinBinding();
-                                       Object prevMinValue = i.current.getMin();
-                                       Object currentValueWithMinBinding = value.getValue( minBinding );
-                                       int diff = minBinding.compare( prevMinValue, currentValueWithMinBinding );
-                                       if (diff>0) i.current.setMin( minBinding, currentValueWithMinBinding );
-                               }                               
-                               if (i.current.hasMax() && i.isNumeric && !isNanValue) {
-                                       Binding maxBinding = i.current.getMaxBinding();
-                                       Object prevMaxValue = i.current.getMax();
-                                       Object currentValueWithMaxBinding = value.getValue( maxBinding );
-                                       int diff = maxBinding.compare( prevMaxValue, currentValueWithMaxBinding );
-                                       if (diff<0) i.current.setMax( maxBinding, currentValueWithMaxBinding );
-                               }
+                                       if (i.isNumeric && !isNanValue) {
+                                               // Add sum
+                                               if (i.current.hasAvg() && !restep) {
+                                                       double duration = (currentTime - i.itemState.firstTime);
+                                                       if(duration < 1e-9) {
+                                                               i.current.setAvg( Bindings.DOUBLE, 0.0 );
+                                                       } else {
+                                                               i.current.setAvg( Bindings.DOUBLE, i.itemState.sum / duration);
+                                                       }
+                                               }
 
-                               // Update median
-                               if (i.current.hasMedian() && i.isNumeric && !isNanValue) {
-                                       Double median = i.itemState.median.getMedian();
-                                       i.current.setMedian( Bindings.DOUBLE, median);
-                               }                               
-                       } else {
-                               i.current.setValueNull();
-                       }
+                                               // Update min-max
+                                               if (i.current.hasMin()) {
+                                                       Binding minBinding = i.current.getMinBinding();
+                                                       Object prevMinValue = i.current.getMin();
+                                                       Object currentValueWithMinBinding = value.getValue( minBinding );
+                                                       int diff = minBinding.compare( prevMinValue, currentValueWithMinBinding );
+                                                       if (diff>0) i.current.setMin( minBinding, currentValueWithMinBinding );
+                                               }
+                                               if (i.current.hasMax()) {
+                                                       Binding maxBinding = i.current.getMaxBinding();
+                                                       Object prevMaxValue = i.current.getMax();
+                                                       Object currentValueWithMaxBinding = value.getValue( maxBinding );
+                                                       int diff = maxBinding.compare( prevMaxValue, currentValueWithMaxBinding );
+                                                       if (diff<0) i.current.setMax( maxBinding, currentValueWithMaxBinding );
+                                               }
 
-                       // Add count
-                       if (i.current.hasCount()) {
-                               i.current.setCount( i.itemState.count );
+                                               // Update median
+                                               if (i.current.hasMedian()) {
+                                                       Double median = i.itemState.median.getMedian();
+                                                       i.current.setMedian( Bindings.DOUBLE, median);
+                                               }
+                                       }
+                               } else {
+                                       i.current.setValueNull();
+                               }
+
+                               // Update count
+                               if (i.current.hasCount()) {
+                                       i.current.setCount( i.itemState.count );
+                               }
                        }
 
                        // Write entry to file stream
                        //              if (i.current.getTimeDouble()<0) 
                        //                      System.err.println("Error sample "+i.current.getBinding().toString(i.current.getSample()));
                        i.stream.setNoflush( i.stream.size()-1, i.binding, i.current.getSample() );                             
+                       //System.err.println(i.itemState.id + " update " + i.current.getSample());
                }
                
                if (condition) {
@@ -923,6 +963,7 @@ public class CollectorImpl implements Collector {
                        
                        // Add the band to the file stream
                        i.stream.addNoflush( i.binding, i.current.getSample() );
+                       //System.err.println(i.itemState.id + " add " + i.current.getSample());
                        
                }