Przeglądaj źródła

修改PLC连接包

zhifei 1 rok temu
rodzic
commit
bfb26edfc6

+ 4 - 4
pom.xml

@@ -43,7 +43,7 @@
         <knife4j.spring.version>3.0.3</knife4j.spring.version>
         <guava.version>23.0</guava.version>
         <guava.retry.version>2.0.0</guava.retry.version>
-        <HslCommunication.version>3.6.0</HslCommunication.version>
+        <iotCommunication.version>1.4.2</iotCommunication.version>
     </properties>
 
     <!-- 依赖声明 -->
@@ -205,9 +205,9 @@
             </dependency>
 
             <dependency>
-                <groupId>com.github.dathlin</groupId>
-                <artifactId>HslCommunication</artifactId>
-                <version>${HslCommunication.version}</version>
+                <groupId>com.github.xingshuangs</groupId>
+                <artifactId>iot-communication</artifactId>
+                <version>${iotCommunication.version}</version>
             </dependency>
 
         </dependencies>

+ 2 - 2
warewms-system/pom.xml

@@ -28,8 +28,8 @@
         </dependency>
 
         <dependency>
-            <groupId>com.github.dathlin</groupId>
-            <artifactId>HslCommunication</artifactId>
+            <groupId>com.github.xingshuangs</groupId>
+            <artifactId>iot-communication</artifactId>
         </dependency>
     </dependencies>
 

+ 4 - 4
warewms-system/src/main/java/com/warewms/hailiang/config/PlcConfig.java

@@ -1,6 +1,6 @@
 package com.warewms.hailiang.config;
 
