穿梭车连接
parent
82e4f55d48
commit
b6585973ca
@ -0,0 +1,108 @@
|
|||||||
|
package com.zhehekeji.web.service.shuttleCar;
|
||||||
|
|
||||||
|
|
||||||
|
import com.zhehekeji.common.util.FileUtil;
|
||||||
|
import com.zhehekeji.web.config.ConfigProperties;
|
||||||
|
import com.zhehekeji.web.service.PlcService;
|
||||||
|
import com.zhehekeji.web.service.ksec.KsecDecoder;
|
||||||
|
import com.zhehekeji.web.service.ksec.KsecInfo;
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
|
import io.netty.channel.Channel;
|
||||||
|
import io.netty.channel.EventLoopGroup;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class ShuttleNettyClient {
|
||||||
|
|
||||||
|
private static EventLoopGroup group = new NioEventLoopGroup();
|
||||||
|
@Resource
|
||||||
|
private PlcService plcService;
|
||||||
|
@Resource
|
||||||
|
private ConfigProperties configProperties;
|
||||||
|
|
||||||
|
|
||||||
|
private static Channel channel;
|
||||||
|
|
||||||
|
public void createClient(ConfigProperties.KSEC ksec) throws InterruptedException {
|
||||||
|
String lotnum = FileUtil.getText("lastLotnum");
|
||||||
|
if(lotnum != null){
|
||||||
|
KsecDecoder.setLastLotnum(lotnum);
|
||||||
|
}
|
||||||
|
if (StringUtils.isEmpty(ksec.getIp()) || ksec.getPort() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Bootstrap client = new Bootstrap();
|
||||||
|
client.group(group);
|
||||||
|
client.channel(NioSocketChannel.class);
|
||||||
|
KsecInfo heart = KsecInfo.heart();
|
||||||
|
client.handler(new ShuttleNettyFilter(heart, plcService,this));
|
||||||
|
// 连接服务端
|
||||||
|
|
||||||
|
channel = client.connect(ksec.getIp(), ksec.getPort()).sync().channel();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 断线重连 尝试 RECONNECT_NUM 次
|
||||||
|
*
|
||||||
|
* @param upId
|
||||||
|
*/
|
||||||
|
public void reconnect(Integer upId) {
|
||||||
|
Boolean isConnected = false;
|
||||||
|
int num = 0;
|
||||||
|
ConfigProperties.KSEC ksec = configProperties.getShuttleCar();
|
||||||
|
if (ksec == null) {
|
||||||
|
log.error("reconnect ,upPc is null ,id:{}", upId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(1500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重连最大次数
|
||||||
|
*/
|
||||||
|
while ((ksec.getReconnectNum() == -1 || num < ksec.getReconnectNum() ) && !isConnected) {
|
||||||
|
try {
|
||||||
|
createClient(ksec);
|
||||||
|
} catch (Exception e) {
|
||||||
|
//没连上 继续
|
||||||
|
log.error("reconnect error num:{}", num);
|
||||||
|
if(channel != null) {
|
||||||
|
channel.close();
|
||||||
|
}
|
||||||
|
num++;
|
||||||
|
try {
|
||||||
|
Thread.sleep(ksec.getReconnectInterval()*1000);
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
isConnected = true;
|
||||||
|
}
|
||||||
|
if (isConnected) {
|
||||||
|
log.info("plc reconnect success");
|
||||||
|
} else {
|
||||||
|
log.error("plc reconnect error .upPcId:{},reconnect num:{},ip:{},port:{}", upId, num, ksec.getIp(), ksec.getPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void write(KsecInfo ksecInfo){
|
||||||
|
if(channel != null){
|
||||||
|
channel.writeAndFlush(ksecInfo);
|
||||||
|
}else {
|
||||||
|
log.error(" no connected upPc");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue