Pārlūkot izejas kodu

Merge branch 'demo_' of http://124.71.196.186:8888/hfxc226/hitch-manage into demo_

guarantee-lsq 2 gadi atpakaļ
vecāks
revīzija
242a15be5d

+ 0 - 2
platform-dao/src/main/resources/application-daoTest.yml

@@ -5,9 +5,7 @@ spring:
         driver-class-name: com.mysql.cj.jdbc.Driver
         url: jdbc:mysql://192.168.16.222:3306/hitch-sb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true&removeAbandoned=true&removeAbandonedTimeout=60&logAbandoned=true
         username: root
-        # password: mysql?123!@MYSQL
         password: mydm888
-        # password: sbgld@QYKH@$_e^mysqlv
         filters: wall,stat
         filter:
           stat:

+ 170 - 0
platform-opc/src/main/java/com/platform/opc/servie/OpcInitService.java

@@ -0,0 +1,170 @@
+package com.platform.opc.servie;
+
+import com.platform.common.constant.UpmsRedisKeyConstants;
+import com.platform.common.model.ClientInfoVO;
+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("opcInitService")
+@DependsOn({"beanUtils", "redisTemplate"})
+@AllArgsConstructor
+@Slf4j
+@EnableScheduling   // 1.开启定时任务
+public class OpcInitService {
+
+    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(OpcDAClient.redis_opc_item_values);
+        log.info("开始初始化分组");
+        addGroupAndItems(findAllItems(true, null));
+    }
+
+    /**
+     * 1: 新增的数据添加到分组里面
+     * a: 之前已经新增,但是server里面没配置,现在server刚配置(修改createdFlag = 1)
+     * b: 新增的点位,在server里面也配置了,createdFlag = 1
+     * c: 新增的点位,设置了createdFlag = 1,但是加入到分组的时候,server里面就没配置,这个时候需要更新:createdFlag = 0
+     * d: 之前点位配置到线路1,但是现在修改到了线路2,需要重新添加到分组,要从原有分组进行删除,不然2个组都获取到了数据*
+     * <p>
+     * 注意:获取的时候写入一个key,用于标识正在读,这个时候,不进行点位新增* * *
+     */
+    /*@Scheduled(fixedDelay = 300000)  //间隔5分钟,300秒查询,需要同步新增的点位。
+    public void addItems() {
+        RedisUtils.setString(OpcDAClient.redis_opc_update_flag, "1");
+        addGroupAndItems(findAllItems(true, null));
+        RedisUtils.del(OpcDAClient.redis_opc_update_flag);
+    }*/
+
+    /**
+     * 立即新增点位:在新增点位后,点击立即生效按钮,这个时间不能再执行上面的addItems()方法
+     * 立即取消点位:在点位,点击取消采集按钮
+     * 每一分钟*
+     */
+    /*@Scheduled(fixedDelay = 60000)
+    public void addItems() {
+        RedisUtils.setString(OpcDAClient.redis_opc_update_flag, "1");
+
+        List<String> list = RedisUtils.getList(OpcDAClient.redis_opc_wait_add_list, 0, -1);
+        if (!CollectionUtils.isEmpty(list)) {
+            List uniqueStr = list.stream().distinct().collect(Collectors.toList());
+            if (!CollectionUtils.isEmpty(uniqueStr)) {
+                addGroupAndItems(findAllItems(false, uniqueStr));
+            }
+        }
+        RedisUtils.del(OpcDAClient.redis_opc_wait_add_list);
+
+        List<String> removeList = RedisUtils.getList(OpcDAClient.redis_opc_wait_remove_list, 0, -1);
+        if (!CollectionUtils.isEmpty(removeList)) {
+            List<RemoteOpc> remoteOpcList = findAllItems(false, list);
+            if (!CollectionUtils.isEmpty(remoteOpcList)) {
+                List uniqueStr = remoteOpcList.stream().distinct().collect(Collectors.toList());
+                if (!CollectionUtils.isEmpty(uniqueStr)) {
+                    OpcDAClient.removeItems(remoteOpcList.stream().collect(Collectors.groupingBy(RemoteOpc::getLine)));
+                }
+            }
+        }
+        RedisUtils.del(OpcDAClient.redis_opc_wait_remove_list);
+
+        RedisUtils.del(OpcDAClient.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) {
+            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);
+        }
+    }
+}
+

+ 70 - 126
platform-opc/src/main/java/com/platform/opc/servie/OpcTaskService.java

@@ -4,20 +4,15 @@ import com.alibaba.fastjson.JSON;
 import com.platform.common.util.DateUtils;
 import com.platform.common.util.RedisUtils;
 import com.platform.common.util.StringUtils;
+import com.platform.dao.dto.check.CheckProjectDTO;
 import com.platform.dao.entity.remote.RemoteOpc;
 import com.platform.dao.entity.remote.RemoteOpcLog;
-import com.platform.dao.enums.YesNoEnum;
 import com.platform.dao.mapper.remote.RemoteOpcLogMapper;
 import com.platform.dao.mapper.remote.RemoteOpcMapper;
 import com.platform.opc.entity.OpcResult;
 import com.platform.opc.util.OpcDAClient;
-import org.openscada.opc.lib.da.AddFailedException;
-import org.openscada.opc.lib.da.Item;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.context.annotation.DependsOn;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -25,15 +20,13 @@ import org.springframework.util.CollectionUtils;
 import tk.mybatis.mapper.weekend.Weekend;
 import tk.mybatis.mapper.weekend.WeekendCriteria;
 
-import javax.annotation.PostConstruct;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 import java.util.stream.Collectors;
 
 @Service("opcTaskService")
-@DependsOn({"beanUtils", "redisTemplate"})
 @AllArgsConstructor
 @Slf4j
 @EnableScheduling   // 1.开启定时任务
@@ -42,84 +35,35 @@ public class OpcTaskService {
     private final RemoteOpcMapper remoteOpcMapper;
     private final RemoteOpcLogMapper remoteOpcLogMapper;
 
-    /**
-     * 初始化redis和点位分组数据,并启动循环判断
-     */
-    @PostConstruct
-    public void initClients() {
-        log.info("开始初始化");
-        // 判断开发环境还是本地环境
-        // 启动分组,按照车间line分组,选择已经在opc server中配置的
-        Weekend<RemoteOpc> weekend = new Weekend<>(RemoteOpc.class);
-        WeekendCriteria<RemoteOpc, Object> weekendCriteria = weekend.weekendCriteria();
-        weekendCriteria.andEqualTo(RemoteOpc::getCreatedFlag, YesNoEnum.YES.getValue());
-        List<RemoteOpc> remoteOpcList = remoteOpcMapper.selectByExample(weekend);
-        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) {
-            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();
-                    // 因为有些标签是:PT_9836_AV,不能用 entry.getKey().split("_")[0],需要找到最后一个_
-                    int index = entry.getKey().lastIndexOf("_");
-                    remoteOpc.setPositionNum(entry.getKey());
-                    remoteOpc.setCreatedFlag(0);
-                    remoteOpc.setRemark("opc server未找到改点位。可能原因1:AV/DV配置错误,2: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);
-        }
-    }
-
     /**
      * 1: 分组获取数据
      * a:保存到redis,前端页面实时从数据库获取数据,5秒刷新一次
      */
