+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.history.impl;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.annotations.Identifier;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.MutableVariant;\r
+import org.simantics.databoard.binding.reflection.BindingRequest;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.Bean;\r
+import org.simantics.history.util.MedianBindingProvider;\r
+import org.simantics.history.util.WeightedMedian;\r
+\r
+/**\r
+ * This is the state that s persisted.\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class CollectorState extends Bean {\r
+\r
+ public static final Binding BINDING = Bindings.getBindingUnchecked( CollectorState.class );\r
+ public static final Binding BINDING_ITEM = Bindings.getBindingUnchecked( CollectorState.Item.class );\r
+ \r
+ public @Identifier String id;\r
+ \r
+ /** Last known time value */\r
+ public MutableVariant time;\r
+\r
+ /** Time stepped between the two last collector steps. Used for time stamp epsilon calculations. */\r
+ public double dT = 1.0;\r
+\r
+ /** Last known values for each variable */\r
+ public Map<String, VariableState> values;\r
+ \r
+ /** Item states - corresponds to subscription.items */\r
+ public Map<String, Item> itemStates;\r
+\r
+ public static class VariableState {\r
+ \r
+ /** The actual value, type is Void is no value */\r
+ public MutableVariant value;\r
+ \r
+ /** Set to true if is valid value */\r
+ public boolean isValid; \r
+ \r
+ /** True, if double or float and is NaN */\r
+ public boolean isNan;\r
+ }\r
+ \r
+ public static class Item extends Bean {\r
+ \r
+ // Subscription info\r
+ /** History item reference */\r
+ public @Identifier String id;\r
+ /** Datasource reference */\r
+ public String variableId;\r
+ /** Dataformat */\r
+ public Datatype format;\r
+ /** Deadband */\r
+ public double deadband = Double.NaN;\r
+ /** Interval */\r
+ public double interval = Double.NaN;\r
+ /** Gain */\r
+ public double gain = 1.0;\r
+ /** Bias */\r
+ public double bias = 0.0;\r
+ /** Enabled */\r
+ public boolean enabled = true;\r
+ /** Format id, group id */\r
+ public String groupId = "", groupItemId = "", formatId = "";\r
+ \r
+ // - Valueband \r
+ /** Time of the first sample in the value band. NAN, if there is no value */\r
+ public double firstTime = Double.NaN;\r
+ /** Value of the first sample in the value band. null, if there is no value */\r
+ public MutableVariant firstValue;\r
+ /** Time of the prev added sample. NAN, if there is no value */\r
+ public double currentTime;\r
+ /** Value of the prev added sample. null, if there is no value */\r
+ public MutableVariant currentValue;\r
+ /** Current value is NaN */\r
+ public boolean isNaN;\r
+ /** Current value is valid */\r
+ public boolean isValid;\r
+ /** Time weighted sum (area), NAN, if not accumulated */\r
+ public double sum;\r
+ /** Sample Count of acquired samples in current band */\r
+ public int count;\r
+ /** Out of dead band, set true if a sample has crossed the deadband region in this value band */\r
+ public boolean ooDeadband = false;\r
+ /** First time after disabled */\r
+ public double firstDisabledTime;\r
+ /** Last time while disabled*/\r
+ public double lastDisabledTime;\r
+ /** Is item currently disabled */\r
+ public boolean isDisabled;\r
+ \r
+ /** Median values of the value band */\r
+ public WeightedMedian median;\r
+ \r
+ /**\r
+ * Interval is the time between two samples, a step length. \r
+ * Distribution contains classification of their variation. It is of \r
+ * interest if the datasource contains varying sizes and a mipmap interval \r
+ * needs to be decided. Null, if not breakdown is not constructed. \r
+ */ \r
+ //public @Optional ClassDistribution intervalDistribution;\r
+ \r
+ /**\r
+ * Value distribution contains clasification of values. \r
+ */\r
+ //public @Optional ClassDistribution valueDistribution;\r
+\r
+ }\r
+ \r
+ public CollectorState() {\r
+ super(BINDING);\r
+ }\r
+ \r
+ static {\r
+ Bindings.classBindingFactory.addFactory( MedianBindingProvider.INSTANCE );\r
+ \r
+ try {\r
+ BindingRequest br = new BindingRequest( CollectorState.Item.class ); \r
+ Binding datatypeBinding = Bindings.getBinding( br );\r
+ Bindings.typeClassFactory.getRepository().put(datatypeBinding.type(), br); \r
+ \r
+ br = new BindingRequest( CollectorState.class ); \r
+ datatypeBinding = Bindings.getBinding( br );\r
+ Bindings.typeClassFactory.getRepository().put(datatypeBinding.type(), br);\r
+ \r
+ } catch (BindingConstructionException e) {\r
+ System.err.println(e);\r
+ }\r
+ }\r
+ \r
+}\r