|
@@ -1,156 +0,0 @@
|
|
|
-package com.platform.opc.servie;
|
|
|
-
|
|
|
-import com.platform.common.constant.RedisKeyConstants;
|
|
|
-import com.platform.common.util.RedisUtils;
|
|
|
-import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
-import com.platform.dao.enums.YesNoEnum;
|
|
|
-import com.platform.dao.mapper.remote.RemoteOpcMapper;
|
|
|
-import com.platform.opc.util.OpcDAClient;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.openscada.opc.lib.da.AddFailedException;
|
|
|
-import org.openscada.opc.lib.da.Item;
|
|
|
-import org.springframework.context.annotation.DependsOn;
|
|
|
-import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-import tk.mybatis.mapper.weekend.Weekend;
|
|
|
-import tk.mybatis.mapper.weekend.WeekendCriteria;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Service("opcInit")
|
|
|
-@DependsOn({"beanUtils", "redisTemplate"})
|
|
|
-@AllArgsConstructor
|
|
|
-@Slf4j
|
|
|
-@EnableScheduling // 1.开启定时任务
|
|
|
-public class OpcInit {
|
|
|
-
|
|
|
- private final RemoteOpcMapper remoteOpcMapper;
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化redis和点位分组数据,并启动循环判断
|
|
|
- * 筛选出来:已经在server里面配置好的点位*
|
|
|
- * 1:分组提交到server,存在2种情况*
|
|
|
- * a)在server里面尚未配置,导致找不到的*
|
|
|
- * b)名称不一致导致的
|
|
|
- * i)TE1009_AV,在server里面是TE1009_DV,
|
|
|
- * ii)TE1009_AV,在server里面是是TE1009,导致不存在的,写入数据库,*
|
|
|
- * 2:正常添加的,数据都对的,这个要写入redis*
|
|
|
- */
|
|
|
- @PostConstruct
|
|
|
- public void initAddAllItem() {
|
|
|
- RedisUtils.del(RedisKeyConstants.redis_ok);
|
|
|
- log.info("开始初始化分组");
|
|
|
- addGroupAndItems(findAllItems(true, null));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 立即新增点位:在新增点位后,点击立即生效按钮,这个时间不能再执行上面的addItems()方法
|
|
|
- * 立即取消点位:在点位,点击取消采集按钮
|
|
|
- * 每100秒*
|
|
|
- */
|
|
|
- @Scheduled(fixedDelay = 100000)
|
|
|
- public void addAndDelItems() {
|
|
|
- RedisUtils.setString(RedisKeyConstants.redis_opc_update_flag, "1");
|
|
|
- List<String> list = RedisUtils.getList(RedisKeyConstants.redis_opc_wait_add_list, 0, -1);
|
|
|
- if (!CollectionUtils.isEmpty(list)) {
|
|
|
- List<String> uniqueStr = list.stream().distinct().collect(Collectors.toList());
|
|
|
- if (!CollectionUtils.isEmpty(uniqueStr)) {
|
|
|
- addGroupAndItems(findAllItems(false, uniqueStr));
|
|
|
- log.info("添加新点位:");
|
|
|
- }
|
|
|
- }
|
|
|
- RedisUtils.del(RedisKeyConstants.redis_opc_wait_add_list);
|
|
|
-
|
|
|
- List<String> removeList = RedisUtils.getList(RedisKeyConstants.redis_opc_wait_remove_list, 0, -1);
|
|
|
- if (!CollectionUtils.isEmpty(removeList)) {
|
|
|
- List<RemoteOpc> remoteOpcList = findAllItems(false, removeList);
|
|
|
- if (!CollectionUtils.isEmpty(remoteOpcList)) {
|
|
|
- List<RemoteOpc> uniqueStr = remoteOpcList.stream().distinct().collect(Collectors.toList());
|
|
|
- if (!CollectionUtils.isEmpty(uniqueStr)) {
|
|
|
- OpcDAClient.removeItems(remoteOpcList.stream().collect(Collectors.groupingBy(RemoteOpc::getLine)));
|
|
|
- log.info("移除点位:");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- RedisUtils.del(RedisKeyConstants.redis_opc_wait_remove_list);
|
|
|
- RedisUtils.del(RedisKeyConstants.redis_opc_update_flag);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化加载所有点位信息
|
|
|
- *
|
|
|
- * @param isAll
|
|
|
- * @param positionNumList
|
|
|
- * @return
|
|
|
- */
|
|
|
- private List<RemoteOpc> findAllItems(boolean isAll, List<String> positionNumList) {
|
|
|
- Weekend<RemoteOpc> weekend = new Weekend<>(RemoteOpc.class);
|
|
|
- WeekendCriteria<RemoteOpc, Object> weekendCriteria = weekend.weekendCriteria();
|
|
|
- if (isAll) {
|
|
|
- // 启动分组,按照车间line分组,选择已经在opc server中配置的, 且点位位置也在页面上配置的
|
|
|
- weekendCriteria.andEqualTo(RemoteOpc::getCreatedFlag, YesNoEnum.YES.getValue()).andEqualTo(RemoteOpc::getPositionFlag, YesNoEnum.YES.getValue());
|
|
|
- List<RemoteOpc> remoteOpcList = remoteOpcMapper.selectByExample(weekend);
|
|
|
- return remoteOpcList;
|
|
|
- } else {
|
|
|
- // 启动分组,按照车间line分组,选择已经在opc server中配置的
|
|
|
- weekendCriteria.andEqualTo(RemoteOpc::getCreatedFlag, YesNoEnum.YES.getValue()).andEqualTo(RemoteOpc::getPositionFlag, YesNoEnum.YES.getValue())
|
|
|
- .andIn(RemoteOpc::getPositionNum, positionNumList);
|
|
|
- List<RemoteOpc> remoteOpcList = remoteOpcMapper.selectByExample(weekend);
|
|
|
- return remoteOpcList;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 建立分组*
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public void addGroupAndItems(List<RemoteOpc> remoteOpcList) {
|
|
|
- log.info("remoteOpcList: " + remoteOpcList.size());
|
|
|
- Map<String, List<RemoteOpc>> listMap = remoteOpcList.stream().collect(Collectors.groupingBy(RemoteOpc::getLine));
|
|
|
- OpcDAClient.connect();
|
|
|
- // OpcDAClient.findAllItem();
|
|
|
- List<RemoteOpc> remoteOpcFailList = new ArrayList<>();
|
|
|
- AddFailedException exception = OpcDAClient.addGroupList(listMap);
|
|
|
- if (exception != null) {
|
|
|
- // 如果报错要清空这2个,不然获取数据和保存数据不动
|
|
|
- RedisUtils.setString(RedisKeyConstants.redis_ok, "0");
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!CollectionUtils.isEmpty(remoteOpcFailList)) {
|
|
|
- remoteOpcMapper.updateBatch(remoteOpcFailList);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|