|
@@ -1,10 +1,14 @@
|
|
|
package com.platform.opc.util;
|
|
|
|
|
|
+import com.platform.common.constant.RedisKeyConstants;
|
|
|
import com.platform.common.util.BigDecimalUtil;
|
|
|
import com.platform.common.util.DateUtils;
|
|
|
+import com.platform.common.util.RedisUtils;
|
|
|
import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
+import com.platform.dao.mapper.remote.RemoteOpcMapper;
|
|
|
import com.platform.dao.vo.query.remote.RemoteOpcVO;
|
|
|
import com.platform.opc.entity.OpcResult;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jinterop.dcom.common.JIErrorCodes;
|
|
|
import org.jinterop.dcom.common.JIException;
|
|
@@ -15,6 +19,7 @@ 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.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
@@ -127,77 +132,104 @@ public class OpcDAClient {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 建立分组*
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<RemoteOpc> addGroupAndItems(List<RemoteOpc> remoteOpcList) {
|
|
|
+ log.info("remoteOpcList: " + remoteOpcList.size());
|
|
|
+ Map<String, List<RemoteOpc>> listMap = remoteOpcList.stream().collect(Collectors.groupingBy(RemoteOpc::getLine));
|
|
|
+ OpcDAClient.connect();
|
|
|
+ List<RemoteOpc> list = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, List<RemoteOpc>> entry : listMap.entrySet()) {
|
|
|
+ list.addAll(addGroup(entry));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 建组:根据车间分组建立
|
|
|
*
|
|
|
- * @param listMap
|
|
|
+ * @param entry
|
|
|
* @return
|
|
|
*/
|
|
|
- public static AddFailedException addGroupList(Map<String, List<RemoteOpc>> listMap) {
|
|
|
+ public static List<RemoteOpc> addGroup(Map.Entry<String, List<RemoteOpc>> entry) {
|
|
|
+ boolean find = false;
|
|
|
+ Group group = null;
|
|
|
try {
|
|
|
- log.info("开始建组,分组数量:" + listMap.size());
|
|
|
- for (Map.Entry<String, List<RemoteOpc>> entry : listMap.entrySet()) {
|
|
|
- Group group = null;
|
|
|
- if (!CollectionUtils.isEmpty(groupList)) {
|
|
|
- for (Group checkGroup : groupList) {
|
|
|
- if (checkGroup.getName().equals(entry.getKey())) {
|
|
|
- log.info("已存在分组line id: " + entry.getKey());
|
|
|
- group = checkGroup;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (group == null) {
|
|
|
- log.info("追加分组line id: " + entry.getKey());
|
|
|
- group = server.addGroup(entry.getKey());
|
|
|
- groupList.add(group);
|
|
|
-
|
|
|
+ if (!CollectionUtils.isEmpty(groupList)) {
|
|
|
+ for (Group checkGroup : groupList) {
|
|
|
+ if (checkGroup.getName().equals(entry.getKey())) {
|
|
|
+ log.info("已存在分组line id: " + entry.getKey());
|
|
|
+ group = checkGroup;
|
|
|
+ find = true;
|
|
|
+ break;
|
|
|
}
|
|
|
- } else {
|
|
|
- log.info("groupList为空,新增分组line id: " + entry.getKey());
|
|
|
- group = server.addGroup(entry.getKey());
|
|
|
- groupList.add(group);
|
|
|
}
|
|
|
+ if (group == null) {
|
|
|
+ log.info("追加分组line id: " + entry.getKey());
|
|
|
+ group = server.addGroup(entry.getKey());
|
|
|
+ // groupList.add(group);
|
|
|
|
|
|
- List<RemoteOpc> list = entry.getValue();
|
|
|
- List<String> itemIdList = list.stream().distinct().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);
|
|
|
-
|
|
|
- List<Item> arrayList = new ArrayList<>(Arrays.asList(itemArr));
|
|
|
- List<Item> oldList = itemArrList.get(entry.getKey() + "");
|
|
|
- if (CollectionUtils.isEmpty(oldList)) {// 第一次追加,新增即可
|
|
|
- itemArrList.put(entry.getKey() + "", arrayList);
|
|
|
- } else {// 追加
|
|
|
- oldList.addAll(arrayList);
|
|
|
- itemArrList.put(entry.getKey() + "", oldList);
|
|
|
}
|
|
|
- log.info("组建完成,开始查询数据...,组key:" + group.getName() + ", 组数量:" + itemArrList.get(entry.getKey() + "").size());
|
|
|
+ } else {
|
|
|
+ log.info("groupList为空,新增分组line id: " + entry.getKey());
|
|
|
+ group = server.addGroup(entry.getKey());
|
|
|
+ // groupList.add(group);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<RemoteOpc> list = entry.getValue();
|
|
|
+ List<String> itemIdList = list.stream().distinct().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);
|
|
|
+ // 添加分组不能在之前group.addItems添加,因为group.addItems会报错,导致添加进去,但是没数据
|
|
|
+ if (!find) {
|
|
|
+ groupList.add(group);
|
|
|
+ }
|
|
|
+ List<Item> arrayList = new ArrayList<>(Arrays.asList(itemArr));
|
|
|
+ List<Item> oldList = itemArrList.get(entry.getKey() + "");
|
|
|
+ if (CollectionUtils.isEmpty(oldList)) {// 第一次追加,新增即可
|
|
|
+ itemArrList.put(entry.getKey() + "", arrayList);
|
|
|
+ } else {// 追加
|
|
|
+ oldList.addAll(arrayList);
|
|
|
+ itemArrList.put(entry.getKey() + "", oldList);
|
|
|
}
|
|
|
+ log.info("组建完成,开始查询数据...,组key:" + group.getName() + ", 组数量:" + itemArrList.get(entry.getKey() + "").size());
|
|
|
+
|
|
|
} catch (AddFailedException e) {
|
|
|
// e.printStackTrace();
|
|
|
// 将不存在的点位信息保存到数据库,
|
|
|
Map<String, Integer> errorsItemMap = e.getErrors();
|
|
|
// e.printStackTrace();
|
|
|
+ // 添加group,group也创建了
|
|
|
log.error("添加点位出错,有不存在的点位,等待下次启动添加");
|
|
|
- /*for (Group checkGroup : groupList) {
|
|
|
- try {
|
|
|
- server.removeGroup(checkGroup, true);
|
|
|
- } catch (JIException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- log.error("清除分组报错,等待下次启动添加");
|
|
|
- }
|
|
|
+ //log.info("items: " + items.length);
|
|
|
+ Map<String, Item> groupItems = e.getItems();
|
|
|
+ Set itemSet = new HashSet(groupItems.values());
|
|
|
+ Item[] itemArr = new Item[itemSet.size()];
|
|
|
+ itemSet.toArray(itemArr);
|
|
|
+ // 添加分组不能在之前group.addItems添加,因为group.addItems会报错,导致添加进去,但是没数据
|
|
|
+ if (!find) {
|
|
|
+ groupList.add(group);
|
|
|
+ }
|
|
|
+ List<Item> arrayList = new ArrayList<>(Arrays.asList(itemArr));
|
|
|
+ List<Item> oldList = itemArrList.get(entry.getKey() + "");
|
|
|
+ if (CollectionUtils.isEmpty(oldList)) {// 第一次追加,新增即可
|
|
|
+ itemArrList.put(entry.getKey() + "", arrayList);
|
|
|
+ } else {// 追加
|
|
|
+ oldList.addAll(arrayList);
|
|
|
+ itemArrList.put(entry.getKey() + "", oldList);
|
|
|
}
|
|
|
- groupList = new ArrayList<>();
|
|
|
- itemArrList = new HashMap<>();*/
|
|
|
- return e;
|
|
|
+ return saveValues(e);
|
|
|
} catch (DuplicateGroupException e) {
|
|
|
e.printStackTrace();
|
|
|
- log.error(e.getMessage());
|
|
|
+ log.error("重复分组id:" + entry.getKey());
|
|
|
} catch (NotConnectedException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (UnknownHostException e) {
|
|
@@ -205,7 +237,39 @@ public class OpcDAClient {
|
|
|
} catch (JIException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- return null;
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<RemoteOpc> saveValues(AddFailedException exception) {
|
|
|
+ List<RemoteOpc> remoteOpcFailList = new ArrayList<>();
|
|
|
+ // 如果报错要清空这2个,不然获取数据和保存数据不动
|
|
|
+ RedisUtils.del(RedisKeyConstants.redis_ok);
|
|
|
+ RedisUtils.del(RedisKeyConstants.redis_opc_update_flag);
|
|
|
+ Map<String, Integer> failedItems = exception.getErrors();
|
|
|
+ Map<String, Item> addItems = exception.getItems();
|
|
|
+ if (failedItems != null) {// 有不存在的item,需要更新对应的点位信息
|
|
|
+ for (Map.Entry<String, Integer> entry : failedItems.entrySet()) {
|
|
|
+ RemoteOpc remoteOpc = new RemoteOpc();
|
|
|
+ remoteOpc.setPositionNum(entry.getKey());
|
|
|
+ remoteOpc.setCreatedFlag(0);
|
|
|
+ remoteOpc.setRemark("opc server未找到改点位。可能原因1:AV/DV配置错误,2:opc server中未配置,3:点位和opc server中不一致");
|
|
|
+ log.error("opc server未找到该点位。key: " + remoteOpc.getPositionNum() + ", value: " + entry.getValue());
|
|
|
+ remoteOpcFailList.add(remoteOpc);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (addItems != null) {// 有不存在的item,需要更新对应的点位信息
|
|
|
+ for (Map.Entry<String, Item> entry : addItems.entrySet()) {
|
|
|
+ RemoteOpc remoteOpc = new RemoteOpc();
|
|
|
+ // 因为有些标签是:PT_9836_AV,不能用 entry.getKey().split("_")[0],需要找到最后一个_
|
|
|
+ // int index = entry.getKey().lastIndexOf("_");
|
|
|
+ remoteOpc.setPositionNum(entry.getKey());
|
|
|
+ remoteOpc.setCreatedFlag(1);
|
|
|
+ remoteOpc.setRemark("opc server已配置,AV/DV配置正确");
|
|
|
+ // log.error("opc server已配置。key: " + remoteOpc.getPositionNum() + ", value: " + entry.getValue());
|
|
|
+ remoteOpcFailList.add(remoteOpc);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return remoteOpcFailList;
|
|
|
}
|
|
|
|
|
|
/**
|