Pu Zhibing
3 天以前 0b71af4620572229218a4f59b85f0a9c0090279b
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.ruoyi.dataInterchange.server;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.dataInterchange.dao.UPExgMsgRealLocationDao;
import com.ruoyi.dataInterchange.dao.UPWarnMsgAdptInfoDao;
import com.ruoyi.dataInterchange.dao.UPWarnMsgUrgeTodoAckDao;
import com.ruoyi.dataInterchange.model.*;
import com.ruoyi.dataInterchange.model.enu.DataType;
import com.ruoyi.dataInterchange.netty.client.ChannelMap;
import com.ruoyi.dataInterchange.util.haikang.model.Alarm;
import com.ruoyi.dataInterchange.util.haikang.model.AlarmPic;
import com.ruoyi.dataInterchange.util.haikang.model.Event;
import com.ruoyi.dataInterchange.util.haikang.model.SubscriptionEvent;
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.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
 
/**
 * @author zhibing.pu
 * @Date 2025/3/4 20:45
 */
@Slf4j
@Component
public class WarnMsgService {
    
    @Resource
    private ConnectReqService connectReqService;
    
    @Resource
    private UPWarnMsgUrgeTodoAckDao upWarnMsgUrgeTodoAckDao;
    
    @Resource
    private UPWarnMsgAdptInfoDao upWarnMsgAdptInfoDao;
    
    @Resource
    private RedisTemplate redisTemplate;
    
    @Resource
    private UPExgMsgRealLocationDao upExgMsgRealLocationDao;
    //走川标的公司
    private List<Integer> enterprise = Arrays.asList(17458936, 43984060, 13914394);
    
    public void up_warn_msg(ChannelHandlerContext ctx, OuterPacket out) {
        if (!redisTemplate.hasKey("login:" + out.getGnsscenterId())) {
            log.error("链路还未登录校验,拒绝连接:{}", out.getGnsscenterId());
            ctx.close();
            return;
        }
        WarnMsg warnMsg = getWarnMsg(out);
        DataType dataType = DataType.getDataType(warnMsg.getDataType());
        switch (dataType) {
            case UP_WARN_MSG_URGE_TODO_ACK:
                log.info("报警督办应答消息({}):{}", DataType.UP_WARN_MSG_URGE_TODO_ACK.getCode(), out);
                up_warn_msg_urge_todo_ack(ctx, out.getGnsscenterId(), warnMsg);
                break;
            case UP_WARN_MSG_ADPT_INFO:
                log.info("上报报警信息({}):{}", DataType.UP_WARN_MSG_ADPT_INFO.getCode(), out);
                up_warn_msg_adpt_info(ctx, out.getGnsscenterId(), warnMsg);
                break;
            default:
                break;
        }
    }
    
