Pu Zhibing
2025-03-14 3c66b754ee314ae87d0f2eda2fa86a30ea2304e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;
        }
    }
    
}