package com.ruoyi.dataInterchange.netty.client;
|
|
import com.ruoyi.common.core.utils.SpringUtils;
|
import com.ruoyi.dataInterchange.model.enu.DataType;
|
import com.ruoyi.dataInterchange.server.*;
|
import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
|
import io.netty.buffer.Unpooled;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.util.CharsetUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
|
/**
|
* @author zhibing.pu
|
* @Date 2025/3/3 20:13
|
*/
|
@Slf4j
|
@Component
|
public class NettyClientHandler extends SimpleChannelInboundHandler<String> {
|
|
@Resource
|
private ConnectReqService connectReqService = SpringUtils.getBean(ConnectReqService.class);
|
|
@Resource
|
private UPDisconnectReqService upDisconnectReqService = SpringUtils.getBean(UPDisconnectReqService.class);
|
|
@Resource
|
private UPLinkTestReqService upLinkTestReqService = SpringUtils.getBean(UPLinkTestReqService.class);
|
|
@Resource
|
private UPDisconnectInformService upDisconnectInformService = SpringUtils.getBean(UPDisconnectInformService.class);
|
|
@Resource
|
private UPCloseLinkInformService upCloseLinkInformService = SpringUtils.getBean(UPCloseLinkInformService.class);
|
|
@Resource
|
private DOWNConnectRspService downConnectRspService = SpringUtils.getBean(DOWNConnectRspService.class);
|
|
@Resource
|
private ExgMsgService exgMsgService = SpringUtils.getBean(ExgMsgService.class);
|
|
@Resource
|
private PlatformMsgService platformMsgService = SpringUtils.getBean(PlatformMsgService.class);
|
|
@Resource
|
private WarnMsgService warnMsgService = SpringUtils.getBean(WarnMsgService.class);
|
|
@Resource
|
private CtrlMsgService ctrlMsgService = SpringUtils.getBean(CtrlMsgService.class);
|
|
@Resource
|
private BaseMsgService baseMsgService = SpringUtils.getBean(BaseMsgService.class);
|
|
|
/**
|
* 向服务端发送数据
|
*/
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
System.err.println("客户端和服务端已建立连接");
|
}
|
|
public void write(ChannelHandlerContext ctx, String mess) throws Exception {
|
String sendInfo = mess;
|
ctx.writeAndFlush(Unpooled.copiedBuffer(sendInfo, CharsetUtil.UTF_8)); // 必须有flush
|
ctx.flush();
|
}
|
|
@Override
|
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
|
OuterPacket outerPacket = (OuterPacket) object;
|
int id = outerPacket.getId();
|
serviceRouting(DataType.getDataType(id), ctx, outerPacket);
|
}
|
|
@Override
|
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
|
|
}
|
|
|
/**
|
* channelInactive
|
* channel 通道 Inactive 不活跃的
|
* 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
|
*/
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
System.out.println("客户端与服务端通道-关闭:" + ctx.channel().localAddress() + "channelInactive");
|
}
|
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
ctx.close();
|
System.out.println("异常退出:" + cause.getMessage());
|
}
|
|
|
/**
|
* 业务路由
|
*
|
* @param dataType
|
* @param ctx
|
* @param out
|
*/
|
public void serviceRouting(DataType dataType, ChannelHandlerContext ctx, OuterPacket out) {
|
log.info("从链路信息交换响应({}):{}", dataType.getCode(), out);
|
switch (dataType) {
|
case UP_DISCONNECT_INFORM:
|
log.info("主链路断开通知请求({}):{}", DataType.UP_DISCONNECT_INFORM.getCode(), out);
|
upDisconnectInformService.disconnect(ctx, out);
|
break;
|
case UP_CLOSELINK_INFORM:
|
log.info("下级平台主动关闭主从链路通知({}):{}", DataType.UP_CLOSELINK_INFORM.getCode(), out);
|
upCloseLinkInformService.closeLinkInform(ctx, out);
|
break;
|
case DOWN_CONNECT_RSP:
|
log.info("从链路连接应答({}):{}", DataType.DOWN_CONNECT_RSP.getCode(), out);
|
downConnectRspService.connectRsp(ctx, out);
|
break;
|
case DOWN_DISCONNECT_RSP:
|
log.info("从链路注销应答({}):{}", DataType.DOWN_DISCONNECT_RSP.getCode(), out);
|
break;
|
case DOWN_LINKTEST_RSP:
|
log.info("从链路连接保持应答({}):{}", DataType.DOWN_LINKTEST_RSP.getCode(), out);
|
break;
|
case UP_MANAGE_TOTAL_RECV_BACK_MSG:
|
log.info("发送车辆定位信息数量通知({}):{}", DataType.UP_MANAGE_TOTAL_RECV_BACK_MSG.getCode(), out);
|
break;
|
default:
|
break;
|
}
|
}
|
|
}
|