    /**
     * 解析子业务数据
     *
     * @param out
     * @return
     */
    public WarnMsg getWarnMsg(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);
        WarnMsg warnMsg = new WarnMsg();
        warnMsg.setVehicleNo(vehicleNo);
        warnMsg.setVehicleColor(vehicleColor);
        warnMsg.setDataType(dataType);
        warnMsg.setDataLength(dataLength);
        warnMsg.setData(data);
        return warnMsg;
    }
 
    /**
     * 报警督办应答消息
     *
     * @param ctx
     * @param inferiorPlatformId
     * @param warnMsg
     */
    public void up_warn_msg_urge_todo_ack(ChannelHandlerContext ctx, int inferiorPlatformId, WarnMsg warnMsg) {
        UPWarnMsgUrgeTodoAck upWarnMsgUrgeTodoAck = new UPWarnMsgUrgeTodoAck().decode(warnMsg);
        upWarnMsgUrgeTodoAck.setInferiorPlatformId(inferiorPlatformId);
        upWarnMsgUrgeTodoAck.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        upWarnMsgUrgeTodoAckDao.save(upWarnMsgUrgeTodoAck);
    }
 
    /**
     * 上报报警信息
     *
     * @param ctx
     * @param inferiorPlatformId
     * @param warnMsg
     */
    public void up_warn_msg_adpt_info(ChannelHandlerContext ctx, int inferiorPlatformId, WarnMsg warnMsg) {
        UPWarnMsgAdptInfo upWarnMsgAdptInfo = enterprise.contains(inferiorPlatformId) ? new UPWarnMsgAdptInfo().decode2(warnMsg) : new UPWarnMsgAdptInfo().decode1(warnMsg);
        upWarnMsgAdptInfo.setResult(0x00);
        upWarnMsgAdptInfo.setInferiorPlatformId(inferiorPlatformId);
        upWarnMsgAdptInfo.setCreateTime(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        if(0 == upWarnMsgAdptInfo.getLongitude()){
            UPExgMsgRealLocation upExgMsgRealLocation = upExgMsgRealLocationDao.findByVehicleNoOrderByCreateTimeDesc(upWarnMsgAdptInfo.getVehicleNo());
            if (null != upExgMsgRealLocation) {
                GnssData gnssData = upExgMsgRealLocation.getGnssData();
                upWarnMsgAdptInfo.setSpeed(gnssData.getVec1());
                upWarnMsgAdptInfo.setLongitude(gnssData.getLon());
                upWarnMsgAdptInfo.setLatitude(gnssData.getLat());
            }
        }else if(2 != upWarnMsgAdptInfo.getLevel()){
            return;
        }
        upWarnMsgAdptInfoDao.save(upWarnMsgAdptInfo);
    }
    
    
    /**
     * 定时任务督办报警请求
     */
    public void taskUrgeTodo() {
        List<UPWarnMsgAdptInfo> list = upWarnMsgAdptInfoDao.findByResultIsAndLevelNotNullAndPushTimeBefore(0x00, LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        for (UPWarnMsgAdptInfo upWarnMsgAdptInfo : list) {
            down_warn_msg_urge_todo_req(upWarnMsgAdptInfo);
        }
    }
    
    
    /**
     * 报警督办请求
     */
    public void down_warn_msg_urge_todo_req(UPWarnMsgAdptInfo upWarnMsgAdptInfo) {
        if (!redisTemplate.hasKey("login:" + upWarnMsgAdptInfo.getInferiorPlatformId())) {
            log.error("链路还未登录校验,拒绝连接:{}", upWarnMsgAdptInfo.getInferiorPlatformId());
            return;
        }
        int inferiorPlatformId = upWarnMsgAdptInfo.getInferiorPlatformId();
        DOWNWarnMsgUrgeTodoReq downWarnMsgUrgeTodoReq = new DOWNWarnMsgUrgeTodoReq().build(upWarnMsgAdptInfo);
        downWarnMsgUrgeTodoReq.setDataType(DataType.DOWN_WARN_MSG_URGE_TODO_REQ.getCode());
        downWarnMsgUrgeTodoReq.setDataLength(92);
        byte[] body = downWarnMsgUrgeTodoReq.encode();
        OuterPacket out = new OuterPacket(DataType.DOWN_WARN_MSG.getCode(), inferiorPlatformId, body);
        //获取从链路通道
        Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
        if (null != channel && channel.isActive()) {
            channel.writeAndFlush(out);
            log.info("报警督办请求({}):{}", DataType.DOWN_WARN_MSG_URGE_TODO_REQ.getCode(), JSON.toJSONString(downWarnMsgUrgeTodoReq));
        }
        upWarnMsgAdptInfo.setPushTime(LocalDateTime.now().plusDays(7).toEpochSecond(ZoneOffset.ofHours(8)));
        upWarnMsgAdptInfoDao.save(upWarnMsgAdptInfo);
    }
    
    
    /**
     * 存储mqtt协议报警信息
     */
    public void saveWarnMsgService(JSONObject jsonObject) {
        SubscriptionEvent subscriptionEvent = jsonObject.getObject("", SubscriptionEvent.class);
        List<Event> events = subscriptionEvent.getEvents();
        events.forEach(event -> {
            //车牌号
            String srcName = event.getSrcName();
            Alarm data = JSONObject.parseObject(event.getData(), Alarm.class);
            UPWarnMsgAdptInfo upWarnMsgAdptInfo = upWarnMsgAdptInfoDao.findByInfoIdIs(data.getAlarmId());
            if(null == upWarnMsgAdptInfo){
                upWarnMsgAdptInfo = new UPWarnMsgAdptInfo();
            }
            upWarnMsgAdptInfo.setVehicleNo(srcName);
            upWarnMsgAdptInfo.setVehicleColor(data.getVehicleLicenseColor());
            upWarnMsgAdptInfo.setWarnSrc(data.getSourceType() + 1);
            upWarnMsgAdptInfo.setWarnType(getAlarmType(data.getEventType()));
            upWarnMsgAdptInfo.setWarnTime(LocalDateTime.parse(data.getAlarmTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toEpochSecond(ZoneOffset.ofHours(8)));
            upWarnMsgAdptInfo.setInfoId(data.getAlarmId());
            upWarnMsgAdptInfo.setLevel(getLevel(data.getLevel()));
            upWarnMsgAdptInfo.setLongitude(data.getLongitude().intValue());
            upWarnMsgAdptInfo.setLatitude(data.getLatitude().intValue());
            upWarnMsgAdptInfo.setAltitude(data.getHeight().intValue() / 10);
            upWarnMsgAdptInfo.setSpeed(data.getSpeed().intValue() / 10000);
            upWarnMsgAdptInfo.setVec2(data.getSpeed().intValue());
            upWarnMsgAdptInfo.setStatus(data.getStatus());
            upWarnMsgAdptInfo.setDirection(data.getDirection() / 100);
            upWarnMsgAdptInfo.setInfoContent(data.getAlarmInfo());
            upWarnMsgAdptInfoDao.save(upWarnMsgAdptInfo);
        });
    }
    
    public int getAlarmType(long eventType){
        switch ((int) eventType){
            case 0x00020501:
                return 0x0301;
            case 0x00020502:
                return 0x0201;
            case 0x00020503:
                return 0x0202;
            case 0x00020504:
                return 0x0202;
            case 0x00020505:
                return 0x0202;
            case 0x00020506:
                return 0x0202;
            case 0x00020507:
                return 0x0203;
            case 0x00020508:
                return 0x0203;
            case 0x00020509:
                return 0x0204;
            case 0x00020510:
                return 0x0204;
            case 0x00037017:
                return 0x00FF;
            case 0x00020511:
                return 0x00FF;
            case 0x00037005:
                return 0x00FF;
            case 0x00020512:
                return 0x0002;
            case 0x00020513:
                return 0x0002;
            case 0x00020514:
                return 0x00FF;
            case 0x00020515:
                return 0x00FF;
            case 0x00020516:
                return 0x0214;
            case 0x00020517:
                return 0x0213;
            case 0x00020519:
                return 0x0215;
            case 0x00020520:
                return 0x0234;
            case 0x00020521:
                return 0x0234;
            case 0x00020522:
                return 0x0234;
            case 0x00020523:
                return 0x0241;
            case 0x00020524:
                return 0x0242;
            case 0x00020525:
                return 0x0243;
            case 0x00020526:
                return 0x0243;
            case 0x00020531:
                return 0x00FF;
            case 0x00020532:
                return 0x00FF;
            case 0x00020533:
                return 0x00FF;
            case 0x00020534:
                return 0x0218;
            case 0x00020518:
                return 0xA0FF;
            case 0x00037001:
                return 0xA0FF;
            case 0x00037002:
                return 0x0216;
            case 0x00037006:
                return 0x0216;
            case 0x00020535:
                return 0x0001;
            case 0x00020536:
                return 0x0001;
            case 0x00020537:
                return 0x000D;
            case 0x00020102:
                return 0x0102;
            case 0x00020538:
                return 0x0005;
            case 0x00020539:
                return 0x0004;
            case 0x00020540:
                return 0x0304;
            case 0x00020541:
                return 0x0304;
            case 0x00020542:
                return 0x000B;
            case 0x00020543:
                return 0x0001;
            case 0x00020544:
                return 0x0001;
            case 0x00020545:
                return 0xA001;
            case 0x00020546:
                return 0x0305;
            case 0x00020547:
                return 0x0305;
            case 0x00020548:
                return 0xA0FF;
            case 0x00010202:
                return 0x0103;
            case 0x00010206:
                return 0x0103;
            case 0x00020101:
                return 0x0101;
            case 0x00020103:
                return 0x000C;
            case 0x00020549:
                return 0xA00A;
            case 0x00020550:
                return 0xA00A;
            case 0x00020551:
                return 0xA00A;
            case 0x00020552:
                return 0x0308;
            case 0x00020553:
                return 0x0308;
            case 0x00020554:
                return 0x0308;
            case 0x00020555:
                return 0x0308;
            case 0x00020556:
                return 0x0308;
            case 0x00020557:
                return 0x0308;
            case 0x00020558:
                return 0x0308;
            case 0x00020559:
                return 0x0003;
            case 0x00020527:
                return 0x0301;
            case 0x00020528:
                return 0x0201;
            case 0x00020529:
                return 0x0302;
            case 0x00020530:
                return 0x0302;
            case 0x00020560:
                return 0x0009;
            case 0x00020561:
                return 0x0306;
            case 0x00020562:
                return 0x0307;
            case 0x00090001:
                return 0x00FF;
                default:
                    return 0x00FF;
        }
    }
    
    public int getLevel(String level){
        switch (level){
            case "h":
            case "m":
                return 1;
            case "l":
            case "w":
                return 2;
            default:
                return 0;
        }
    }
    
    /**
     * 存储mqtt协议报警图片信息
     */
    public void saveWarnMsgPicService(JSONObject jsonObject) {
        SubscriptionEvent subscriptionEvent = jsonObject.getObject("", SubscriptionEvent.class);
        List<Event> events = subscriptionEvent.getEvents();
        events.forEach(event -> {
            AlarmPic data = JSONObject.parseObject(event.getData(), AlarmPic.class);
            if(StringUtils.hasLength(data.getAlarmId())){
                UPWarnMsgAdptInfo upWarnMsgAdptInfo = upWarnMsgAdptInfoDao.findByInfoIdIs(data.getAlarmId());
                if(null != upWarnMsgAdptInfo){
                    upWarnMsgAdptInfo.setPicUrl(data.getUrl());
                    upWarnMsgAdptInfoDao.save(upWarnMsgAdptInfo);
                }else{
                    upWarnMsgAdptInfo = new UPWarnMsgAdptInfo();
                    upWarnMsgAdptInfo.setInfoId(data.getAlarmId());
                    upWarnMsgAdptInfoDao.save(upWarnMsgAdptInfo);
                }
            }
        });
    }
    
    
    
}