소스 검색

PCL对接添加重连机制

zhifei 1 년 전
부모
커밋
4634a003c4

+ 8 - 8
ruoyi-admin/src/main/resources/logback-test.xml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
     <!-- 日志存放路径 -->
-	<property name="log.path" value="/Users/gizmo/JavaProjectsFiles/Logs/ruoyi/logs" />
+	<property name="log.path" value="../logs" />
     <!-- 日志输出格式 -->
 	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
 
@@ -11,7 +11,7 @@
 			<pattern>${log.pattern}</pattern>
 		</encoder>
 	</appender>
-	
+
 	<!-- 系统日志输出 -->
 	<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
 	    <file>${log.path}/sys-info.log</file>
@@ -34,7 +34,7 @@
             <onMismatch>DENY</onMismatch>
         </filter>
 	</appender>
-	
+
 	<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
 	    <file>${log.path}/sys-error.log</file>
         <!-- 循环政策:基于时间创建日志文件 -->
@@ -56,7 +56,7 @@
             <onMismatch>DENY</onMismatch>
         </filter>
     </appender>
-	
+
 	<!-- 用户访问日志输出  -->
     <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
 		<file>${log.path}/sys-user.log</file>
@@ -70,7 +70,7 @@
             <pattern>${log.pattern}</pattern>
         </encoder>
     </appender>
-	
+
 	<!-- 系统模块日志级别控制  -->
 	<logger name="com.ruoyi" level="info" />
 	<!-- Spring日志级别控制  -->
@@ -79,15 +79,15 @@
 	<root level="info">
 		<appender-ref ref="console" />
 	</root>
-	
+
 	<!--系统操作日志-->
     <root level="info">
         <appender-ref ref="file_info" />
         <appender-ref ref="file_error" />
     </root>
-	
+
 	<!--系统用户操作日志-->
     <logger name="sys-user" level="info">
         <appender-ref ref="sys-user"/>
     </logger>
-</configuration> 
+</configuration>

+ 8 - 8
ruoyi-admin/src/main/resources/logback.xml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
     <!-- 日志存放路径 -->
-	<property name="log.path" value="../../../logs/ruoyi/logs" />
+	<property name="log.path" value="../logs" />
     <!-- 日志输出格式 -->
 	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
 
@@ -11,7 +11,7 @@
 			<pattern>${log.pattern}</pattern>
 		</encoder>
 	</appender>
-	
+
 	<!-- 系统日志输出 -->
 	<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
 	    <file>${log.path}/sys-info.log</file>
@@ -34,7 +34,7 @@
             <onMismatch>DENY</onMismatch>
         </filter>
 	</appender>
-	
+
 	<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
 	    <file>${log.path}/sys-error.log</file>
         <!-- 循环政策:基于时间创建日志文件 -->
@@ -56,7 +56,7 @@
             <onMismatch>DENY</onMismatch>
         </filter>
     </appender>
-	
+
 	<!-- 用户访问日志输出  -->
     <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
 		<file>${log.path}/sys-user.log</file>
@@ -70,7 +70,7 @@
             <pattern>${log.pattern}</pattern>
         </encoder>
     </appender>
-	
+
 	<!-- 系统模块日志级别控制  -->
 	<logger name="com.ruoyi" level="info" />
 	<!-- Spring日志级别控制  -->
@@ -79,15 +79,15 @@
 	<root level="info">
 		<appender-ref ref="console" />
 	</root>
-	
+
 	<!--系统操作日志-->
     <root level="info">
         <appender-ref ref="file_info" />
         <appender-ref ref="file_error" />
     </root>
-	
+
 	<!--系统用户操作日志-->
     <logger name="sys-user" level="info">
         <appender-ref ref="sys-user"/>
     </logger>
-</configuration> 
+</configuration>

+ 0 - 2
warewms-ams/src/main/java/com/ruoyi/ams/MaGang02/service/PlcCallAgvService.java

@@ -318,7 +318,6 @@ public class PlcCallAgvService {
         return AjaxResult.success();
     }
 
