| | |
| | | package com.ruoyi.dataInterchange.netty.client; |
| | | |
| | | import io.netty.buffer.ByteBuf; |
| | | 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.ChannelInboundHandlerAdapter; |
| | | import io.netty.channel.SimpleChannelInboundHandler; |
| | | import io.netty.util.CharsetUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import javax.annotation.Resource; |
| | | |
| | | /** |
| | | * @author zhibing.pu |
| | | * @Date 2025/3/3 20:13 |
| | | */ |
| | | public class NettyClientHandler extends ChannelInboundHandlerAdapter { |
| | | @Slf4j |
| | | @Component |
| | | public class NettyClientHandler extends SimpleChannelInboundHandler<String> { |
| | | |
| | | public static List<ChannelHandlerContext> cts = new ArrayList<ChannelHandlerContext>(); |
| | | @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); |
| | | |
| | | |
| | | /** |
| | |
| | | System.err.println("客户端和服务端已建立连接"); |
| | | } |
| | | |
| | | public void write(ChannelHandlerContext ctx , String mess) throws Exception { |
| | | 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 msg) throws Exception { |
| | | //读取数据 |
| | | |
| | | //读取数据 |
| | | ByteBuf buf1 = (ByteBuf) msg; |
| | | byte[] req = readClientData((ByteBuf) msg); |
| | | String body = new String(req, "UTF-8"); //获取到的值 |
| | | System.out.println("客户端的数据------>"+body); |
| | | //写数据 |
| | | write(ctx,"wits写的数据"); |
| | | |
| | | public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception { |
| | | OuterPacket outerPacket = (OuterPacket) object; |
| | | int id = outerPacket.getId(); |
| | | serviceRouting(DataType.getDataType(id), ctx, outerPacket); |
| | | } |
| | | |
| | | //将netty的数据装换为字节数组 |
| | | private byte[] readClientData(ByteBuf msg) { |
| | | ByteBuf buf = msg; |
| | | byte[] req = new byte[buf.readableBytes()]; |
| | | buf.readBytes(req); |
| | | buf.release(); |
| | | return req; |
| | | @Override |
| | | protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception { |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * channelInactive |
| | |
| | | |
| | | @Override |
| | | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| | | cts.remove(ctx); |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | } |