Browse Source

opc集成redis

hfxc226 2 years ago
parent
commit
a5df840824

+ 0 - 1
platform-common/src/main/java/com/platform/common/util/RedisUtils.java

@@ -18,7 +18,6 @@ import java.util.concurrent.TimeUnit;
 @Slf4j
 public final class RedisUtils {
 
-
     /**
      * 指定缓存失效时间
      * 若key不存在,不会报错,并且返回成功

+ 9 - 2
platform-opc/pom.xml

@@ -125,13 +125,17 @@
             <groupId>org.openscada.utgard</groupId>
             <artifactId>org.openscada.opc.lib</artifactId>
             <version>1.5.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.bouncycastle</groupId>
+            <artifactId>bcprov-jdk15on</artifactId>
+            <version>1.5.0</version>
         </dependency>-->
         <dependency>
             <groupId>org.bouncycastle</groupId>
             <artifactId>bcprov-jdk15on</artifactId>
-            <version>1.61</version>
+            <version>1.50</version>
         </dependency>
-
     </dependencies>
 
     <build>
@@ -141,6 +145,9 @@
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <includeSystemScope>true</includeSystemScope>
+                </configuration>
             </plugin>
         </plugins>
     </build>

+ 8 - 24
platform-opc/src/main/java/com/platform/opc/util/OpcDAClient.java

@@ -1,5 +1,6 @@
 package com.platform.opc.util;
 
+import com.platform.common.util.RedisUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.jinterop.dcom.common.JIErrorCodes;
 import org.jinterop.dcom.common.JIException;
@@ -11,6 +12,7 @@ import org.openscada.opc.lib.da.*;
 
 import java.net.UnknownHostException;
 import java.util.*;
+import java.util.concurrent.Executors;
 
 /**
  * @Auther: 熊
@@ -23,34 +25,16 @@ public class OpcDAClient {
     public static String user = "Administrator";
     public static String password = "Hollysys";
     public static String clsId = "001AAAA6-FB54-4627-84B2-8777379E5868";
+    public static String progId = "Hollysys.HOLLiASiComm.1";
     public static String tag_prefix = "Channel1.Device1.";
     private Server server;
-    private String bakHost;
     private Group group = null;
     private Map<String, Item> groupItems = null;
 
     /**
      * 初始化连接信息
-     *
-     * @param host
-     * @param user
-     * @param password
-     * @param clsId
      */
-    public OpcDAClient(String host, String user, String password, String clsId) {
-        this.host = host;
-        this.user = user;
-        this.password = password;
-        this.clsId = clsId;
-    }
-
-    /**
-     * 设置备用服务地址
-     *
-     * @param bakHost
-     */
-    public void setBakHost(String bakHost) {
-        this.bakHost = bakHost;
+    public OpcDAClient() {
     }
 
     /**
@@ -66,7 +50,8 @@ public class OpcDAClient {
         ci.setUser(user);
         ci.setPassword(password);
         ci.setClsid(clsId);
-        server = new Server(ci, null);
+        ci.setProgId(progId);
+        server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
         try {
             server.connect();
         } catch (UnknownHostException e) {
@@ -77,7 +62,6 @@ public class OpcDAClient {
             log.error("opc 连接失败:", e);
             log.info("开始连接备用服务...");
             try {
-                ci.setHost(bakHost);
                 server = new Server(ci, null);
                 server.connect();
             } catch (Exception e2) {
@@ -125,7 +109,6 @@ public class OpcDAClient {
                 groupItems = group.addItems(items);
                 log.info("组建完成,开始查询数据...");
             }
-
             Set itemSet = new HashSet(groupItems.values());
             Item[] itemArr = new Item[itemSet.size()];
             itemSet.toArray(itemArr);
@@ -229,7 +212,8 @@ public class OpcDAClient {
     public static String[] tagList = new String[]{"AT_11001_AV", "AT_11002_AV", "AT_11003_AV", "AT_11004_AV", "AT_11005_AV", "AT_11006_AV", "AT_11007_AV", "AT_11008_AV", "AT_7101A_AV", "AT_7101B_AV", "AT_7101C_AV"};
 
     public static void run() {
-        OpcDAClient opcDAClient = new OpcDAClient(OpcDAClient.host, OpcDAClient.user, OpcDAClient.password, OpcDAClient.clsId);
+        // RedisUtils.set("test", 1);
+        OpcDAClient opcDAClient = new OpcDAClient();
         opcDAClient.connect();
         List<String> itemList = new ArrayList<>();
         for (String tag : tagList) {

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

@@ -1,6 +1,11 @@
 spring:
   application:
     name: platform-opc
+  redis:
+    password:
+    port: 6379
+    host: localhost
+    database: 1
 platform:
   slf4j:
     data: D://logs

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

@@ -1,6 +1,11 @@
 spring:
   application:
     name: platform-opc
+  redis:
+    password:
+    port: 6379
+    host: localhost
+    database: 2
 platform:
   slf4j:
     data: D://xajg

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

@@ -1,6 +1,11 @@
 spring:
   application:
     name: platform-opc
+  redis:
+    password:
+    port: 6379
+    host: localhost
+    database: 3
 platform:
   slf4j:
     data: D://xajg