|
@@ -3,8 +3,12 @@ package com.platform.opc.servie;
|
|
import com.platform.common.constant.RedisKeyConstants;
|
|
import com.platform.common.constant.RedisKeyConstants;
|
|
import com.platform.common.util.RedisUtils;
|
|
import com.platform.common.util.RedisUtils;
|
|
import com.platform.common.util.StringUtils;
|
|
import com.platform.common.util.StringUtils;
|
|
|
|
+import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
|
+import com.platform.dao.enums.YesNoEnum;
|
|
import com.platform.dao.mapper.remote.RemoteOpcLogMapper;
|
|
import com.platform.dao.mapper.remote.RemoteOpcLogMapper;
|
|
|
|
+import com.platform.dao.mapper.remote.RemoteOpcMapper;
|
|
import com.platform.opc.util.OpcDAClient;
|
|
import com.platform.opc.util.OpcDAClient;
|
|
|
|
+import com.platform.opc.util.OpcUtil;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.jinterop.dcom.common.JIException;
|
|
import org.jinterop.dcom.common.JIException;
|
|
@@ -12,9 +16,14 @@ import org.openscada.opc.lib.da.Group;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
+import tk.mybatis.mapper.weekend.Weekend;
|
|
|
|
+import tk.mybatis.mapper.weekend.WeekendCriteria;
|
|
|
|
|
|
|
|
+import java.util.List;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service("opcTask")
|
|
@Service("opcTask")
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@@ -22,8 +31,60 @@ import java.util.concurrent.ScheduledExecutorService;
|
|
@EnableScheduling // 1.开启定时任务
|
|
@EnableScheduling // 1.开启定时任务
|
|
public class OpcTask {
|
|
public class OpcTask {
|
|
|
|
|
|
|
|
+ private final RemoteOpcMapper remoteOpcMapper;
|
|
private final OpcService opcService;
|
|
private final OpcService opcService;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 新增点位:在新增点位后,点击立即生效按钮,这个时间不能再执行上面的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)) {
|
|
|
|
+ OpcUtil.addGroupAndItems(findAllItems(uniqueStr));
|
|
|
|
+ log.info("添加新点位:"+ uniqueStr);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ 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(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("移除点位:" + removeList);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ RedisUtils.del(RedisKeyConstants.redis_opc_wait_remove_list);
|
|
|
|
+ RedisUtils.del(RedisKeyConstants.redis_opc_update_flag);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 初始化加载所有点位信息
|
|
|
|
+ *
|
|
|
|
+ * @param positionNumList
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<RemoteOpc> findAllItems(List<String> positionNumList) {
|
|
|
|
+ Weekend<RemoteOpc> weekend = new Weekend<>(RemoteOpc.class);
|
|
|
|
+ WeekendCriteria<RemoteOpc, Object> weekendCriteria = weekend.weekendCriteria();
|
|
|
|
+ // 启动分组,按照车间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;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 1: 分组获取数据
|
|
* 1: 分组获取数据
|
|
* a:保存到redis,前端页面实时从数据库获取数据,5秒刷新一次
|
|
* a:保存到redis,前端页面实时从数据库获取数据,5秒刷新一次
|
|
@@ -31,7 +92,8 @@ public class OpcTask {
|
|
@Scheduled(fixedDelay = 2000) //间隔2秒
|
|
@Scheduled(fixedDelay = 2000) //间隔2秒
|
|
public void getValue() throws JIException {
|
|
public void getValue() throws JIException {
|
|
String key = RedisUtils.getString(RedisKeyConstants.redis_opc_update_flag);
|
|
String key = RedisUtils.getString(RedisKeyConstants.redis_opc_update_flag);
|
|
- if(StringUtils.isBlank(key)){
|
|
|
|
|
|
+ String ok = RedisUtils.getString(RedisKeyConstants.redis_opc_update_flag);
|
|
|
|
+ if(StringUtils.isBlank(key) && StringUtils.isBlank(ok)){
|
|
for (int i = 0; i < OpcDAClient.groupList.size(); i++) {
|
|
for (int i = 0; i < OpcDAClient.groupList.size(); i++) {
|
|
Group group = OpcDAClient.groupList.get(i);
|
|
Group group = OpcDAClient.groupList.get(i);
|
|
opcService.getValue(group);
|
|
opcService.getValue(group);
|
|
@@ -49,7 +111,8 @@ public class OpcTask {
|
|
@Scheduled(fixedDelay = 300000) //间隔300秒,5分钟保存一次数据到数据库,确保每天不超过700万数据
|
|
@Scheduled(fixedDelay = 300000) //间隔300秒,5分钟保存一次数据到数据库,确保每天不超过700万数据
|
|
public void saveValue() throws JIException {
|
|
public void saveValue() throws JIException {
|
|
String key = RedisUtils.getString(RedisKeyConstants.redis_opc_update_flag);
|
|
String key = RedisUtils.getString(RedisKeyConstants.redis_opc_update_flag);
|
|
- if(StringUtils.isBlank(key)){
|
|
|
|
|
|
+ String ok = RedisUtils.getString(RedisKeyConstants.redis_opc_update_flag);
|
|
|
|
+ if(StringUtils.isBlank(key) && StringUtils.isBlank(ok)){
|
|
for (int i = 0; i < OpcDAClient.groupList.size(); i++) {
|
|
for (int i = 0; i < OpcDAClient.groupList.size(); i++) {
|
|
Group group = OpcDAClient.groupList.get(i);
|
|
Group group = OpcDAClient.groupList.get(i);
|
|
opcService.saveValue(group.getName());
|
|
opcService.saveValue(group.getName());
|