Pu Zhibing
2025-04-07 4109495b9c51a4bbd8b0a7c3c69093909d2e33e1
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package com.ruoyi.dataInterchange.server;
 
import com.alibaba.fastjson.JSON;
import com.ruoyi.dataInterchange.api.vo.GnssDataVo;
import com.ruoyi.dataInterchange.dao.*;
import com.ruoyi.dataInterchange.model.*;
import com.ruoyi.dataInterchange.model.enu.DataType;
import com.ruoyi.dataInterchange.netty.client.ChannelMap;
import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Util;
import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Iterator;
import java.util.List;
 
/**
 * @author zhibing.pu
 * @Date 2025/3/4 19:42
 */
@Slf4j
@Service
public class ExgMsgService {
    
    @Resource
    private ConnectReqService connectReqService;
    
    @Resource
    private UPExgMsgRegisterDao upExgMsgRegisterDao;
    
    @Resource
    private UPExgMsgRealLocationDao upExgMsgRealLocationDao;
    
    @Resource
    private UPExgMsgHistoryLocationDao upExgMsgHistoryLocationDao;
    
    @Resource
    private UPExgMsgReportDriverInfoAckDao upExgMsgReportDriverInfoAckDao;
    
    @Resource
    private UPExgMsgTakeEwayBillAckDao upExgMsgTakeEwayBillAckDao;
    
    @Resource
    private RedisTemplate redisTemplate;
    
    
    public void up_exg_msg(ChannelHandlerContext ctx, OuterPacket out) {
        if (!redisTemplate.hasKey("login:" + out.getGnsscenterId())) {
            log.error("链路还未登录校验,拒绝连接");
            ctx.close();
            return;
        }
        UPExgMsg exgMsg = getExgMsg(out);
        DataType dataType = DataType.getDataType(exgMsg.getDataType());
        log.info("主链路车辆动态信息交换子业务({}):{}", dataType.getCode(), out);
        switch (dataType) {
            case UP_EXG_MSG_REGISTER:
                log.info("上传车辆注册信息({}):{}", DataType.UP_EXG_MSG_REGISTER.getCode(), out);
                up_exg_msg_register(ctx, out.getGnsscenterId(), exgMsg);
                break;
            case UP_EXG_MSG_REAL_LOCATION:
                log.info("上传车辆实时定位信息({}):{}", DataType.UP_EXG_MSG_REAL_LOCATION.getCode(), out);
                up_exg_msg_real_location(ctx, out.getGnsscenterId(), exgMsg);
                break;
            case UP_EXG_MSG_HISTORY_LOCATION:
                log.info("车辆定位信息自动补报请求({}):{}", DataType.UP_EXG_MSG_HISTORY_LOCATION.getCode(), out);
                up_exg_msg_history_location(ctx, out.getGnsscenterId(), exgMsg);
                break;
            case UP_EXG_MSG_RETURN_STARTUP_ACK:
                log.info("启动车辆定位信息交换应答({}):{}", DataType.UP_EXG_MSG_RETURN_STARTUP_ACK.getCode(), out);
                break;
            case UP_EXG_MSG_RETURN_END_ACK:
                log.info("结束车辆定位信息交换应答({}):{}", DataType.UP_EXG_MSG_RETURN_END_ACK.getCode(), out);
                break;
            case UP_EXG_MSG_APPLY_FOR_MONITOR_STARTUP:
                log.info("申请交换指定车辆定位信息请求({}):{}", DataType.UP_EXG_MSG_APPLY_FOR_MONITOR_STARTUP.getCode(), out);
                break;
            case UP_EXG_MSG_APPLY_FOR_MONITOR_END:
                log.info("取消交换指定车辆定位信息请求({}):{}", DataType.UP_EXG_MSG_APPLY_FOR_MONITOR_END.getCode(), out);
                break;
            case UP_EXG_MSG_APPLY_HISGNSSDATA_REQ:
                log.info("车辆定位信息补发请求({}):{}", DataType.UP_EXG_MSG_APPLY_HISGNSSDATA_REQ.getCode(), out);
                break;
            case UP_EXG_MSG_REPORT_DRIVER_INFO_ACK:
                log.info("上报驾驶员身份信息应答({}):{}", DataType.UP_EXG_MSG_REPORT_DRIVER_INFO_ACK.getCode(), out);
                up_exg_msg_report_driver_info_ack(ctx, out.getGnsscenterId(), exgMsg);
                break;
            case UP_EXG_MSG_TAKE_EWAYBILL_ACK:
                log.info("上报车辆电子运单应答消息({}):{}", DataType.UP_EXG_MSG_TAKE_EWAYBILL_ACK.getCode(), out);
                up_exg_msg_take_ewaybill_ack(ctx, out.getGnsscenterId(), exgMsg);
                break;
            default:
                break;
        }
    }
    
    
    /**
     * 解析子业务数据
     *
     * @param out
     * @return
     */
    public UPExgMsg getExgMsg(OuterPacket out) {
        byte[] body = out.getBody();
        ByteBuf byteBuf = Unpooled.wrappedBuffer(body);
        //车牌号
        String vehicleNo = Jtt809Util.readGBKString(byteBuf, 21);
        //车牌颜色
        byte vehicleColor = byteBuf.readByte();
        //子业务类型标识
        int dataType = byteBuf.readUnsignedShort();
        //后续数据长度
        int dataLength = byteBuf.readInt();
        //子业务数据包
        byte[] data = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(data);
        UPExgMsg upExgMsg = new UPExgMsg();
        upExgMsg.setVehicleNo(vehicleNo);
        upExgMsg.setVehicleColor(vehicleColor);
        upExgMsg.setDataType(dataType);
        upExgMsg.setDataLength(dataLength);
        upExgMsg.setData(data);
        return upExgMsg;
    }
    
    
    /**
     * 上传车辆注册信息
     *
     * @param ctx
     */
    public void up_exg_msg_register(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
        UPExgMsgRegister upExgMsgRegister = new UPExgMsgRegister().decode(exgMsg);
        upExgMsgRegister.setInferiorPlatformId(inferiorPlatformId);
        upExgMsgRegister.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        UPExgMsgRegister register = upExgMsgRegisterDao.findByVehicleNo(upExgMsgRegister.getVehicleNo());
        if (null == register) {
            upExgMsgRegisterDao.save(upExgMsgRegister);
        }
    }
    
    
    /**
     * 定时获取司机基础信息
     */
    public void taskReportDriverInfo() {
        Iterator<UPExgMsgRegister> iterator = upExgMsgRegisterDao.findAll().iterator();
        while (iterator.hasNext()) {
            UPExgMsgRegister register = iterator.next();
            
            //上报驾驶员身份识别信息请求
            UPExgMsgReportDriverInfoAck byVehicleNo = upExgMsgReportDriverInfoAckDao.findByVehicleNo(register.getVehicleNo());
            if (null == byVehicleNo) {
                down_exg_msg_report_driver_info(register.getInferiorPlatformId(), register.getVehicleNo(), register.getVehicleColor());
            }
        }
        
    }
    
