|
@@ -1,6 +1,8 @@
|
|
|
package com.platform.opc.util;
|
|
|
|
|
|
import com.platform.common.util.RedisUtils;
|
|
|
+import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
+import com.platform.dao.vo.SysUserVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jinterop.dcom.common.JIErrorCodes;
|
|
|
import org.jinterop.dcom.common.JIException;
|
|
@@ -8,11 +10,15 @@ import org.jinterop.dcom.core.JIVariant;
|
|
|
import org.openscada.opc.dcom.da.OPCSERVERSTATE;
|
|
|
import org.openscada.opc.lib.common.AlreadyConnectedException;
|
|
|
import org.openscada.opc.lib.common.ConnectionInformation;
|
|
|
+import org.openscada.opc.lib.common.NotConnectedException;
|
|
|
import org.openscada.opc.lib.da.*;
|
|
|
+import org.openscada.opc.lib.da.browser.FlatBrowser;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.Executors;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @Auther: 熊
|
|
@@ -27,9 +33,11 @@ public class OpcDAClient {
|
|
|
public static String clsId = "001AAAA6-FB54-4627-84B2-8777379E5868";
|
|
|
public static String progId = "Hollysys.HOLLiASiComm.1";
|
|
|
public static String tag_prefix = "Channel1.Device1.";
|
|
|
- private Server server;
|
|
|
- private Group group = null;
|
|
|
- private Map<String, Item> groupItems = null;
|
|
|
+ private static Server server;
|
|
|
+ private static List<Group> groupList = new ArrayList<>();
|
|
|
+ private static List<Map<String, Item>> groupItemsList = new ArrayList<>();
|
|
|
+ private static List<Set> itemSetList = new ArrayList<>();
|
|
|
+ private static Item[][] itemArrList = new Item[][]{};
|
|
|
|
|
|
/**
|
|
|
* 初始化连接信息
|
|
@@ -40,7 +48,7 @@ public class OpcDAClient {
|
|
|
/**
|
|
|
* 创建连接
|
|
|
*/
|
|
|
- public void connect() {
|
|
|
+ public static void connect() {
|
|
|
if (server != null && server.getServerState() != null && server.getServerState().getServerState().equals(OPCSERVERSTATE.OPC_STATUS_RUNNING)) {
|
|
|
return;
|
|
|
}
|
|
@@ -75,6 +83,27 @@ public class OpcDAClient {
|
|
|
log.info("OPC Server connect success...");
|
|
|
}
|
|
|
|
|
|
+ public static List<String> findAllItem() {
|
|
|
+ FlatBrowser flatBrowser = server.getFlatBrowser();
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ if (flatBrowser == null) {
|
|
|
+ log.warn("读取空");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ Collection<String> collection = flatBrowser.browse();
|
|
|
+ for (String item : collection) {
|
|
|
+ log.info("find item: " + item);
|
|
|
+ list.add(item);
|
|
|
+ }
|
|
|
+ } catch (JIException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ log.error("host主机IP错误 = " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据地址获取数据
|
|
|
*
|
|
@@ -93,13 +122,69 @@ public class OpcDAClient {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 建组:根据车间分组建立
|
|
|
+ *
|
|
|
+ * @param listMap
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static AddFailedException addGroupList(Map<Integer, List<RemoteOpc>> listMap) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ try {
|
|
|
+ if (CollectionUtils.isEmpty(groupItemsList) || CollectionUtils.isEmpty(groupList)) {
|
|
|
+ log.info("开始建组...");
|
|
|
+ int i = 0;
|
|
|
+ for (Map.Entry<Integer, List<RemoteOpc>> entry : listMap.entrySet()) {
|
|
|
+ Group group = server.addGroup(entry.getKey()+"");
|
|
|
+ List<RemoteOpc> list = entry.getValue();
|
|
|
+ List<String> itemIdList = list.stream().distinct().map(t->{
|
|
|
+ if(t.getAvFlag()==1){
|
|
|
+ t.setPositionNum(t.getPositionNum()+"_AV");
|
|
|
+ }else{
|
|
|
+ t.setPositionNum(t.getPositionNum()+"_DV");
|
|
|
+ }
|
|
|
+ return t;
|
|
|
+ }).map(RemoteOpc::getPositionNum).collect(Collectors.toList());
|
|
|
+ log.info("itemIdList: " + itemIdList.size());
|
|
|
+ String[] items = itemIdList.toArray(new String[]{});
|
|
|
+ log.info("items: " + items.length);
|
|
|
+ Map<String, Item> groupItems = group.addItems(items);
|
|
|
+ Set itemSet = new HashSet(groupItems.values());
|
|
|
+ Item[] itemArr = new Item[itemSet.size()];
|
|
|
+ itemSet.toArray(itemArr);
|
|
|
+ groupList.add(group);
|
|
|
+ groupItemsList.add(groupItems);
|
|
|
+ itemSetList.add(itemSet);
|
|
|
+ itemArrList[i++] = itemArr;
|
|
|
+ }
|
|
|
+ log.info("组建完成,开始查询数据...,分组数量:" + i);
|
|
|
+ }
|
|
|
+ } catch (AddFailedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ // 将不存在的点位信息保存到数据库,
|
|
|
+ Map<String, Integer> errorsItemMap = e.getErrors();
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("批量获取数据异常:", e);
|
|
|
+ return e;
|
|
|
+ } catch (DuplicateGroupException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NotConnectedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (JIException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取多组数据
|
|
|
*
|
|
|
* @param itemIdList
|
|
|
* @return
|
|
|
*/
|
|
|
- public Map<String, Object> getItemValues(List<String> itemIdList) {
|
|
|
+ /* public Map<String, Object> (List<String> itemIdList) {
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
try {
|
|
|
if (groupItems == null || group == null) {
|
|
@@ -123,6 +208,35 @@ public class OpcDAClient {
|
|
|
log.error("批量获取数据异常:", e);
|
|
|
}
|
|
|
return result;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取多组数据
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Map<String, Object>> getItemValuesList() {
|
|
|
+ List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ if (!CollectionUtils.isEmpty(groupItemsList)) {
|
|
|
+ for (int i = 0; i < groupItemsList.size(); i++) {
|
|
|
+ Map<Item, ItemState> resultMap = groupList.get(i).read(true, itemArrList[i]);
|
|
|
+ log.info("数据获取完成:{}条", resultMap.size());
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ for (Item item : resultMap.keySet()) {
|
|
|
+ ItemState itemMap = resultMap.get(item);
|
|
|
+ Object value = getVal(itemMap.getValue());
|
|
|
+ result.put(item.getId(), value);
|
|
|
+ log.info("id: " + item.getId() + ", value: " + value);
|
|
|
+ }
|
|
|
+ resultList.add(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("批量获取数据异常:", e);
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -219,11 +333,11 @@ public class OpcDAClient {
|
|
|
for (String tag : tagList) {
|
|
|
itemList.add(tag_prefix + tag);
|
|
|
}
|
|
|
- Map<String, Object> stringObjectMap = opcDAClient.getItemValues(itemList);
|
|
|
+ /*Map<String, Object> stringObjectMap = opcDAClient.(itemList);
|
|
|
for (String key : stringObjectMap.keySet()) {
|
|
|
Object itemMap = stringObjectMap.get(key);
|
|
|
System.out.println(key + " : " + itemMap);
|
|
|
- }
|
|
|
+ }*/
|
|
|
}
|
|
|
}
|
|
|
|