-    @Scheduled(fixedDelay = 5000)  //间隔5
+    @Scheduled(fixedDelay = 2000)  //间隔2秒
     public void getValue() {
-        log.info("开始定时任务");
-        List<OpcResult> resultList = OpcDAClient.getItemValuesList();
-        if (!CollectionUtils.isEmpty(resultList)) {
-            //log.info("resultList数量:" + resultList.size());
-            RedisUtils.setString("opcList", JSON.toJSONString(resultList));
-            // 更新数据库实时数据
-            List<RemoteOpc> remoteOpcList = new ArrayList<>();
-            LocalDateTime localDateTime = LocalDateTime.now();
-            for (OpcResult result : resultList) {
-                RemoteOpc remoteOpc = new RemoteOpc();
-                remoteOpc.setResult(result.getValue());
-                remoteOpc.setPositionNum(result.getId());
-                remoteOpc.setUpdateTime(localDateTime);
-                remoteOpcList.add(remoteOpc);
+        String key = RedisUtils.getString(OpcDAClient.redis_opc_update_flag);
+        if(StringUtils.isBlank(key)){
+            log.info("开始拉取数据");
+            List<OpcResult> resultList = OpcDAClient.getItemValuesList();
+            if (!CollectionUtils.isEmpty(resultList)) {
+                log.info("拉取数量:" + resultList.size());
+                RedisUtils.setString(OpcDAClient.redis_opc_item_values, JSON.toJSONString(resultList));
+                // 更新数据库实时数据
+                List<RemoteOpc> remoteOpcList = new ArrayList<>();
+                LocalDateTime localDateTime = LocalDateTime.now();
+                for (OpcResult result : resultList) {
+                    RemoteOpc remoteOpc = new RemoteOpc();
+                    remoteOpc.setResult(new BigDecimal(result.getValue()));
+                    remoteOpc.setPositionNum(result.getId());
+                    remoteOpc.setUpdateTime(localDateTime);
+                    remoteOpcList.add(remoteOpc);
+                }
+                remoteOpcMapper.updateBatch(remoteOpcList);
+            } else {
+                log.info("初始化启动分组错误,等待下次重新启动分组");
             }
-            remoteOpcMapper.updateBatch(remoteOpcList);
-        }else{
-            log.info("初始化启动分组错误,重新启动分组");
-            initClients();
+            log.info("结束拉取数据");
         }
-        log.info("结束定时任务");
     }
 
     /**
@@ -131,54 +75,54 @@ public class OpcTaskService {
      */
     @Scheduled(fixedDelay = 300000)  //间隔300秒,5分钟保存一次数据到数据库,确保每天不超过700万数据
     public void saveValue() {
-        log.info("开始读取redis1");
-        String jsonStr = RedisUtils.getString("opcList");
-        if (StringUtils.isNotBlank(jsonStr)) {
-            List<OpcResult> resultList = JSON.parseArray(jsonStr, OpcResult.class);
-            /*for(OpcResult result: resultList){
-                log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
-            }*/
-            // 保存入库
-            // 查询当天是否已经存在了,存在则追加,否则则更新
-            Weekend<RemoteOpcLog> weekend = new Weekend<>(RemoteOpcLog.class);
-            WeekendCriteria<RemoteOpcLog, Object> weekendCriteria = weekend.weekendCriteria();
-            OpcResult opcResult = resultList.get(0);
-            LocalDateTime time = DateUtils.strToLocalDateTime(opcResult.getTime(), DateUtils.PATTERN_YMD_HMS);
-            weekendCriteria.andEqualTo(RemoteOpcLog::getPositionNum, opcResult.getId());
-            weekendCriteria.andEqualTo(RemoteOpcLog::getYear, time.getYear());
-            weekendCriteria.andEqualTo(RemoteOpcLog::getMonth, time.getMonthValue());
-            weekendCriteria.andEqualTo(RemoteOpcLog::getDay, time.getDayOfMonth());
-            int count = remoteOpcLogMapper.selectCountByExample(weekend);
-            List<RemoteOpcLog> remoteOpcLogList = new ArrayList<>();
-            for (OpcResult result : resultList) {
-                RemoteOpcLog remoteOpcLog = new RemoteOpcLog();
-                remoteOpcLog.setPositionNum(result.getId());
-                remoteOpcLog.setResult(result.getValue());
-                LocalDateTime localDateTime = DateUtils.strToLocalDateTime(result.getTime(), DateUtils.PATTERN_YMD_HMS);
-                remoteOpcLog.setCreatedTime(localDateTime);
-                remoteOpcLog.setYear(localDateTime.getYear());
-                remoteOpcLog.setMonth(localDateTime.getMonthValue());
-                remoteOpcLog.setDay(localDateTime.getDayOfMonth());
-                // remoteOpcLog.setHour(localDateTime.getHour());
-                // remoteOpcLog.setMinute(localDateTime.getMinute());
-                remoteOpcLog.setRemark(result.getTime().split(" ")[1] + "," + result.getValue() + ";");
-                remoteOpcLogList.add(remoteOpcLog);
-                //log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
-            }
-
-            // TODO:判断remoteOpcLogList是否大于5000,大于5000,就分多次存入
-            log.info("count:" + count);
-            if (count > 0) {
-                remoteOpcLogMapper.updateBatch(remoteOpcLogList);
-            } else {
-                remoteOpcLogMapper.insertListforComplex(remoteOpcLogList);
+        String key = RedisUtils.getString(OpcDAClient.redis_opc_update_flag);
+        if(StringUtils.isBlank(key)){
+            log.info("开始保存点位");
+            String jsonStr = RedisUtils.getString(OpcDAClient.redis_opc_item_values);
+            if (StringUtils.isNotBlank(jsonStr)) {
+                List<OpcResult> resultList = JSON.parseArray(jsonStr, OpcResult.class);
+                List<RemoteOpcLog> addOpcLogList = new ArrayList<>();
+                List<RemoteOpcLog> updateRemoteOpcLogList = new ArrayList<>();
+                Weekend<RemoteOpcLog> weekend = new Weekend<>(RemoteOpcLog.class);
+                WeekendCriteria<RemoteOpcLog, Object> weekendCriteria = weekend.weekendCriteria();
+                // 查询当天是否已经存在了,存在则追加,否则更新
+                LocalDateTime time = LocalDateTime.now();
+                weekendCriteria.andIn(RemoteOpcLog::getPositionNum, resultList.stream().map(OpcResult::getId).collect(Collectors.toList()));
+                weekendCriteria.andEqualTo(RemoteOpcLog::getYear, time.getYear());
+                weekendCriteria.andEqualTo(RemoteOpcLog::getMonth, time.getMonthValue());
+                weekendCriteria.andEqualTo(RemoteOpcLog::getDay, time.getDayOfMonth());
+                List<RemoteOpcLog> checkList = remoteOpcLogMapper.selectByExample(weekend);
+                for (OpcResult result : resultList) {
+                    RemoteOpcLog remoteOpcLog = new RemoteOpcLog();
+                    remoteOpcLog.setPositionNum(result.getId());
+                    remoteOpcLog.setResult(new BigDecimal(result.getValue()));
+                    LocalDateTime localDateTime = DateUtils.strToLocalDateTime(result.getTime(), DateUtils.PATTERN_YMD_HMS);
+                    remoteOpcLog.setCreatedTime(localDateTime);
+                    remoteOpcLog.setYear(localDateTime.getYear());
+                    remoteOpcLog.setMonth(localDateTime.getMonthValue());
+                    remoteOpcLog.setDay(localDateTime.getDayOfMonth());
+                    // remoteOpcLog.setHour(localDateTime.getHour());
+                    // remoteOpcLog.setMinute(localDateTime.getMinute());
+                    remoteOpcLog.setRemark(result.getTime().split(" ")[1] + "," + result.getValue() + ";");
+                    List<RemoteOpcLog> findItemList = checkList.stream().filter(remoteOpcLog1 -> remoteOpcLog1.getPositionNum().equals(result.getId())).collect(Collectors.toList());
+                    if (!CollectionUtils.isEmpty(findItemList) && findItemList.size()>0) {
+                        updateRemoteOpcLogList.add(remoteOpcLog);
+                    } else {
+                        addOpcLogList.add(remoteOpcLog);
+                    }
+                }
+                if(!CollectionUtils.isEmpty(updateRemoteOpcLogList)){
+                    remoteOpcLogMapper.updateBatch(updateRemoteOpcLogList);
+                    log.info("更新数量:" + resultList.size());
+                }
+                if(!CollectionUtils.isEmpty(addOpcLogList)){
+                    remoteOpcLogMapper.insertListforComplex(addOpcLogList);
+                    log.info("写入数量:" + resultList.size());
+                }
+                log.info("写入/更新数据库数量:" + resultList.size());
             }
+            log.info("结束保存点位");
         }
-        log.info("结束读取redis1");
     }
 
-    public static void main(String[] args) {
-        String time = "2022-11-12 12:04:06";
-        System.out.println(time.split(" ")[1]);
-    }
 }

+ 87 - 83
platform-opc/src/main/java/com/platform/opc/util/OpcDAClient.java

@@ -1,9 +1,8 @@
 package com.platform.opc.util;
 
+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.vo.SysUserVO;
 import com.platform.opc.entity.OpcResult;
 import lombok.extern.slf4j.Slf4j;
 import org.jinterop.dcom.common.JIErrorCodes;
@@ -18,8 +17,6 @@ import org.openscada.opc.lib.da.browser.FlatBrowser;
 import org.springframework.util.CollectionUtils;
 
 import java.net.UnknownHostException;
-import java.time.LocalDateTime;
-import java.time.ZoneOffset;
 import java.util.*;
 import java.util.concurrent.Executors;
 import java.util.stream.Collectors;
@@ -31,6 +28,13 @@ import java.util.stream.Collectors;
  */
 @Slf4j
 public class OpcDAClient {
+
+    public static String redis_opc_item_values = "redis_opc_item_values";
+    public static String redis_opc_update_flag = "redis_opc_update_flag";
+    // 保存的是点位列表
+    public static String redis_opc_wait_add_list = "redis_opc_wait_add_list";
+    // 保存的是点位列表
+    public static String redis_opc_wait_remove_list = "redis_opc_wait_remove_list";
     public static String host = "192.168.108.108";
     public static String user = "Administrator";
     public static String password = "Hollysys";
@@ -39,9 +43,8 @@ public class OpcDAClient {
     public static String tag_prefix = "Channel1.Device1.";
     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 = null;
+    // private static Item[][] itemArrList = null;
+    private static Map<String, List<Item>> itemArrList = new HashMap<>();
 
     /**
      * 初始化连接信息
@@ -133,27 +136,44 @@ public class OpcDAClient {
      * @return
      */
     public static AddFailedException addGroupList(Map<String, List<RemoteOpc>> listMap) {
-        Map<String, Object> result = new HashMap<>();
-        itemArrList = new Item[listMap.size()][];
+        itemArrList = new HashMap<>();
         try {
-            if (CollectionUtils.isEmpty(groupItemsList) || CollectionUtils.isEmpty(groupList)) {
-                log.info("开始建组...");
-                int i = 0;
-                for (Map.Entry<String, List<RemoteOpc>> entry : listMap.entrySet()) {
-                    Group group = server.addGroup(entry.getKey()+"");
-                    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);
-                    groupList.add(group);
-                    groupItemsList.add(groupItems);
-                    itemSetList.add(itemSet);
-                    itemArrList[i++] = itemArr;
+            log.info("开始建组...");
+            int i = 0;
+            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() + "")) {
+                            group = checkGroup;
+                            break;
+                        }
+                    }
+                    if (group == null) {
+                        group = server.addGroup(entry.getKey() + "");
+                    }
+                } else {
+                    group = server.addGroup(entry.getKey() + "");
+                }
+
+                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);
+                groupList.add(group);
+
+                List<Item> arrayList = new ArrayList<Item>(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("组建完成,开始查询数据...,分组数量:" + i);
             }
@@ -161,8 +181,8 @@ public class OpcDAClient {
             e.printStackTrace();
             // 将不存在的点位信息保存到数据库,
             Map<String, Integer> errorsItemMap = e.getErrors();
-            e.printStackTrace();
-            log.error("批量获取数据异常:", e);
+            // e.printStackTrace();
+            log.error("添加点位出错,有不存在的地位,等待下次启动添加", e);
             return e;
         } catch (DuplicateGroupException e) {
             e.printStackTrace();
@@ -177,77 +197,61 @@ public class OpcDAClient {
     }
 
     /**
-     * 获取多组数据
+     * 取消点位
      *
-     * @param itemIdList
+     * @param listMap
      * @return
      */
-   /* public Map<String, Object> (List<String> itemIdList) {
-        Map<String, Object> result = new HashMap<>();
+    public static void removeItems(Map<String, List<RemoteOpc>> listMap) {
         try {
-            if (groupItems == null || group == null) {
-                log.info("开始建组...");
-                group = server.addGroup();
-                String[] items = itemIdList.toArray(new String[]{});
-                groupItems = group.addItems(items);
-                log.info("组建完成,开始查询数据...");
+            if (CollectionUtils.isEmpty(groupList)) {
+                return;
             }
-            Set itemSet = new HashSet(groupItems.values());
-            Item[] itemArr = new Item[itemSet.size()];
-            itemSet.toArray(itemArr);
-            Map<Item, ItemState> resultMap = group.read(true, itemArr);
-            log.info("数据获取完成:{}条", resultMap.size());
-            for (Item item : resultMap.keySet()) {
-                ItemState itemMap = resultMap.get(item);
-                result.put(item.getId(), getVal(itemMap.getValue()));
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            log.error("批量获取数据异常:", e);
-        }
-        return result;
-    }*/
-
-    /**
-     * 获取多组数据
-     * @return
-     */
-    /*public static List<Map<String, String>> getItemValuesList() {
-        List<Map<String, String>> resultList = new ArrayList<>();
-        try {
-            if (!CollectionUtils.isEmpty(groupItemsList)) {
-                log.info("数据获取总组数:", groupItemsList.size());
-                for (int i = 0; i < groupItemsList.size(); i++) {
-                    Map<Item, ItemState> resultMap = groupList.get(i).read(true, itemArrList[i]);
-                    log.info("数据获取完成。数量:", resultMap.size() + ", 组序号:" + i);
-                    Map<String, String> result = new HashMap<>();
-                    for (Item item : resultMap.keySet()) {
-                        ItemState itemMap = resultMap.get(item);
-                        String value = getVal(itemMap.getValue());
-                        result.put(item.getId(), value);
-                        log.info("id: " + item.getId() + ", value: " + value + ", timestamp: " + itemMap.getTimestamp());
+            for (Group group : groupList) {
+                for (Map.Entry<String, List<RemoteOpc>> entry : listMap.entrySet()) {
+                    if (group.getName().equals(entry.getKey() + "")) {
+                        List<RemoteOpc> list = entry.getValue();
+                        for (RemoteOpc remoteOpc : list) {
+                            group.removeItem(remoteOpc.getPositionNum());
+                        }
+                        // 更新item数据
+                        List<Item> items = itemArrList.get(entry.getKey() + "");
+                        List<Item> itemResults = new ArrayList<>();
+                        for (Item item : items) {
+                            boolean find = false;
+                            for (RemoteOpc remoteOpc : list) {
+                                if(item.getId().equals(remoteOpc.getPositionNum())){
+                                    find =true;
+                                    break;
+                                }
+                            }
+                            if(!find){
+                                itemResults.add(item);
+                            }
+                        }
+                        itemArrList.put(entry.getKey() + "", itemResults);
                     }
-                    resultList.add(result);
                 }
             }
-        } catch (Exception e) {
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        } catch (JIException e) {
             e.printStackTrace();
-            log.error("批量获取数据异常:", e);
         }
-        return resultList;
-    }*/
+    }
 
     /**
      * 获取多组数据
+     *
      * @return
      */
     public static List<OpcResult> getItemValuesList() {
         List<OpcResult> resultList = new ArrayList<>();
         try {
-            if (!CollectionUtils.isEmpty(groupItemsList)) {
-                log.info("数据获取总组数:", groupItemsList.size());
-                for (int i = 0; i < groupItemsList.size(); i++) {
-                    Map<Item, ItemState> resultMap = groupList.get(i).read(true, itemArrList[i]);
+            if (!CollectionUtils.isEmpty(groupList)) {
+                log.info("数据获取总组数:", groupList.size());
+                for (int i = 0; i < groupList.size(); i++) {
+                    Map<Item, ItemState> resultMap = groupList.get(i).read(true, itemArrList.get(groupList.get(i).getName()).toArray(new Item[0]));
                     //log.info("数据获取完成。数量:", resultMap.size() + ", 组序号:" + i);
                     for (Item item : resultMap.keySet()) {
                         OpcResult result = new OpcResult();
@@ -289,7 +293,7 @@ public class OpcDAClient {
                 value = var.getObjectAsLong() + "";
                 break;
             case JIVariant.VT_R4:
-                value = var.getObjectAsFloat() + "";
+                value = BigDecimalUtil.decimal(var.getObjectAsFloat(), 2) + "";
                 break;
             case JIVariant.VT_R8:
                 value = var.getObjectAsDouble() + "";

+ 0 - 2
platform-opc/src/main/java/org/openscada/opc/lib/da/Group.java

@@ -231,11 +231,9 @@ public class Group {
 
     private synchronized Map<String, Integer> findItems(final String[] items) {
         Map<String, Integer> data = new HashMap<String, Integer>();
-
         for (int i = 0; i < items.length; i++) {
             data.put(items[i], this._itemHandleMap.get(items[i]));
         }
-
         return data;
     }
 

+ 1 - 1
platform-rest/src/main/java/com/platform/rest/controller/remote/RemoteOpcController.java

@@ -134,7 +134,7 @@ public class RemoteOpcController {
      * @return R
      */
     @GetMapping("/page")
-    public R<AbstractPageResultBean<RemoteOpcVO>> query(RemoteOpcDTO remoteMeasureDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
+    public R<AbstractPageResultBean<RemoteOpcVO>> queryPage(RemoteOpcDTO remoteMeasureDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
         return new R<>(remoteMeasureService.selectPageList(remoteMeasureDTO, pageNum, pageSize));
     }