|
@@ -1,11 +1,17 @@
|
|
|
package com.warewms.hailiang.init;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
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.common.exception.ServiceException;
|
|
|
+import com.warewms.common.utils.StringUtils;
|
|
|
import com.warewms.hailiang.config.PlcConfig;
|
|
|
import com.warewms.hailiang.config.PlcProperties;
|
|
|
+import com.warewms.hailiang.domain.Device;
|
|
|
+import com.warewms.hailiang.service.DeviceService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.core.annotation.Order;
|
|
@@ -13,8 +19,11 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -31,9 +40,12 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
@Resource
|
|
|
ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
|
|
|
- private HashMap<String, S7PLC> plcToolsMap = new HashMap<>();
|
|
|
+ @Resource
|
|
|
+ DeviceService deviceService;
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ScheduledExecutorService scheduledExecutorService;
|
|
|
|
|
|
@Override
|
|
|
public void run(String... args) throws Exception {
|
|
@@ -41,6 +53,10 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
log.info("plc初始化完成!");
|
|
|
}
|
|
|
|
|
|
+ private HashMap<String, S7PLC> plcToolsMap = new HashMap<>();
|
|
|
+
|
|
|
+ private ArrayList<String> reTryPlc = new ArrayList<>();
|
|
|
+
|
|
|
/**
|
|
|
* 初始化
|
|
|
*/
|
|
@@ -53,6 +69,7 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
s7PLC.connect();
|
|
|
if (s7PLC.checkConnected()){
|
|
|
plcToolsMap.put(plcConfig.getName(),s7PLC);
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new Device(plcConfig.getName(), "1"));
|
|
|
log.info("plc:{},ip:{},连接成功",plcConfig.getName(),plcConfig.getIp());
|
|
|
}
|
|
|
}catch (Exception e){
|
|
@@ -65,6 +82,7 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
}
|
|
|
|
|
|
public void retry(PlcConfig plcConfig) {
|
|
|
+ reTryPlc.add(plcConfig.getName());
|
|
|
threadPoolTaskExecutor.execute (() -> {
|
|
|
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
|
|
|
.retryIfResult(Boolean.FALSE::equals)
|
|
@@ -80,7 +98,10 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
s7PLC.connect();
|
|
|
if (s7PLC.checkConnected()){
|
|
|
plcToolsMap.put(plcConfig.getName(),s7PLC);
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new Device(plcConfig.getName(), "1"));
|
|
|
log.info("plc:{},ip:{},重试连接成功",plcConfig.getName(),plcConfig.getIp());
|
|
|
+ reTryPlc.remove(plcConfig.getName());
|
|
|
+ plcConnectsTheHeartbeat(plcConfig,s7PLC);
|
|
|
return true;
|
|
|
}
|
|
|
}catch (Exception e){
|
|
@@ -95,13 +116,42 @@ public class PlcConnectServiceRunner implements CommandLineRunner {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * plc连接心跳
|
|
|
+ * @param plcConfig
|
|
|
+ * @param s7PLC
|
|
|
+ */
|
|
|
+ private void plcConnectsTheHeartbeat(PlcConfig plcConfig,S7PLC s7PLC){
|
|
|
+ String heartbeatAdd = plcConfig.getHeartbeat();
|
|
|
+ if (StringUtils.isNotEmpty(heartbeatAdd)){
|
|
|
+ scheduledExecutorService.scheduleWithFixedDelay(()->{
|
|
|
+ if (!reTryPlc.contains(plcConfig.getName())){
|
|
|
+ try {
|
|
|
+ s7PLC.readBoolean(plcConfig.getHeartbeat());
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("设备:{},连接中断",plcConfig.getName());
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new Device(plcConfig.getName(), "2"));
|
|
|
+ retry(plcConfig);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },0,3,TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * @param key plc设备名
|
|
|
+ * @param pclName plc设备名
|
|
|
* @return
|
|
|
*/
|
|
|
- public S7PLC getPlcServer(String key) {
|
|
|
- return plcToolsMap.get(key);
|
|
|
+ public S7PLC getPlcServer(String pclName) {
|
|
|
+ S7PLC s7PLC = plcToolsMap.get(pclName);
|
|
|
+ if(ObjectUtil.isNull(s7PLC)){
|
|
|
+ throw new ServiceException("设备未连接");
|
|
|
+ }
|
|
|
+ if (!deviceService.IsConnect(pclName)){
|
|
|
+ throw new ServiceException("设备连接中断");
|
|
|
+ }
|
|
|
+ return plcToolsMap.get(pclName);
|
|
|
}
|
|
|
}
|