package com.ruoyi.dataInterchange.netty.server;
|
|
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.channel.ChannelHandlerContext;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
|
/**
|
* 自定义handler
|
*
|
* @author zhibing.pu
|
* @Date 2025/3/3 19:30
|
*/
|
@Slf4j
|
@Component
|
public class NettyHandle 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);
|
|
@Resource
|
private AuthorizeMsgService authorizeMsgService = SpringUtils.getBean(AuthorizeMsgService.class);
|
|
@Resource
|
private RealvideoMsg realvideoMsg = SpringUtils.getBean(RealvideoMsg.class);
|
|
|
@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 ctx, String msg) throws Exception {
|
log.info("收到客户端发送消息:{}", msg);
|
//数据转义
|
msg = msg.replaceAll("5a01", "5b");
|
msg = msg.replaceAll("5a02", "5a");
|
msg = msg.replaceAll("5e01", "5d");
|
msg = msg.replaceAll("5e02", "5e");
|
log.info("转义后的数据包:{}", msg);
|
//数据头
|
String head = msg.substring(0, 2); //--头标识
|
String tail = msg.substring(msg.length() - 2);
|
String datalength = msg.substring(2, 10);//--数据头->数据长度
|
String dataSeqNo = msg.substring(10, 18);// --数据头->报文序列号
|
String bizdata = msg.substring(18, 22);// --数据头->业务数据类型
|
String code = msg.substring(22, 30); //--数据头->下级平台接入码,上级平台给下级平台分配唯一标识码
|
String version = msg.substring(30, 36); //--数据头->协议版本号标识
|
String entryFlag = msg.substring(36, 38);//--数据头->报文加密标识位
|
String key = msg.substring(38, 46);//--数据头->数据加密的密匙
|
String time = msg.substring(46, 62);//--数据头->系统时间
|
|
//数据体
|
String body = msg.substring(62, msg.length() - 2);
|
|
}
|
|
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
log.info("Netty客户端链接成功");
|
super.channelActive(ctx);
|
}
|
|
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
log.info("Netty客户端连链接常关闭");
|
cause.printStackTrace();
|
ctx.close();
|
}
|
|
@Override
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
log.info("Netty客户端离线");
|
super.channelInactive(ctx);
|
}
|
|
|
/**
|
* 业务路由
|
*
|
* @param dataType
|
* @param ctx
|
* @param out
|
*/
|
public void serviceRouting(DataType dataType, ChannelHandlerContext ctx, OuterPacket out) {
|
log.info("主链路信息交换响应({}):{}", dataType.getCode(), out);
|
switch (dataType) {
|
case UP_CONNECT_REQ:
|
log.info("主链路登录请求({}):{}", DataType.UP_CONNECT_REQ.getCode(), out);
|
connectReqService.connect(ctx, out);
|
break;
|
case UP_DISCONNECT_REQ:
|
log.info("主链路注销请求({}):{}", DataType.UP_DISCONNECT_REQ.getCode(), out);
|
upDisconnectReqService.disconnect(ctx, out);
|
break;
|
case UP_LINKTEST_REQ:
|
log.info("主链路连接保持请求({}):{}", DataType.UP_LINKTEST_REQ.getCode(), out);
|
upLinkTestReqService.linkTest(ctx, out);
|
break;
|
case UP_EXG_MSG:
|
log.info("主链路车辆动态信息交换({}):{}", DataType.UP_EXG_MSG.getCode(), out);
|
exgMsgService.up_exg_msg(ctx, out);
|
break;
|
case UP_PLATFORM_MSG:
|
log.info("主链路平台间信息交互({}):{}", DataType.UP_PLATFORM_MSG.getCode(), out);
|
platformMsgService.up_platform_msg(ctx, out);
|
break;
|
case UP_WARN_MSG:
|
log.info("报警信息交互({}):{}", DataType.UP_WARN_MSG.getCode(), out);
|
warnMsgService.up_warn_msg(ctx, out);
|
break;
|
case UP_CTRL_MSG:
|
log.info("车辆监管业务({}):{}", DataType.UP_CTRL_MSG.getCode(), out);
|
ctrlMsgService.up_ctrl_msg(ctx, out);
|
break;
|
case UP_BASE_MSG:
|
log.info("车辆静态信息交换业务({}):{}", DataType.UP_BASE_MSG.getCode(), out);
|
baseMsgService.up_base_msg(ctx, out);
|
break;
|
case UP_AUTHORIZE_MSG:
|
log.info("主链路视频时效口令交互({}):{}", DataType.UP_AUTHORIZE_MSG.getCode(), out);
|
authorizeMsgService.up_authorize_msg(ctx, out);
|
break;
|
case UP_REALVIDEO_MSG:
|
log.info("主链路实时音视频交互({}):{}", DataType.UP_REALVIDEO_MSG.getCode(), out);
|
realvideoMsg.up_realvideo_msg(ctx, out);
|
break;
|
default:
|
break;
|
}
|
}
|
}
|