Pu Zhibing
2025-03-07 297512bc22b179b7038d96a1ff033eceaed38c4b
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 自定义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);
    
    
    
    @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){
        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_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;
            case UP_MANAGE_MSG_RSP:
                log.info("平台链路连接情况与车辆定位消息传输情况上报应答消息({}):{}", DataType.UP_MANAGE_MSG_RSP.getCode(), out);
                break;
            case UP_MANAGE_MSG_SN_INFORM:
                log.info("上传平台间消息序列号通知({}):{}", DataType.UP_MANAGE_MSG_SN_INFORM.getCode(), 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;
            default:
                break;
        }
    }
}