Selaa lähdekoodia

完善opc展示页面
修改ScheduleConfig的启动方式

hfxc226 2 vuotta sitten
vanhempi
commit
5eb933ea10

+ 10 - 1
platform-dao/src/main/java/com/platform/dao/quartz/ScheduleConfig.java

@@ -1,5 +1,6 @@
 package com.platform.dao.quartz;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@@ -17,6 +18,9 @@ import java.util.Properties;
 @Configuration
 public class ScheduleConfig {
 
+    @Value("${scheduling.enabled}")
+    private Boolean scheduleEnableFlg;
+
     @Bean
     public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
         SchedulerFactoryBean factory = new SchedulerFactoryBean();
@@ -52,7 +56,12 @@ public class ScheduleConfig {
         //可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
         factory.setOverwriteExistingJobs(true);
         //设置自动启动,默认为true
-        factory.setAutoStartup(true);
+        if (!scheduleEnableFlg) {
+            factory.setAutoStartup(false);
+        } else {
+            // 设置自动启动,默认为true
+            factory.setAutoStartup(true);
+        }
 
         return factory;
     }

+ 65 - 0
platform-opc/src/main/java/com/platform/opc/command/InitAddGroupAndItem.java

@@ -0,0 +1,65 @@
+package com.platform.opc.command;
+
+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 com.platform.opc.util.OpcUtil;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import tk.mybatis.mapper.weekend.Weekend;
+import tk.mybatis.mapper.weekend.WeekendCriteria;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+
+@Component("initAddGroupAndItem")
+@DependsOn({"beanUtils", "redisTemplate"})
+@AllArgsConstructor
+@Slf4j
+public class InitAddGroupAndItem implements CommandLineRunner {
+
+    private final RemoteOpcMapper remoteOpcMapper;
+
+    /**
+     * 建立ua连接*
+     */
+    @PostConstruct
+    public void getServer() {
+        OpcDAClient.connect();
+    }
+    /**
+     * 初始化redis和点位分组数据,并启动循环判断
+     * 筛选出来:已经在server里面配置好的点位*
+     * 1:分组提交到server,存在2种情况*
+     * a)在server里面尚未配置,导致找不到的*
+     * b)名称不一致导致的
+     * i)TE1009_AV,在server里面是TE1009_DV,
+     * ii)TE1009_AV,在server里面是是TE1009,导致不存在的,写入数据库,*
+     * 2:正常添加的,数据都对的,这个要写入redis*
+     */
+    @Override
+    public void run(String... args) {
+        RedisUtils.setString(RedisKeyConstants.redis_ok, "0");
+        log.info("开始-初始化分组");
+        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());
+        List<RemoteOpc> remoteOpcList = remoteOpcMapper.selectByExample(weekend);
+        List<RemoteOpc> list = OpcUtil.addGroupAndItems(remoteOpcList);
+        if (!CollectionUtils.isEmpty(list)) {
+            remoteOpcMapper.updateBatch(list);
+        }
+        log.info("结束-初始化分组");
+        RedisUtils.del(RedisKeyConstants.redis_ok);
+    }
+}
+

+ 0 - 156
platform-opc/src/main/java/com/platform/opc/servie/OpcInit.java

@@ -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);
-        }
-    }
-}
-

+ 65 - 2
platform-opc/src/main/java/com/platform/opc/servie/OpcTask.java

@@ -3,8 +3,12 @@ package com.platform.opc.servie;
 import com.platform.common.constant.RedisKeyConstants;
 import com.platform.common.util.RedisUtils;
 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.RemoteOpcMapper;
 import com.platform.opc.util.OpcDAClient;
+import com.platform.opc.util.OpcUtil;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 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.Scheduled;
 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.ScheduledExecutorService;
+import java.util.stream.Collectors;
 
 @Service("opcTask")
 @AllArgsConstructor
