package com.ld.igds.protocol.es.dlt645.client;
|
|
import com.ld.igds.protocol.es.dlt645.analysis.AnalysisService;
|
import com.ld.igds.util.BytesUtil;
|
import com.ld.igds.util.SpringUtil;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.util.internal.logging.InternalLogger;
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
import org.springframework.stereotype.Component;
|
import java.net.InetSocketAddress;
|
|
|
/**
|
* Handles a client-side channel.
|
*/
|
public class ClientHandler extends SimpleChannelInboundHandler<Object> {
|
|
|
private AnalysisService analysisService;
|
|
private final InternalLogger log = InternalLoggerFactory.getInstance(this.getClass());
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) {
|
InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
|
.remoteAddress();
|
log.info("电表服务器成功连接,IP={},port={}", insocket.getAddress()
|
.getHostAddress(), insocket.getPort());
|
}
|
|
@Override
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
super.channelInactive(ctx);
|
|
InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
|
.remoteAddress();
|
|
log.info("电表服务器断开连接,IP={},port={}", insocket.getAddress(),insocket.getPort());
|
}
|
|
@Override
|
public void channelRead0(ChannelHandlerContext ctx, Object msg)
|
throws Exception {
|
byte[] bytes = (byte[]) msg;
|
String result = BytesUtil.bytesToString(bytes);
|
log.info("电表服务器分机返回信息=" + result);
|
|
// InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
//解析
|
//调用解析接口开始解析
|
if (null == analysisService) {
|
analysisService = (AnalysisService) SpringUtil.getBean(AnalysisService.BEAN_ID);
|
}
|
analysisService.analysis(result);
|
}
|
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
ctx.close();
|
}
|
|
}
|