-import HslCommunication.Profinet.Siemens.SiemensPLCS;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import lombok.Data;
 
 /**
@@ -23,10 +23,10 @@ public class PlcConfig {
 
     private byte rack;
 
-    private SiemensPLCS siemensPLCS;
+    private EPlcType ePlcType;
 
-    public void setSiemensPLCS(String  siemensPLCS) {
-        this.siemensPLCS = SiemensPLCS.valueOf(siemensPLCS);
+    public void setSiemensPLCS(String  ePlcType) {
+        this.ePlcType = EPlcType.valueOf(ePlcType);
     }
 
 }

+ 37 - 23
warewms-system/src/main/java/com/warewms/hailiang/init/PlcConnectServiceRunner.java

@@ -2,12 +2,14 @@ package com.warewms.hailiang.init;
 
 
 import com.github.rholder.retry.*;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
 import com.warewms.hailiang.config.PlcConfig;
 import com.warewms.hailiang.config.PlcProperties;
-import com.warewms.hailiang.util.HslTools;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.core.annotation.Order;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
@@ -26,7 +28,12 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
     @Resource
     private PlcProperties plcProperties;
 
-    private HashMap<String, HslTools> plcToolsMap = new HashMap<>();
+    @Resource
+    ThreadPoolTaskExecutor threadPoolTaskExecutor;
+
+    private HashMap<String, S7PLC> plcToolsMap = new HashMap<>();
+
+
 
     @Override
     public void run(String... args) throws Exception {
@@ -41,15 +48,16 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
         for (PlcConfig plcConfig : plcProperties.getPlcList()) {
             if (plcConfig.getEnable()) {
                 log.info("初始化:{},ip:{}", plcConfig.getName(), plcConfig.getIp());
-                HslTools hslTools = new HslTools(plcConfig);
-                Thread.sleep(5000);
-                if (hslTools.getSuccess()) {
-                    log.info("plc连接成功:{},ip:{}", plcConfig.getName(), plcConfig.getIp());
-                    plcToolsMap.put(plcConfig.getName(), hslTools);
-                } else {
-                    new Thread(() -> {
-                        retry(plcConfig);
-                    }).start();
+                try {
+                    S7PLC s7PLC = new S7PLC(plcConfig.getEPlcType(), plcConfig.getIp());
+                    s7PLC.connect();
+                    if (s7PLC.checkConnected()){
+                        plcToolsMap.put(plcConfig.getName(),s7PLC);
+                        log.info("plc:{},ip:{},连接成功",plcConfig.getName(),plcConfig.getIp());
+                    }
+                }catch (Exception e){
+                    log.info("plc:{},ip:{},连接失败",plcConfig.getName(),plcConfig.getIp());
+                    retry(plcConfig);
                 }
             }
         }
@@ -57,32 +65,38 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
     }
 
     public void retry(PlcConfig plcConfig) {
-        new Thread(() -> {
+        threadPoolTaskExecutor.execute (() -> {
             Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                     .retryIfResult(Boolean.FALSE::equals)
                     .retryIfExceptionOfType(Exception.class)
-                    .withStopStrategy(StopStrategies.stopAfterAttempt(5))
-                    .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
+                    .withStopStrategy(StopStrategies.neverStop())
+                    .withWaitStrategy(WaitStrategies.fixedWait(7, TimeUnit.SECONDS))
                     .build();
             try {
                 retryer.call(() -> {
                     log.info("重试:{},ip:{}", plcConfig.getName(), plcConfig.getIp());
-                    HslTools hslTools = new HslTools(plcConfig);
-                    Thread.sleep(5000);
-                    if (hslTools.getSuccess()) {
-                        log.info("plc连接成功:{},ip:{}", plcConfig.getName(), plcConfig.getIp());
-                        plcToolsMap.put(plcConfig.getName(), hslTools);
-                        return true;
+                    try {
+                        S7PLC s7PLC = new S7PLC(plcConfig.getEPlcType(), plcConfig.getIp());
+                        s7PLC.connect();
+                        if (s7PLC.checkConnected()){
+                            plcToolsMap.put(plcConfig.getName(),s7PLC);
+                            log.info("plc:{},ip:{},重试连接成功",plcConfig.getName(),plcConfig.getIp());
+                            return true;
+                        }
+                    }catch (Exception e){
+                        log.info("plc:{},ip:{},重试连接失败,meg:{}",plcConfig.getName(),plcConfig.getIp(),e.getMessage());
+                        return false;
                     }
                     return false;
                 });
             } catch (RetryException | ExecutionException e) {
                 e.printStackTrace();
             }
-        }).start();
+        });
     }
+    
 
-    public HslTools getPlcServer(String key){
-      return  plcToolsMap.get(key);
+    public S7PLC getPlcServer(String key) {
+        return plcToolsMap.get(key);
     }
 }

+ 0 - 147
warewms-system/src/main/java/com/warewms/hailiang/util/HslTools.java

@@ -1,147 +0,0 @@
-package com.warewms.hailiang.util;
-
-import HslCommunication.Core.Types.OperateResult;
-import HslCommunication.Core.Types.OperateResultExOne;
-import HslCommunication.Profinet.Siemens.SiemensPLCS;
-import HslCommunication.Profinet.Siemens.SiemensS7Net;
-import com.warewms.common.exception.ServiceException;
-import com.warewms.hailiang.config.PlcConfig;
-import lombok.extern.slf4j.Slf4j;
-
-import java.nio.charset.StandardCharsets;
-
-@Slf4j
-public class HslTools{
-
-    private SiemensS7Net siemensS7Net;
-    private Boolean IsSuccess;
-
-    public Boolean getSuccess() {
-        return IsSuccess;
-    }
-
-    public HslTools(PlcConfig plcConfig) {
-        log.info("正在创建连接:{},ip:{}",plcConfig.getSiemensPLCS(),plcConfig.getIp());
-        siemensS7Net = new SiemensS7Net(plcConfig.getSiemensPLCS(), plcConfig.getIp());
-        siemensS7Net.setSlot(plcConfig.getSlot());
-        siemensS7Net.setRack(plcConfig.getRack());
-        OperateResult operateResult = siemensS7Net.ConnectServer();
-        this.IsSuccess = operateResult.IsSuccess;
-        if (!this.IsSuccess){
-            log.info("连接结果:{}--{}--{}",operateResult.IsSuccess,operateResult.Message,operateResult.ErrorCode);
-        }
-    }
-
-
-    public OperateResult closeConn() {
-        OperateResult result = OperateResult.CreateSuccessResult();
-        try {
-            result = siemensS7Net.ConnectClose();
-        } catch (Exception e) {
-
-        }
-        siemensS7Net = null;
-        return result;
-    }
-
-    public boolean writeInt(String address, Integer value) {
-        OperateResult result = siemensS7Net.Write(address, value);
-        log.info(result.Message);
-        if (!result.IsSuccess) {
-            closeConn();
-            throw new ServiceException(String.format("写入数据失败,db:%s,数据:%d,错误信息:%s", address, value, result.Message));
-        }
-        return result.IsSuccess;
-    }
-
-    public boolean writeShort(String address, short value) {
-        OperateResult result = siemensS7Net.Write(address, value);
-        log.info(result.Message);
-        if (!result.IsSuccess) {
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
-        } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%d", address, value));
-        }
-        return result.IsSuccess;
-    }
-
-    public boolean writeBoolean(String address, Boolean value) {
-        OperateResult result = siemensS7Net.Write(address, value);
-        if (!result.IsSuccess) {
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, address, result.Message));
-        } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%b", address, value));
-        }
-        return result.IsSuccess;
-    }
-
-    public boolean writeString(String address, String value) {
-        OperateResult result = siemensS7Net.Write(address, value.getBytes(StandardCharsets.US_ASCII));
-        if (!result.IsSuccess) {
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
-        } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
-        }
-        return result.IsSuccess;
-    }
-
-    public boolean writeStringStr(String address, String value) {
-        OperateResult result = siemensS7Net.Write(address, value);
-        if (!result.IsSuccess) {
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
-        } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
-        }
-        return result.IsSuccess;
-    }
-
-    public String getString(String address) {
-        OperateResultExOne<String> result = siemensS7Net.ReadString(address);
-        if (!result.IsSuccess) {
-            log.error(String.format(String.format("String类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
-            return "";
-        } else {
-            return result.Content;
-        }
-    }
-
-    public boolean getBool(String address) {
-        OperateResultExOne<Boolean> result = siemensS7Net.ReadBool(address);
-        if (!result.IsSuccess) {
-            log.error(String.format(String.format("Bool类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
-            return false;
-        } else {
-            return result.Content;
-        }
-    }
-
-    public int getInt(String address) {
-        OperateResultExOne<Integer> result = siemensS7Net.ReadInt32(address);
-        if (!result.IsSuccess) {
-            log.error(String.format(String.format("int类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
-            return 0;
-        } else {
-            return result.Content;
-        }
-    }
-
-    public Float getFlot(String address) {
-        OperateResultExOne<Float> result = siemensS7Net.ReadFloat(address);
-        if (!result.IsSuccess) {
-            log.error(String.format(String.format("flot类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
-            return 0F;
-        } else {
-            return result.Content;
-        }
-    }
-
-    public Double getDabble(String address) {
-        OperateResultExOne<Double> result = siemensS7Net.ReadDouble(address);
-        if (!result.IsSuccess) {
-            log.error(String.format(String.format("dabble类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
-            return 0D;
-        } else {
-            return result.Content;
-        }
-    }
-}