@@ -22,8 +31,60 @@ import java.util.concurrent.ScheduledExecutorService;
 @EnableScheduling   // 1.开启定时任务
 public class OpcTask {
 
+    private final RemoteOpcMapper remoteOpcMapper;
     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: 分组获取数据
      * a:保存到redis,前端页面实时从数据库获取数据,5秒刷新一次
@@ -31,7 +92,8 @@ public class OpcTask {
     @Scheduled(fixedDelay = 2000)  //间隔2秒
     public void getValue() throws JIException {
         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++) {
                 Group group = OpcDAClient.groupList.get(i);
                 opcService.getValue(group);
@@ -49,7 +111,8 @@ public class OpcTask {
     @Scheduled(fixedDelay = 300000)  //间隔300秒,5分钟保存一次数据到数据库,确保每天不超过700万数据
     public void saveValue() throws JIException {
         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++) {
                 Group group = OpcDAClient.groupList.get(i);
                 opcService.saveValue(group.getName());

+ 4 - 44
platform-opc/src/main/java/com/platform/opc/util/OpcDAClient.java

@@ -135,7 +135,7 @@ public class OpcDAClient {
      */
     public static AddFailedException addGroupList(Map<String, List<RemoteOpc>> listMap) {
         try {
-            log.info("开始建组...");
+            log.info("开始建组,分组数量:" + listMap.size());
             for (Map.Entry<String, List<RemoteOpc>> entry : listMap.entrySet()) {
                 Group group = null;
                 if (!CollectionUtils.isEmpty(groupList)) {
@@ -148,10 +148,9 @@ public class OpcDAClient {
                     }
                     if (group == null) {
                         log.info("追加分组line id: " + entry.getKey());
-                        if(server.findGroup(entry.getKey()) == null){
-                            group = server.addGroup(entry.getKey());
-                            groupList.add(group);
-                        }
+                        group = server.addGroup(entry.getKey());
+                        groupList.add(group);
+
                     }
                 } else {
                     log.info("groupList为空,新增分组line id: " + entry.getKey());
@@ -205,8 +204,6 @@ public class OpcDAClient {
             e.printStackTrace();
         } catch (JIException e) {
             e.printStackTrace();
-        } catch (UnknownGroupException e) {
-            e.printStackTrace();
         }
         return null;
     }
@@ -263,43 +260,6 @@ public class OpcDAClient {
         }
     }
 
-    /**
-     * 获取多组数据
-     *
-     * @return
-     */
-    public static List<OpcResult> getItemValuesList() {
-        List<OpcResult> resultList = new ArrayList<>();
-        try {
-            if (!CollectionUtils.isEmpty(groupList) || groupList.size() > 0) {
-                log.info("数据获取总组数:", groupList.size());
-                for (int i = 0; i < groupList.size(); i++) {
-                    Group group = groupList.get(i);
-                    log.info("获取分组的数据: 组名:" + group.getName());
-                    Item[] items = itemArrList.get(groupList.get(i).getName()).toArray(new Item[0]);
-                    log.info("获取分组的数据数量:" + items.length);
-                    Map<Item, ItemState> resultMap = group.read(true, items);
-                    //log.info("数据获取完成。数量:", resultMap.size() + ", 组序号:" + i);
-                    for (Item item : resultMap.keySet()) {
-                        OpcResult result = new OpcResult();
-                        ItemState itemMap = resultMap.get(item);
-                        String value = getVal(itemMap.getValue());
-                        result.setId(item.getId());
-                        result.setValue(value);
-                        result.setTime(DateUtils.dateToString(itemMap.getTimestamp().getTime()));
-                        // log.info("id: " + item.getId() + ", value: " + value + ", timestamp: " + itemMap.getTimestamp());
-                        resultList.add(result);
-                    }
-                }
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            log.error("批量获取数据异常:", e);
-            return null;
-        }
-        return resultList;
-    }
-
     /**
      * 获取单个组数据
      * 根据车间id来获取

+ 0 - 185
platform-opc/src/main/java/com/platform/opc/util/OpcUAClientUtil.java

@@ -1,185 +0,0 @@
-package com.platform.opc.util;
-
-import lombok.extern.slf4j.Slf4j;
-import org.jinterop.dcom.common.JIErrorCodes;
-import org.jinterop.dcom.common.JISystem;
-import org.openscada.opc.dcom.list.ClassDetails;
-import org.openscada.opc.lib.common.ConnectionInformation;
-
-import org.jinterop.dcom.common.JIException;
-import org.jinterop.dcom.core.JIString;
-import org.jinterop.dcom.core.JIVariant;
-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.openscada.opc.lib.list.Categories;
-import org.openscada.opc.lib.list.Category;
-import org.openscada.opc.lib.list.ServerList;
-
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.concurrent.Executors;
-
-@Slf4j
-public class OpcUAClientUtil {
-
-    public static String OPC_HOST = "192.168.108.108";
-    public static String OPC_USER = "Administrator";
-    public static String OPC_PASSWORD = "Hollysys";
-    public static String OPC_CLSID = "001AAAA6-FB54-4627-84B2-8777379E5868";
-
-    public static void run() throws Exception {
-        // 连接信息
-        JISystem.setAutoRegisteration(true);
-        final ConnectionInformation ci = new ConnectionInformation();
-        ci.setHost("192.168.108.108");         // 电脑IP
-        // ci.setDomain("");                  // 域,为空就行
-        ci.setUser("Administrator");             // 电脑上自己建好的用户名
-        ci.setPassword("Hollysys");          // 用户名的密码
-
-        // 使用MatrikonOPC Server的配置
-        // ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // MatrikonOPC的注册表ID,可以在“组件服务”里看到
-        //ci.setProgId("Matrikon.OPC.Simulation.1");
-
-        // 和利时
-        ci.setClsid("001AAAA6-FB54-4627-84B2-8777379E5868");
-        ci.setProgId("Hollysys.HOLLiASiComm.1");
-
-        // 使用KEPServer的配置
-        // ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
-        // ci.setProgId("Kepware.KEPServerEX.V6");
-        // final String itemId = "u.u.u";    // 项的名字按实际,没有实际PLC,用的模拟器:simulator
-        // final String itemId = "通道 1.设备 1.标记 1";
-
-        // 启动服务
-        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
-        //AutoReconnectController autos = new AutoReconnectController(server);
-        try {
-            //建立连接
-            System.out.println("Start开始连接----");
-            server.connect();
-            System.out.println("Success连接成功----");
-            ServerList serverList = new ServerList("192.168.108.108", "Administrator", "Hollysys", "");
-            Collection<ClassDetails> classDetails = serverList
-                    .listServersWithDetails(new Category[]{
-                            Categories.OPCDAServer10}, new Category[]{});
-            for (ClassDetails cds : classDetails) {
-                System.out.println(cds.getProgId() + "=" + cds.getDescription());
-            }
-            findAllItem(server);
-            final String itemId = "Channel1.Device1.AT_11001_AV";
-            System.out.println("连接成功");
-            final AccessBase access = new SyncAccess(server, 1000);
-            final int[] i = {1};
-            access.addItem(itemId, new DataCallback() {
-                @Override
-                public void changed(Item item, ItemState state) {
-                    // also dump value
-                    try {
-                        System.out.println(i[0]++ + ":" + getVal(state.getValue()));
-                    } catch (JIException e) {
-                        e.printStackTrace();
-                    }
-                }
-            });
-            // start reading,开始读值
-            access.bind();
-            // wait a little bit,有个10秒延时
-            Thread.sleep(10 * 1000);
-            // stop reading,停止读取
-            access.unbind();
-        } catch (final JIException e) {
-            e.printStackTrace();
-        } catch (IllegalArgumentException e) {
-            e.printStackTrace();
-        } catch (UnknownHostException e) {
-            e.printStackTrace();
-        } finally {
-            server.disconnect();
-        }
-
-    }
-
-    /**
-     * 查找所有item
-     *
-     * @param server
-     * @return
-     */
-    public static List findAllItem(Server server) {
-        FlatBrowser flatBrowser = server.getFlatBrowser();
-        List<String> list = new ArrayList<>();
-        if (flatBrowser == null) {
-            System.out.println("读取 空");
-        } else {
-            try {
-                Collection<String> collection = flatBrowser.browse();
-                for (String item : collection) {
-                    list.add(item);
-                }
-                System.out.println("list: " + list.size());
-            } catch (JIException e) {
-                System.out.println(e.getMessage());
-            } catch (UnknownHostException e) {
-                System.out.println("host主机IP错误 = " + e.getMessage());
-            }
-        }
-        return list;
-    }
-
-
-    /**
-     * 如果是 bool、string、short、int等直接返回字符串;
-     * 如果是 long 类型的数组,返回数字内容间加点,对应 long,数组,大小为6
-     * 如果是 float 类型的数组,返回数字内容间加逗号,对应 float,数组,大小为20
-     *
-     * @param var
-     * @return
-     * @throws JIException
-     */
-    private static Object getVal(JIVariant var) throws JIException {
-        Object value;
-        int type = var.getType();
-        switch (type) {
-            case JIVariant.VT_I2:
-                value = var.getObjectAsShort();
-                break;
-            case JIVariant.VT_I4:
-                value = var.getObjectAsInt();
-                break;
-            case JIVariant.VT_I8:
-                value = var.getObjectAsLong();
-                break;
-            case JIVariant.VT_R4:
-                value = var.getObjectAsFloat();
-                break;
-            case JIVariant.VT_R8:
-                value = var.getObjectAsDouble();
-                break;
-            case JIVariant.VT_BSTR:
-                value = var.getObjectAsString2();
-                break;
-            case JIVariant.VT_BOOL:
-                value = var.getObjectAsBoolean();
-                break;
-            case JIVariant.VT_UI2:
-            case JIVariant.VT_UI4:
-                value = var.getObjectAsUnsigned().getValue();
-                break;
-            case JIVariant.VT_EMPTY:
-                throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Variant is Empty.");
-            case JIVariant.VT_NULL:
-                throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Variant is null.");
-            default:
-                throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Unknown Type.");
-        }
-        return value;
-    }
-
-    public static void main(String[] args) throws Exception {
-        run();
-    }
-}

+ 84 - 0
platform-opc/src/main/java/com/platform/opc/util/OpcUtil.java

@@ -0,0 +1,84 @@
+package com.platform.opc.util;
+
+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 lombok.extern.slf4j.Slf4j;
+import org.jinterop.dcom.common.JIErrorCodes;
+import org.jinterop.dcom.common.JISystem;
+import org.openscada.opc.dcom.list.ClassDetails;
+import org.openscada.opc.lib.common.ConnectionInformation;
+
+import org.jinterop.dcom.common.JIException;
+import org.jinterop.dcom.core.JIString;
+import org.jinterop.dcom.core.JIVariant;
+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.openscada.opc.lib.list.Categories;
+import org.openscada.opc.lib.list.Category;
+import org.openscada.opc.lib.list.ServerList;
+import org.springframework.util.CollectionUtils;
+import tk.mybatis.mapper.weekend.Weekend;
+import tk.mybatis.mapper.weekend.WeekendCriteria;
+
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class OpcUtil {
+
+    /**
+     * 建立分组*
+     *
+     * @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();
+        // OpcDAClient.findAllItem();
+        List<RemoteOpc> remoteOpcFailList = new ArrayList<>();
+        AddFailedException exception = OpcDAClient.addGroupList(listMap);
+        if (exception != null) {
+            // 如果报错要清空这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;
+        /*if (!CollectionUtils.isEmpty(remoteOpcFailList)) {
+            remoteOpcMapper.updateBatch(remoteOpcFailList);
+        }*/
+    }
+}

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

@@ -3,3 +3,5 @@ spring:
     active: daoDev,dev
   main:
     web-application-type: none
+scheduling:
+  enabled: false

+ 3 - 0
platform-rest/src/main/resources/application-dev.yml

@@ -93,3 +93,6 @@ aliyun:
 remove:
   capitalType: 5
   companyId: 5e902722d762f008b01b9ee1
+
+scheduling:
+  enabled: false

+ 2 - 0
platform-rest/src/main/resources/application-prod.yml

@@ -71,3 +71,5 @@ remove:
   capitalType: 5
   companyId: 5e902722d762f008b01b9ee1
 
+scheduling:
+  enabled: true

+ 3 - 0
platform-rest/src/main/resources/application-test.yml

@@ -71,3 +71,6 @@ aliyun:
 remove:
   capitalType: 5
   companyId: 5e902722d762f008b01b9ee1
+
+scheduling:
+  enabled: false

+ 1 - 1
platform-rest/src/main/resources/application.yml

@@ -1,3 +1,3 @@
 spring:
   profiles:
-    active: daoDev,serviceDev,dev
+    active: daoDev,serviceDev,dev