]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / util / subscription / SubscriptionItem.java
diff --git a/bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java b/bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java
new file mode 100644 (file)
index 0000000..584749a
--- /dev/null
@@ -0,0 +1,221 @@
+/*******************************************************************************\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.util.subscription;\r
+\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+\r
+import org.simantics.databoard.annotations.Identifier;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.Bean;\r
+\r
+/**\r
+ * (Utility Class) Item format for HistoryManager, Collector, and meta-data for Simantics subscription configurations.\r
+ * \r
+ * The items objects the HistoryManager and Collector uses are complete meta-data\r
+ * descriptions. Different formats are supported and the content is written to disc\r
+ * completely as is. \r
+ * \r
+ * The "id" is composed with the following aggregation: [subscriptionId] [variableId] [formatId].\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class SubscriptionItem extends Bean {\r
+\r
+       /**\r
+        * Create HistoryItem descriptions for collecting one variable with multiple\r
+        * sampling formats.\r
+        * \r
+        * @param item\r
+        *            the item to use as a template for the created sampled items\r
+        * @param groupItemId\r
+        *            the group item id to use with\r
+        *            {@link #composeItemName(String, String, String)}\r
+        * @param groupId\r
+        *            the group id to use with\r
+        *            {@link #composeItemName(String, String, String)}\r
+        * @param formats\r
+        *            the sampling formats to create\r
+        * @return an array of history items\r
+        */\r
+       public static SubscriptionItem[] createItems(\r
+                       SubscriptionItem item,\r
+                       String groupItemId,\r
+                       String groupId,\r
+                       Collection<SamplingFormat> formats) {\r
+               Iterator<SamplingFormat> itr = formats.iterator();\r
+               SubscriptionItem[] items = new SubscriptionItem[ formats.size() ];\r
+               for (int i=0; i<formats.size(); i++) {\r
+                       items[i] = createItem( item, groupItemId, groupId, itr.next() );\r
+               }\r
+               return items;\r
+       }\r
+\r
+       /**\r
+        * Create HistoryItem descriptions for collecting one variable with multiple\r
+        * sampling formats.\r
+        * \r
+        * @param variableId\r
+        * @param subscriptionId\r
+        * @param formats\r
+        * @return an array of history items\r
+        */\r
+       public static SubscriptionItem[] createItems(\r
+                       String variableId,\r
+                       String subscriptionId,\r
+                       SamplingFormat...formats\r
+                               ) {\r
+                       SubscriptionItem[] items = new SubscriptionItem[ formats.length ];\r
+                       for (int i=0; i<formats.length; i++) {\r
+                               items[i] = createItem( variableId, subscriptionId, formats[i] );\r
+                       }\r
+                       return items;\r
+               }\r
+               \r
+       /**\r
+        * Create HistoryItem descriptions for collecting one variable with multiple\r
+        * sampling formats.\r
+        * \r
+        * @param groupItemId\r
+        * @param groupId\r
+        * @param formats\r
+        * @return an array of history items\r
+        */\r
+       public static SubscriptionItem[] createItems(\r
+                       String groupItemId,\r
+                       String groupId,\r
+                       Collection<SamplingFormat> formats) {\r
+                       Iterator<SamplingFormat> itr = formats.iterator();\r
+                       SubscriptionItem[] items = new SubscriptionItem[ formats.size() ];\r
+                       for (int i=0; i<formats.size(); i++) {\r
+                               items[i] = createItem( groupItemId, groupId, itr.next() );\r
+                       }\r
+                       return items;\r
+               }\r
+\r
+       /**\r
+        * Create HistoryItem description for collecting one variable with one\r
+        * sampling format.\r
+        * \r
+        * @param item\r
+        *            the subscription item to clone values from\r
+        * @param groupItemId\r
+        *            the group item id to use with\r
+        *            {@link #composeItemName(String, String, String)}\r
+        * @param groupId\r
+        *            the group id to use with\r
+        *            {@link #composeItemName(String, String, String)}\r
+        * @param format\r
+        * @return HistoryItem description.\r
+        */\r
+       public static SubscriptionItem createItem(\r
+                       SubscriptionItem item,\r
+                       String groupItemId,\r
+                       String groupId,\r
+                       SamplingFormat format\r
+                       ) {\r
+               SubscriptionItem hi = new SubscriptionItem();\r
+               hi.variableId = item.variableId;\r
+               hi.groupItemId = item.groupItemId;\r
+               hi.id = composeItemName(groupId, groupItemId, format.formatId);\r
+               hi.deadband = format.deadband;\r
+               hi.interval = format.interval;\r
+               hi.gain = item.gain;\r
+               hi.bias = item.bias;\r
+               hi.formatId = format.formatId;\r
+               hi.format = format.format;\r
+               hi.groupId = item.groupId;\r
+               hi.enabled = item.enabled;\r
+               return hi;\r
+       }\r
+\r
+       /**\r
+        * Create HistoryItem description for collecting one variable with one\r
+        * sampling format. \r
+        * \r
+        * @param groupItemId\r
+        *            the group item id to use with\r
+        *            {@link #composeItemName(String, String, String)}\r
+        * @param groupId\r
+        *            the group id to use with\r
+        *            {@link #composeItemName(String, String, String)}\r
+        * @param format\r
+        * @return HistoryItem description.\r
+        */\r
+       public static SubscriptionItem createItem(\r
+                       String groupItemId,\r
+                       String groupId,\r
+                       SamplingFormat format\r
+                       ) {\r
+               SubscriptionItem hi = new SubscriptionItem();\r
+               hi.variableId = groupItemId;\r
+               hi.groupItemId = groupItemId;\r
+               hi.id = composeItemName(groupId, groupItemId, format.formatId);\r
+               hi.deadband = format.deadband;\r
+               hi.interval = format.interval;\r
+               hi.formatId = format.formatId;\r
+               hi.format = format.format;\r
+               hi.groupId = groupId;\r
+               hi.enabled = true;\r
+               return hi;\r
+       }\r
+\r
+       public static String composeItemName(String groupId, String groupItemId, String formatId)\r
+       {\r
+               return groupId+" "+groupItemId+" "+formatId;\r
+       }\r
+\r
+       /** Item identifier in the HistoryManager */\r
+       public @Identifier String id;\r
+               \r
+       /** Variable Id, Reference of variable in Datasource. This field is used by Collector */\r
+       public String variableId;\r
+       \r
+       /** \r
+        * Describes the format of the packed sample. The sample must be a record.\r
+        * The record must have any combination of the following named fields.\r
+        * The field types must one of: byte, integer, long, float, double.\r
+        * \r
+        * time, endTime, value - are mandatory fields.\r
+        * \r
+        *  time      -  Region start time, the time of the 1st sample included into the band\r
+        *  endTime   -  Region end time, the time of the last sample included into the band\r
+        *  \r
+        *  value     -  First value in the band\r
+        *  lastValue -  Last value in the band\r
+        *  avg       -  Average value of all included samples\r
+        *  median    -  Median value of all samples in the band\r
+        *  min       -  Lowest value in the band\r
+        *  max       -  Highest value in the band\r
+        *  \r
+        *  quality   -  0 = Good, -1 = No value\r
+        *  count     -  The number of included samples in the band\r
+        */\r
+       public Datatype format;\r
+                       \r
+       // Subscription parameters\r
+       public double deadband = Double.NaN;\r
+       public double interval = Double.NaN;\r
+       public double gain = 1.0;\r
+       public double bias = 0.0;\r
+       public boolean enabled = true;\r
+\r
+       /** Identifier of group or subscription, used by Simantics */\r
+       public String groupId = "";\r
+       \r
+       /** Identifier of the item in the group, used by Simantics */\r
+       public String groupItemId = "";\r
+       \r
+       /** Identifier of the sample format description, used by Simantics */\r
+       public String formatId = "";\r
+\r
+}\r