    /**
     * 上报驾驶员身份识别信息请求
     */
    public void down_exg_msg_report_driver_info(int inferiorPlatformId, String vehicleNo, int VehicleColor) {
        DOWNExgMsgReportDriverInfo downExgMsgReportDriverInfo = new DOWNExgMsgReportDriverInfo();
        downExgMsgReportDriverInfo.setVehicleNo(vehicleNo);
        downExgMsgReportDriverInfo.setVehicleColor(VehicleColor);
        downExgMsgReportDriverInfo.setDataType(DataType.DOWN_EXG_MSG_REPORT_DRIVER_INFO.getCode());
        downExgMsgReportDriverInfo.setDataLength(0);
        byte[] body = downExgMsgReportDriverInfo.encode();
        OuterPacket out = new OuterPacket(DataType.DOWN_EXG_MSG.getCode(), body);
        //获取从链路通道
        Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
        if (null != channel && channel.isActive()) {
            channel.writeAndFlush(out);
            log.info("上报驾驶员身份识别信息请求({}):{}", DataType.DOWN_EXG_MSG_REPORT_DRIVER_INFO.getCode(), JSON.toJSONString(downExgMsgReportDriverInfo));
        }
    }
    
    
    /**
     * 上传车辆实时定位信息
     *
     * @param ctx
     */
    public void up_exg_msg_real_location(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
        UPExgMsgRealLocation upExgMsgRealLocation = new UPExgMsgRealLocation().decode(exgMsg);
        upExgMsgRealLocation.setInferiorPlatformId(inferiorPlatformId);
        upExgMsgRealLocation.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        upExgMsgRealLocationDao.save(upExgMsgRealLocation);
        //修改车辆实时定位
        GnssDataVo vo = new GnssDataVo();
        BeanUtils.copyProperties(upExgMsgRealLocation.getGnssData(), vo);
        redisTemplate.opsForValue().set("location:" + upExgMsgRealLocation.getVehicleNo(), vo);
        //查询车辆信息,没有则主动发起请求
        UPExgMsgRegister msgRegister = upExgMsgRegisterDao.findByVehicleNo(upExgMsgRealLocation.getVehicleNo());
        if (null == msgRegister) {
            down_base_msg_vehicle_added(inferiorPlatformId, upExgMsgRealLocation.getVehicleNo(), upExgMsgRealLocation.getVehicleColor());
        }
    }
    
    
    /**
     * 定时任务获取车辆运单信息
     */
    public void takeEwaybill() {
        Iterator<UPExgMsgRegister> iterator = upExgMsgRegisterDao.findAll().iterator();
        while (iterator.hasNext()) {
            UPExgMsgRegister pojo = iterator.next();
            down_exg_msg_take_ewaybill_req(pojo.getInferiorPlatformId(), pojo.getVehicleNo(), pojo.getVehicleColor());
        }
    }
    
    
    /**
     * 上报车辆电子运单请求
     *
     * @param inferiorPlatformId
     */
    public void down_exg_msg_take_ewaybill_req(int inferiorPlatformId, String vehicleNo, int VehicleColor) {
        if (!redisTemplate.hasKey("login:" + inferiorPlatformId)) {
            log.error("链路还未登录校验,拒绝连接");
            return;
        }
        DOWNExgMsgTakeEwaybillReq downExgMsgTakeEwaybillReq = new DOWNExgMsgTakeEwaybillReq();
        downExgMsgTakeEwaybillReq.setVehicleNo(vehicleNo);
        downExgMsgTakeEwaybillReq.setVehicleColor(VehicleColor);
        downExgMsgTakeEwaybillReq.setDataType(DataType.DOWN_EXG_MSG_TAKE_EWAYBILL_REQ.getCode());
        downExgMsgTakeEwaybillReq.setDataLength(0);
        
        byte[] body = downExgMsgTakeEwaybillReq.encode();
        OuterPacket out = new OuterPacket(DataType.DOWN_EXG_MSG.getCode(), body);
        //获取从链路通道
        Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
        if (null != channel && channel.isActive()) {
            channel.writeAndFlush(out);
            log.info("上报车辆电子运单请求({}):{}", DataType.DOWN_EXG_MSG_TAKE_EWAYBILL_REQ.getCode(), JSON.toJSONString(downExgMsgTakeEwaybillReq));
        }
    }
    
    
    /**
     * 车辆定位信息自动补报请求
     *
     * @param ctx
     * @param inferiorPlatformId
     * @param exgMsg
     */
    public void up_exg_msg_history_location(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
        UPExgMsgHistoryLocation upExgMsgHistoryLocation = new UPExgMsgHistoryLocation().decode(exgMsg);
        upExgMsgHistoryLocation.setInferiorPlatformId(inferiorPlatformId);
        upExgMsgHistoryLocation.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        upExgMsgHistoryLocationDao.save(upExgMsgHistoryLocation);
        
        //将数据存储实时定位数据表中
        List<GnssData> gnssData = upExgMsgHistoryLocation.getGnssData();
        for (GnssData gnssDatum : gnssData) {
            UPExgMsgRealLocation upExgMsgRealLocation = new UPExgMsgRealLocation();
            upExgMsgRealLocation.setVehicleNo(upExgMsgHistoryLocation.getVehicleNo());
            upExgMsgRealLocation.setVehicleColor(upExgMsgHistoryLocation.getVehicleColor());
            upExgMsgRealLocation.setInferiorPlatformId(inferiorPlatformId);
            upExgMsgRealLocation.setDataType(DataType.UP_EXG_MSG_REAL_LOCATION.getCode());
            upExgMsgRealLocation.setGnssData(gnssDatum);
            upExgMsgRealLocation.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
            upExgMsgRealLocationDao.save(upExgMsgRealLocation);
        }
    }
    
    
    /**
     * 上报驾驶员身份识别信息应答
     *
     * @param ctx
     * @param inferiorPlatformId
     * @param exgMsg
     */
    public void up_exg_msg_report_driver_info_ack(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
        UPExgMsgReportDriverInfoAck upExgMsgReportDriverInfoAck = new UPExgMsgReportDriverInfoAck().decode(exgMsg);
        upExgMsgReportDriverInfoAck.setInferiorPlatformId(inferiorPlatformId);
        upExgMsgReportDriverInfoAck.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        upExgMsgReportDriverInfoAckDao.save(upExgMsgReportDriverInfoAck);
    }
    
    
    /**
     * 上报车辆电子运单应答
     *
     * @param ctx
     * @param inferiorPlatformId
     * @param exgMsg
     */
    public void up_exg_msg_take_ewaybill_ack(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
        UPExgMsgTakeEwayBillAck upExgMsgTakeEwayBillAck = new UPExgMsgTakeEwayBillAck().decode(exgMsg);
        upExgMsgTakeEwayBillAck.setInferiorPlatformId(inferiorPlatformId);
        upExgMsgTakeEwayBillAck.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        UPExgMsgTakeEwayBillAck byVehicleNo = upExgMsgTakeEwayBillAckDao.findByVehicleNo(upExgMsgTakeEwayBillAck.getVehicleNo());
        if (null == byVehicleNo) {
            upExgMsgTakeEwayBillAckDao.save(upExgMsgTakeEwayBillAck);
        }
    }
    
    
    /**
     * 补报车辆静态信息
     */
    public void down_base_msg_vehicle_added(int inferiorPlatformId, String vehicleNo, int VehicleColor) {
        if (!redisTemplate.hasKey("login:" + inferiorPlatformId)) {
            log.error("链路还未登录校验,拒绝连接");
            return;
        }
        DOWNBaseMsgVehicleAdded downBaseMsgVehicleAdded = new DOWNBaseMsgVehicleAdded();
        downBaseMsgVehicleAdded.setVehicleNo(vehicleNo);
        downBaseMsgVehicleAdded.setVehicleColor(VehicleColor);
        downBaseMsgVehicleAdded.setDataType(DataType.DOWN_BASE_MSG_VEHICLE_ADDED.getCode());
        downBaseMsgVehicleAdded.setDataLength(0);
        
        byte[] body = downBaseMsgVehicleAdded.encode();
        OuterPacket out = new OuterPacket(DataType.DOWN_BASE_MSG.getCode(), body);
        //获取从链路通道
        Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
        if (null != channel && channel.isActive()) {
            channel.writeAndFlush(out);
            log.info("补报车辆静态信息请求({}):{}", DataType.DOWN_BASE_MSG_VEHICLE_ADDED.getCode(), JSON.toJSONString(downBaseMsgVehicleAdded));
        }
    }
    
}