-
     /**
      * 合格品左请求下料
      *
@@ -391,7 +390,6 @@ public class PlcCallAgvService {
         return AjaxResult.success();
     }
 
-
     /**
      * 合格品右请求下料
      *

+ 16 - 0
warewms-ams/src/main/java/com/ruoyi/ams/task/service/impl/WcsTaskServiceImpl.java

@@ -399,6 +399,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品左下料结束");
                         }
                         //不合格品左空托上料结束
                         if (wcsTask.getLocationTo().equals("100000")){
@@ -414,6 +415,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品左下料结束");
                         }
                         //不合格品右下料结束
                         if (wcsTask.getLocationFrom().equals("100002")){
@@ -429,6 +431,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品右下料结束");
                         }
                         //不合格品右空托上料结束
                         if (wcsTask.getLocationTo().equals("100002")){
@@ -444,6 +447,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品右上空托结束");
                         }
                         //合格品左下料结束
                         if (wcsTask.getLocationFrom().equals("100001")){
@@ -459,6 +463,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品左下料结束");
                         }
                         //合格品左空托上料结束
                         if (wcsTask.getLocationTo().equals("100001")){
@@ -474,6 +479,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品左上空托结束");
                         }
                         //合格品右下料结束
                         if (wcsTask.getLocationFrom().equals("100003")){
@@ -489,6 +495,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品右下料结束");
                         }
                         //合格品右空托上料结束
                         if (wcsTask.getLocationTo().equals("100003")){
@@ -504,6 +511,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品右上空托结束");
                         }
                         wcsTask.setEndTime(new Date());
                         // 解锁终点库位
@@ -569,6 +577,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品左下料结束");
                         }
                         //不合格品左空托上料结束
                         if (wcsTask.getLocationTo().equals("100000")){
@@ -584,6 +593,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品左上空托结束");
                         }
                         //不合格品右下料结束
                         if (wcsTask.getLocationFrom().equals("100002")){
@@ -599,6 +609,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品右下料结束");
                         }
                         //不合格品右空托上料结束
                         if (wcsTask.getLocationTo().equals("100002")){
@@ -614,6 +625,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("不合格品右上空托结束");
                         }
                         //合格品左下料结束
                         if (wcsTask.getLocationFrom().equals("100001")){
@@ -629,6 +641,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品左下料结束");
                         }
                         //合格品左空托上料结束
                         if (wcsTask.getLocationTo().equals("100001")){
@@ -644,6 +657,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品左上空托结束");
                         }
                         //合格品右下料结束
                         if (wcsTask.getLocationFrom().equals("100003")){
@@ -659,6 +673,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品右下料结束");
                         }
                         //合格品右空托上料结束
                         if (wcsTask.getLocationTo().equals("100003")){
@@ -674,6 +689,7 @@ public class WcsTaskServiceImpl implements IWcsTaskService {
                                     e.printStackTrace();
                                 }
                             }).start();
+                            log.info("合格品右上空托结束");
                         }
                         wcsTask.setEndTime(new Date());
                         // 解锁终点库位

+ 12 - 0
warewms-hard/pom.xml

@@ -35,5 +35,17 @@
             <artifactId>HslCommunication</artifactId>
             <version>2.0.2</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>23.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.github.rholder</groupId>
+            <artifactId>guava-retrying</artifactId>
+            <version>2.0.0</version>
+        </dependency>
     </dependencies>
 </project>

+ 85 - 24
warewms-hard/src/main/java/com/ruoyi/hard/plc/HslTools.java

@@ -4,38 +4,91 @@ import HslCommunication.Core.Types.OperateResult;
 import HslCommunication.Core.Types.OperateResultExOne;
 import HslCommunication.Profinet.Siemens.SiemensPLCS;
 import HslCommunication.Profinet.Siemens.SiemensS7Net;
-import com.ruoyi.common.exception.ServiceException;
+import com.github.rholder.retry.*;
 import lombok.extern.slf4j.Slf4j;
 
 import java.nio.charset.StandardCharsets;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 @Slf4j
 public class HslTools {
 
     private SiemensS7Net siemensS7Net;
-    private String ip;
+
+    private Boolean isConnect = false;
 
     private HslTools(String ip) {
         siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, ip);
         OperateResult operateResult = siemensS7Net.ConnectServer();
         if(operateResult.IsSuccess)
         {
+            isConnect = true;
             log.info("连接成功");
         }
         else
         {
+            isConnect = false;
             log.info("连接失败");
         }
-        this.ip = ip;
     }
 
-    private static final HslTools hslTools= new HslTools("10.95.8.66");
+    private static HslTools hslTools= new HslTools("10.95.8.66");
 
     //静态工厂方法
     public static HslTools getInstance() {
         return hslTools;
     }
 
+    private void retryConnect(){
+        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
+                .retryIfResult(Boolean.FALSE::equals)
+                .retryIfExceptionOfType(Exception.class)
+                .withStopStrategy(StopStrategies.stopAfterAttempt(10))
+                .withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS))
+                .build();
+        try {
+            retryer.call(() -> {
+                log.info("重试连接");
+                try {
+                    hslTools = new HslTools("10.95.8.66");
+                    return isConnect;
+                } catch (Exception e) {
+                    return false;
+                }
+            });
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        } catch (RetryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    private void retry(String DBName,boolean DBValue) {
+        try {
+            Thread.sleep(2000L);
+            Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
+                    .retryIfResult(Boolean.FALSE::equals)
+                    .retryIfExceptionOfType(Exception.class)
+                    .withStopStrategy(StopStrategies.stopAfterAttempt(10))
+                    .withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS))
+                    .build();
+            retryer.call(() -> {
+                log.info("重试:{},value:{}", DBName, DBValue);
+                try {
+                    return siemensS7Net.Write(DBName, DBValue).IsSuccess;
+                } catch (Exception e) {
+                    log.info("修改失败:{},{},msg:{}", DBName, DBValue, e.getMessage());
+                    return false;
+                }
+            });
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        } catch (RetryException e) {
+            throw new RuntimeException(e);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
 
     public OperateResult closeConn() {
         OperateResult result = OperateResult.CreateSuccessResult();
@@ -52,8 +105,9 @@ public class HslTools {
         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));
+            log.error("PLC connection is lost!");
+            log.info("写入数据失败,db:{},数据:{},错误信息:{}", address, value, result.Message);
+            retryConnect();
         }
         return result.IsSuccess;
     }
@@ -62,10 +116,11 @@ public class HslTools {
         OperateResult result = siemensS7Net.Write(address, value);
         log.info(result.Message);
         if (!result.IsSuccess) {
-            closeConn();
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
+            log.error("PLC connection is lost!");
+            log.info("写入数据失败,db:{},错误信息:{}", address, result.Message);
+            retryConnect();
         } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%d", address, value));
+            log.info(String.format("写入数据完成,db:{},数据:{}", address, value));
         }
         return result.IsSuccess;
     }
@@ -73,10 +128,11 @@ public class HslTools {
     public boolean writeBoolean(String address, Boolean value) {
         OperateResult result = siemensS7Net.Write(address, value);
         if (!result.IsSuccess) {
-            closeConn();
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, address, result.Message));
+            log.info("写入数据失败,db:{},错误信息:{}", address, address, result.Message);
+            log.error("PLC connection is lost!");
+            retry(address,value);
         } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%b", address, value));
+            log.info(String.format("写入数据完成,db:{},数据:%b", address, value));
         }
         return result.IsSuccess;
     }
@@ -84,10 +140,11 @@ public class HslTools {
     public boolean writeString(String address, String value) {
         OperateResult result = siemensS7Net.Write(address, value.getBytes(StandardCharsets.US_ASCII));
         if (!result.IsSuccess) {
-            closeConn();
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
+            log.info("写入数据失败,db:{},错误信息:{}", address, result.Message);
+            log.error("PLC connection is lost!");
+            retryConnect();
         } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
+            log.info(String.format("写入数据完成,db:{},数据:{}", address, value));
         }
         return result.IsSuccess;
     }
@@ -95,10 +152,11 @@ public class HslTools {
     public boolean writeStringStr(String address, String value) {
         OperateResult result = siemensS7Net.Write(address, value);
         if (!result.IsSuccess) {
-            closeConn();
-            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
+            log.info("写入数据失败,db:{},错误信息:{}", address, result.Message);
+            log.error("PLC connection is lost!");
+            retryConnect();
         } else {
-            log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
+            log.info(String.format("写入数据完成,db:{},数据:{}", address, value));
         }
         return result.IsSuccess;
     }
@@ -106,8 +164,9 @@ public class HslTools {
     public String getString(String address) {
         OperateResultExOne<String> result = siemensS7Net.ReadString(address);
         if (!result.IsSuccess) {
-            closeConn();
-            log.error(String.format(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message)));
+            log.error(String.format(String.format("写入数据失败,db:{},错误信息:{}", address, result.Message)));
+            log.error("PLC connection is lost!");
+            retryConnect();
             return "";
         } else {
             return result.Content;
@@ -117,8 +176,9 @@ public class HslTools {
     public boolean getBool(String address) {
         OperateResultExOne<Boolean> result = siemensS7Net.ReadBool(address);
         if (!result.IsSuccess) {
-            closeConn();
-            log.error(String.format(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message)));
+            log.error(String.format(String.format("写入数据失败,db:{},错误信息:{}", address, result.Message)));
+            log.error("PLC connection is lost!");
+            retryConnect();
             return false;
         } else {
             return result.Content;
@@ -128,8 +188,9 @@ public class HslTools {
     public int getInt(String address) {
         OperateResultExOne<Integer> result = siemensS7Net.ReadInt32(address);
         if (!result.IsSuccess) {
-            closeConn();
-            log.error(String.format(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message)));
+            log.error(String.format(String.format("写入数据失败,db:{},错误信息:{}", address, result.Message)));
+            log.error("PLC connection is lost!");
+            retryConnect();
             return 0;
         } else {
             return result.Content;