普天协议对接
parent
4f0fc140a7
commit
699ad94a9f
@ -0,0 +1,41 @@
|
||||
package com.zhehekeji.web.service.putian;
|
||||
|
||||
import com.zhehekeji.web.service.PlcService;
|
||||
import com.zhehekeji.web.service.ksec.*;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 普天编解码和心跳的设置
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public class PTFilter extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
private PlcService plcService;
|
||||
|
||||
private PuTianNettyClient nettyClient;
|
||||
|
||||
public PTFilter(PlcService plcService, PuTianNettyClient nettyClient){
|
||||
this.plcService = plcService;
|
||||
this.nettyClient = nettyClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline ph = ch.pipeline();
|
||||
//30秒发一次心跳
|
||||
ph.addLast(new IdleStateHandler(0, 30, 0, TimeUnit.SECONDS));
|
||||
ByteBuf byteBuf = Unpooled.copiedBuffer(">".getBytes());
|
||||
ph.addLast(new PTDecoder(1000,false,true));
|
||||
ph.addLast(new PtEncoder());
|
||||
ph.addLast(new PTNettyHandler(nettyClient));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package com.zhehekeji.web.service.putian;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 普天发送TCP数据
|
||||
*/
|
||||
public class PtEncoder extends MessageToByteEncoder<PTData> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext channelHandlerContext, PTData data, ByteBuf byteBuf) throws Exception {
|
||||
String body = data.toString();
|
||||
byteBuf.writeBytes(body.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,95 @@
|
||||
package com.zhehekeji.web.service.putian;
|
||||
|
||||
import com.zhehekeji.web.config.ConfigProperties;
|
||||
import com.zhehekeji.web.service.PlcService;
|
||||
import com.zhehekeji.web.service.client.Decoder;
|
||||
import com.zhehekeji.web.service.client.Encoder;
|
||||
import com.zhehekeji.web.service.ksec.KescFilter;
|
||||
import com.zhehekeji.web.service.ksec.KsecInfo;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
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 PuTianNettyClient {
|
||||
|
||||
private static EventLoopGroup group = new NioEventLoopGroup();
|
||||
@Resource
|
||||
private PlcService plcService;
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
|
||||
/**
|
||||
* 重连最大次数
|
||||
*/
|
||||
private static int RECONNECT_NUM = 10;
|
||||
|
||||
private static Channel channel;
|
||||
|
||||
public void createClient(ConfigProperties.KSEC ksec) throws InterruptedException {
|
||||
if (StringUtils.isEmpty(ksec.getIp()) || ksec.getPort() == null) {
|
||||
return;
|
||||
}
|
||||
Bootstrap client = new Bootstrap();
|
||||
client.group(group);
|
||||
client.channel(NioSocketChannel.class);
|
||||
client.handler(new PTNettyHandler(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.getKsec();
|
||||
if (ksec == null) {
|
||||
log.error("reconnect ,upPc is null ,id:{}", upId);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while (num < RECONNECT_NUM && !isConnected) {
|
||||
try {
|
||||
createClient(ksec);
|
||||
} catch (Exception e) {
|
||||
//没连上 继续
|
||||
log.error("reconnect error num:{}", num);
|
||||
num++;
|
||||
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