|
@@ -10,8 +10,6 @@ import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.system.config.PlcConfig;
|
|
|
import com.ruoyi.system.config.PlcProperties;
|
|
|
-import com.ruoyi.system.enums.PLCConnectNameEnum;
|
|
|
-import com.ruoyi.system.enums.PLCEnum;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.core.annotation.Order;
|
|
@@ -23,6 +21,7 @@ import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
+import java.util.concurrent.ScheduledFuture;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -44,6 +43,8 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
@Resource
|
|
|
private ScheduledExecutorService scheduledExecutorService;
|
|
|
|
|
|
+ private HashMap<String, ScheduledFuture<?>> heartbeatThreads = new HashMap<>();
|
|
|
+
|
|
|
@Override
|
|
|
public void run(String... args) throws Exception {
|
|
|
initConnect();
|
|
@@ -67,6 +68,7 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
if (s7PLC.checkConnected()){
|
|
|
plcToolsMap.put(plcConfig.getName(),s7PLC);
|
|
|
log.info("plc:{},ip:{},The connection was successful",plcConfig.getName(),plcConfig.getIp());
|
|
|
+ plcConnectsTheHeartbeat(plcConfig,s7PLC);
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
log.info("plc:{},ip:{},Connection failed",plcConfig.getName(),plcConfig.getIp());
|
|
@@ -78,8 +80,12 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
}
|
|
|
|
|
|
public void retry(PlcConfig plcConfig) {
|
|
|
- SpringUtil.getApplicationContext().publishEvent(new DeviceLog(plcConfig.getName(),"PLC设备已恢复连接,请联系管理员","2"));
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new DeviceLog(plcConfig.getName(),"PLC设备已断开连接,请联系管理员","2"));
|
|
|
reTryPlc.add(plcConfig.getName());
|
|
|
+ ScheduledFuture<?> scheduledFuture = heartbeatThreads.get(plcConfig.getName());
|
|
|
+ if (ObjectUtil.isNotNull(scheduledFuture)){
|
|
|
+ scheduledFuture.cancel(true);
|
|
|
+ }
|
|
|
threadPoolTaskExecutor.execute (() -> {
|
|
|
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
|
|
|
.retryIfResult(Boolean.FALSE::equals)
|
|
@@ -95,7 +101,6 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
s7PLC.connect();
|
|
|
if (s7PLC.checkConnected()){
|
|
|
SpringUtil.getApplicationContext().publishEvent(new DeviceLog(plcConfig.getName(),"PLC设备已恢复连接","1"));
|
|
|
-
|
|
|
plcToolsMap.put(plcConfig.getName(),s7PLC);
|
|
|
log.info("plc:{},ip:{},Retry the connection succeeded",plcConfig.getName(),plcConfig.getIp());
|
|
|
reTryPlc.remove(plcConfig.getName());
|
|
@@ -120,18 +125,19 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
* @param s7PLC
|
|
|
*/
|
|
|
private void plcConnectsTheHeartbeat(PlcConfig plcConfig,S7PLC s7PLC){
|
|
|
- String heartbeatAdd = plcConfig.getHeartbeat();
|
|
|
- if (StringUtils.isNotEmpty(heartbeatAdd)){
|
|
|
- scheduledExecutorService.scheduleWithFixedDelay(()->{
|
|
|
- if (!reTryPlc.contains(plcConfig.getName())){
|
|
|
+ if (StringUtils.isNotEmpty( plcConfig.getHeartbeat())){
|
|
|
+ ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
|
|
+ if (!reTryPlc.contains(plcConfig.getName())) {
|
|
|
try {
|
|
|
- s7PLC.readByte(plcConfig.getHeartbeat());
|
|
|
- }catch (Exception e){
|
|
|
- log.error("device:{},The connection is lost",plcConfig.getName());
|
|
|
+ byte b = s7PLC.readByte(plcConfig.getHeartbeat());
|
|
|
+ s7PLC.writeByte(plcConfig.getConfirmTheStatus(), b);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("device:{},The connection is lost", plcConfig.getName());
|
|
|
retry(plcConfig);
|
|
|
}
|
|
|
}
|
|
|
- },0,3,TimeUnit.SECONDS);
|
|
|
+ }, 0, 1, TimeUnit.SECONDS);
|
|
|
+ heartbeatThreads.put(plcConfig.getName(),scheduledFuture);
|
|
|
}
|
|
|
}
|
|
|
|