|
@@ -4,38 +4,91 @@ import HslCommunication.Core.Types.OperateResult;
|
|
import HslCommunication.Core.Types.OperateResultExOne;
|
|
import HslCommunication.Core.Types.OperateResultExOne;
|
|
import HslCommunication.Profinet.Siemens.SiemensPLCS;
|
|
import HslCommunication.Profinet.Siemens.SiemensPLCS;
|
|
import HslCommunication.Profinet.Siemens.SiemensS7Net;
|
|
import HslCommunication.Profinet.Siemens.SiemensS7Net;
|
|
-import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
|
+import com.github.rholder.retry.*;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
public class HslTools {
|
|
public class HslTools {
|
|
|
|
|
|
private SiemensS7Net siemensS7Net;
|
|
private SiemensS7Net siemensS7Net;
|
|
- private String ip;
|
|
|
|
|
|
+
|
|
|
|
+ private Boolean isConnect = false;
|
|
|
|
|
|
private HslTools(String ip) {
|
|
private HslTools(String ip) {
|
|
siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, ip);
|
|
siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, ip);
|
|
OperateResult operateResult = siemensS7Net.ConnectServer();
|
|
OperateResult operateResult = siemensS7Net.ConnectServer();
|
|
if(operateResult.IsSuccess)
|
|
if(operateResult.IsSuccess)
|
|
{
|
|
{
|
|
|
|
+ isConnect = true;
|
|
log.info("连接成功");
|
|
log.info("连接成功");
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
|
|
+ isConnect = false;
|
|
log.info("连接失败");
|
|
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() {
|
|
public static HslTools getInstance() {
|
|
return hslTools;
|
|
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() {
|
|
public OperateResult closeConn() {
|
|
OperateResult result = OperateResult.CreateSuccessResult();
|
|
OperateResult result = OperateResult.CreateSuccessResult();
|
|
@@ -52,8 +105,9 @@ public class HslTools {
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
log.info(result.Message);
|
|
log.info(result.Message);
|
|
if (!result.IsSuccess) {
|
|
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;
|
|
return result.IsSuccess;
|
|
}
|
|
}
|
|
@@ -62,10 +116,11 @@ public class HslTools {
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
log.info(result.Message);
|
|
log.info(result.Message);
|
|
if (!result.IsSuccess) {
|
|
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 {
|
|
} else {
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%d", address, value));
|
|
|
|
|
|
+ log.info(String.format("写入数据完成,db:{},数据:{}", address, value));
|
|
}
|
|
}
|
|
return result.IsSuccess;
|
|
return result.IsSuccess;
|
|
}
|
|
}
|
|
@@ -73,10 +128,11 @@ public class HslTools {
|
|
public boolean writeBoolean(String address, Boolean value) {
|
|
public boolean writeBoolean(String address, Boolean value) {
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
if (!result.IsSuccess) {
|
|
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 {
|
|
} else {
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%b", address, value));
|
|
|
|
|
|
+ log.info(String.format("写入数据完成,db:{},数据:%b", address, value));
|
|
}
|
|
}
|
|
return result.IsSuccess;
|
|
return result.IsSuccess;
|
|
}
|
|
}
|
|
@@ -84,10 +140,11 @@ public class HslTools {
|
|
public boolean writeString(String address, String value) {
|
|
public boolean writeString(String address, String value) {
|
|
OperateResult result = siemensS7Net.Write(address, value.getBytes(StandardCharsets.US_ASCII));
|
|
OperateResult result = siemensS7Net.Write(address, value.getBytes(StandardCharsets.US_ASCII));
|
|
if (!result.IsSuccess) {
|
|
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 {
|
|
} else {
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
|
|
|
|
|
|
+ log.info(String.format("写入数据完成,db:{},数据:{}", address, value));
|
|
}
|
|
}
|
|
return result.IsSuccess;
|
|
return result.IsSuccess;
|
|
}
|
|
}
|
|
@@ -95,10 +152,11 @@ public class HslTools {
|
|
public boolean writeStringStr(String address, String value) {
|
|
public boolean writeStringStr(String address, String value) {
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
OperateResult result = siemensS7Net.Write(address, value);
|
|
if (!result.IsSuccess) {
|
|
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 {
|
|
} else {
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
|
|
|
|
|
|
+ log.info(String.format("写入数据完成,db:{},数据:{}", address, value));
|
|
}
|
|
}
|
|
return result.IsSuccess;
|
|
return result.IsSuccess;
|
|
}
|
|
}
|
|
@@ -106,8 +164,9 @@ public class HslTools {
|
|
public String getString(String address) {
|
|
public String getString(String address) {
|
|
OperateResultExOne<String> result = siemensS7Net.ReadString(address);
|
|
OperateResultExOne<String> result = siemensS7Net.ReadString(address);
|
|
if (!result.IsSuccess) {
|
|
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 "";
|
|
return "";
|
|
} else {
|
|
} else {
|
|
return result.Content;
|
|
return result.Content;
|
|
@@ -117,8 +176,9 @@ public class HslTools {
|
|
public boolean getBool(String address) {
|
|
public boolean getBool(String address) {
|
|
OperateResultExOne<Boolean> result = siemensS7Net.ReadBool(address);
|
|
OperateResultExOne<Boolean> result = siemensS7Net.ReadBool(address);
|
|
if (!result.IsSuccess) {
|
|
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;
|
|
return false;
|
|
} else {
|
|
} else {
|
|
return result.Content;
|
|
return result.Content;
|
|
@@ -128,8 +188,9 @@ public class HslTools {
|
|
public int getInt(String address) {
|
|
public int getInt(String address) {
|
|
OperateResultExOne<Integer> result = siemensS7Net.ReadInt32(address);
|
|
OperateResultExOne<Integer> result = siemensS7Net.ReadInt32(address);
|
|
if (!result.IsSuccess) {
|
|
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;
|
|
return 0;
|
|
} else {
|
|
} else {
|
|
return result.Content;
|
|
return result.Content;
|