|
@@ -0,0 +1,335 @@
|
|
|
+//
|
|
|
+// Source code recreated from a .class file by IntelliJ IDEA
|
|
|
+// (powered by FernFlower decompiler)
|
|
|
+//
|
|
|
+
|
|
|
+package org.openscada.opc.lib.da;
|
|
|
+
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.Set;
|
|
|
+import org.jinterop.dcom.common.JIException;
|
|
|
+import org.openscada.opc.dcom.common.EventHandler;
|
|
|
+import org.openscada.opc.dcom.common.KeyedResult;
|
|
|
+import org.openscada.opc.dcom.common.KeyedResultSet;
|
|
|
+import org.openscada.opc.dcom.common.Result;
|
|
|
+import org.openscada.opc.dcom.common.ResultSet;
|
|
|
+import org.openscada.opc.dcom.da.IOPCDataCallback;
|
|
|
+import org.openscada.opc.dcom.da.OPCDATASOURCE;
|
|
|
+import org.openscada.opc.dcom.da.OPCITEMDEF;
|
|
|
+import org.openscada.opc.dcom.da.OPCITEMRESULT;
|
|
|
+import org.openscada.opc.dcom.da.OPCITEMSTATE;
|
|
|
+import org.openscada.opc.dcom.da.impl.OPCAsyncIO2;
|
|
|
+import org.openscada.opc.dcom.da.impl.OPCGroupStateMgt;
|
|
|
+import org.openscada.opc.dcom.da.impl.OPCItemMgt;
|
|
|
+import org.openscada.opc.dcom.da.impl.OPCSyncIO;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+public class Group {
|
|
|
+ private static Logger _log = LoggerFactory.getLogger(Group.class);
|
|
|
+ private static Random _random = new Random();
|
|
|
+ private Server _server = null;
|
|
|
+ private final int _serverHandle;
|
|
|
+ private OPCGroupStateMgt _group = null;
|
|
|
+ private OPCItemMgt _items = null;
|
|
|
+ private OPCSyncIO _syncIO = null;
|
|
|
+ private final Map<String, Integer> _itemHandleMap = new HashMap();
|
|
|
+ private final Map<Integer, Item> _itemMap = new HashMap();
|
|
|
+ private final Map<Integer, Item> _itemClientMap = new HashMap();
|
|
|
+
|
|
|
+ Group(Server server, int serverHandle, OPCGroupStateMgt group) throws IllegalArgumentException, UnknownHostException, JIException {
|
|
|
+ _log.debug("Creating new group instance with COM group " + group);
|
|
|
+ this._server = server;
|
|
|
+ this._serverHandle = serverHandle;
|
|
|
+ this._group = group;
|
|
|
+ this._items = group.getItemManagement();
|
|
|
+ this._syncIO = group.getSyncIO();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setActive(boolean state) throws JIException {
|
|
|
+ this._group.setState((Integer)null, state, (Integer)null, (Float)null, (Integer)null, (Integer)null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setActive(boolean state, int period) throws JIException {
|
|
|
+ this._group.setState(period, state, (Integer)null, (Float)null, (Integer)null, (Integer)null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void remove() throws JIException {
|
|
|
+ this._server.removeGroup(this, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isActive() throws JIException {
|
|
|
+ return this._group.getState().isActive();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getName() throws JIException {
|
|
|
+ return this._group.getState().getName();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setName(String name) throws JIException {
|
|
|
+ this._group.setName(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Item addItem(String item) throws JIException, AddFailedException {
|
|
|
+ Map<String, Item> items = this.addItems(item);
|
|
|
+ return (Item)items.get(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized Map<String, Result<OPCITEMRESULT>> validateItems(String... items) throws JIException {
|
|
|
+ OPCITEMDEF[] defs = new OPCITEMDEF[items.length];
|
|
|
+
|
|
|
+ for(int i = 0; i < items.length; ++i) {
|
|
|
+ defs[i] = new OPCITEMDEF();
|
|
|
+ defs[i].setItemID(items[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ KeyedResultSet<OPCITEMDEF, OPCITEMRESULT> result = this._items.validate(defs);
|
|
|
+ Map<String, Result<OPCITEMRESULT>> resultMap = new HashMap();
|
|
|
+ Iterator var6 = result.iterator();
|
|
|
+
|
|
|
+ while(var6.hasNext()) {
|
|
|
+ KeyedResult<OPCITEMDEF, OPCITEMRESULT> resultEntry = (KeyedResult)var6.next();
|
|
|
+ resultMap.put(((OPCITEMDEF)resultEntry.getKey()).getItemID(), new Result((OPCITEMRESULT)resultEntry.getValue(), resultEntry.getErrorCode()));
|
|
|
+ }
|
|
|
+
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized Map<String, Item> addItems(String... items) throws JIException, AddFailedException {
|
|
|
+ Map<String, Integer> handles = this.findItems(items);
|
|
|
+ List<Integer> foundItems = new ArrayList(items.length);
|
|
|
+ List<String> missingItems = new ArrayList();
|
|
|
+ Iterator var6 = handles.entrySet().iterator();
|
|
|
+
|
|
|
+ while(var6.hasNext()) {
|
|
|
+ Map.Entry<String, Integer> entry = (Map.Entry)var6.next();
|
|
|
+ if (entry.getValue() == null) {
|
|
|
+ missingItems.add((String)entry.getKey());
|
|
|
+ } else {
|
|
|
+ foundItems.add((Integer)entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<Integer> newClientHandles = new HashSet();
|
|
|
+ OPCITEMDEF[] itemDef = new OPCITEMDEF[missingItems.size()];
|
|
|
+
|
|
|
+ for(int i = 0; i < missingItems.size(); ++i) {
|
|
|
+ OPCITEMDEF def = new OPCITEMDEF();
|
|
|
+ def.setItemID((String)missingItems.get(i));
|
|
|
+ def.setActive(true);
|
|
|
+
|
|
|
+ Integer clientHandle;
|
|
|
+ do {
|
|
|
+ clientHandle = _random.nextInt();
|
|
|
+ } while(this._itemClientMap.containsKey(clientHandle) || newClientHandles.contains(clientHandle));
|
|
|
+
|
|
|
+ newClientHandles.add(clientHandle);
|
|
|
+ def.setClientHandle(clientHandle);
|
|
|
+ itemDef[i] = def;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Integer> failedItems = new HashMap();
|
|
|
+ KeyedResultSet<OPCITEMDEF, OPCITEMRESULT> result = this._items.add(itemDef);
|
|
|
+ int i = 0;
|
|
|
+
|
|
|
+ for(Iterator var11 = result.iterator(); var11.hasNext(); ++i) {
|
|
|
+ KeyedResult<OPCITEMDEF, OPCITEMRESULT> entry = (KeyedResult)var11.next();
|
|
|
+ if (entry.getErrorCode() == 0) {
|
|
|
+ Item item = new Item(this, ((OPCITEMRESULT)entry.getValue()).getServerHandle(), itemDef[i].getClientHandle(), ((OPCITEMDEF)entry.getKey()).getItemID());
|
|
|
+ this.addItem(item);
|
|
|
+ foundItems.add(item.getServerHandle());
|
|
|
+ } else {
|
|
|
+ failedItems.put(((OPCITEMDEF)entry.getKey()).getItemID(), entry.getErrorCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (failedItems.size() != 0) {
|
|
|
+ throw new AddFailedException(failedItems, this.findItems((Collection)foundItems));
|
|
|
+ } else {
|
|
|
+ return this.findItems((Collection)foundItems);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void addItem(Item item) {
|
|
|
+ _log.debug(String.format("Adding item: '%s', %d", item.getId(), item.getServerHandle()));
|
|
|
+ this._itemHandleMap.put(item.getId(), item.getServerHandle());
|
|
|
+ this._itemMap.put(item.getServerHandle(), item);
|
|
|
+ this._itemClientMap.put(item.getClientHandle(), item);
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void removeItem(Item item) {
|
|
|
+ this._itemHandleMap.remove(item.getId());
|
|
|
+ this._itemMap.remove(item.getServerHandle());
|
|
|
+ this._itemClientMap.remove(item.getClientHandle());
|
|
|
+ }
|
|
|
+
|
|
|
+ protected Item getItemByOPCItemId(String opcItemId) {
|
|
|
+ Integer serverHandle = (Integer)this._itemHandleMap.get(opcItemId);
|
|
|
+ if (serverHandle == null) {
|
|
|
+ _log.debug(String.format("Failed to locate item with id '%s'", opcItemId));
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ _log.debug(String.format("Item '%s' has server id '%d'", opcItemId, serverHandle));
|
|
|
+ return (Item)this._itemMap.get(serverHandle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized Map<String, Integer> findItems(String[] items) {
|
|
|
+ Map<String, Integer> data = new HashMap();
|
|
|
+
|
|
|
+ for(int i = 0; i < items.length; ++i) {
|
|
|
+ data.put(items[i], (Integer)this._itemHandleMap.get(items[i]));
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized Map<String, Item> findItems(Collection<Integer> handles) {
|
|
|
+ Map<String, Item> itemMap = new HashMap();
|
|
|
+ Iterator var4 = handles.iterator();
|
|
|
+
|
|
|
+ while(var4.hasNext()) {
|
|
|
+ Integer i = (Integer)var4.next();
|
|
|
+ Item item = (Item)this._itemMap.get(i);
|
|
|
+ if (item != null) {
|
|
|
+ itemMap.put(item.getId(), item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return itemMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void checkItems(Item[] items) {
|
|
|
+ Item[] var5 = items;
|
|
|
+ int var4 = items.length;
|
|
|
+
|
|
|
+ for(int var3 = 0; var3 < var4; ++var3) {
|
|
|
+ Item item = var5[var3];
|
|
|
+ if (item.getGroup() != this) {
|
|
|
+ throw new IllegalArgumentException("Item does not belong to this group");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setActive(boolean state, Item... items) throws JIException {
|
|
|
+ this.checkItems(items);
|
|
|
+ Integer[] handles = new Integer[items.length];
|
|
|
+
|
|
|
+ for(int i = 0; i < items.length; ++i) {
|
|
|
+ handles[i] = items[i].getServerHandle();
|
|
|
+ }
|
|
|
+
|
|
|
+ this._items.setActiveState(state, handles);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected Integer[] getServerHandles(Item[] items) {
|
|
|
+ this.checkItems(items);
|
|
|
+ Integer[] handles = new Integer[items.length];
|
|
|
+
|
|
|
+ for(int i = 0; i < items.length; ++i) {
|
|
|
+ handles[i] = items[i].getServerHandle();
|
|
|
+ }
|
|
|
+
|
|
|
+ return handles;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized Map<Item, Integer> write(WriteRequest... requests) throws JIException {
|
|
|
+ Item[] items = new Item[requests.length];
|
|
|
+
|
|
|
+ for(int i = 0; i < requests.length; ++i) {
|
|
|
+ items[i] = requests[i].getItem();
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer[] handles = this.getServerHandles(items);
|
|
|
+ org.openscada.opc.dcom.da.WriteRequest[] wr = new org.openscada.opc.dcom.da.WriteRequest[items.length];
|
|
|
+
|
|
|
+ for(int i = 0; i < items.length; ++i) {
|
|
|
+ wr[i] = new org.openscada.opc.dcom.da.WriteRequest(handles[i], requests[i].getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ ResultSet<org.openscada.opc.dcom.da.WriteRequest> resultSet = this._syncIO.write(wr);
|
|
|
+ Map<Item, Integer> result = new HashMap();
|
|
|
+
|
|
|
+ for(int i = 0; i < requests.length; ++i) {
|
|
|
+ Result<org.openscada.opc.dcom.da.WriteRequest> entry = (Result)resultSet.get(i);
|
|
|
+ result.put(requests[i].getItem(), entry.getErrorCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized Map<Item, ItemState> read(boolean device, Item... items) throws JIException {
|
|
|
+ Integer[] handles = this.getServerHandles(items);
|
|
|
+ KeyedResultSet<Integer, OPCITEMSTATE> states = this._syncIO.read(device ? OPCDATASOURCE.OPC_DS_DEVICE : OPCDATASOURCE.OPC_DS_CACHE, handles);
|
|
|
+ Map<Item, ItemState> data = new HashMap();
|
|
|
+ Iterator var7 = states.iterator();
|
|
|
+
|
|
|
+ while(var7.hasNext()) {
|
|
|
+ KeyedResult<Integer, OPCITEMSTATE> entry = (KeyedResult)var7.next();
|
|
|
+ Item item = (Item)this._itemMap.get(entry.getKey());
|
|
|
+ ItemState state = new ItemState(entry.getErrorCode(), ((OPCITEMSTATE)entry.getValue()).getValue(), ((OPCITEMSTATE)entry.getValue()).getTimestamp().asCalendar(), ((OPCITEMSTATE)entry.getValue()).getQuality());
|
|
|
+ data.put(item, state);
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Server getServer() {
|
|
|
+ return this._server;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized void clear() throws JIException {
|
|
|
+ Integer[] handles = (Integer[])this._itemMap.keySet().toArray(new Integer[0]);
|
|
|
+
|
|
|
+ try {
|
|
|
+ this._items.remove(handles);
|
|
|
+ } finally {
|
|
|
+ this._itemHandleMap.clear();
|
|
|
+ this._itemMap.clear();
|
|
|
+ this._itemClientMap.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized OPCAsyncIO2 getAsyncIO20() {
|
|
|
+ return this._group.getAsyncIO2();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setGroupState(Integer requestedUpdateRate, Integer timeBias, Float percentDeadband) throws JIException {
|
|
|
+ this._group.setState(requestedUpdateRate, (Boolean)null, timeBias, percentDeadband, (Integer)null, (Integer)null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized EventHandler attach(IOPCDataCallback dataCallback) throws JIException {
|
|
|
+ return this._group.attach(dataCallback);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Item findItemByClientHandle(int clientHandle) {
|
|
|
+ return (Item)this._itemClientMap.get(clientHandle);
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getServerHandle() {
|
|
|
+ return this._serverHandle;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized void removeItem(String opcItemId) throws IllegalArgumentException, UnknownHostException, JIException {
|
|
|
+ _log.debug(String.format("Removing item '%s'", opcItemId));
|
|
|
+ Item item = this.getItemByOPCItemId(opcItemId);
|
|
|
+ if (item != null) {
|
|
|
+ this._group.getItemManagement().remove(new Integer[]{item.getServerHandle()});
|
|
|
+ this.removeItem(item);
|
|
|
+ _log.debug(String.format("Removed item '%s'", opcItemId));
|
|
|
+ } else {
|
|
|
+ _log.warn(String.format("Unable to find item '%s'", opcItemId));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|