GpsAndBsConsumer.java
3.38 KB
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
package cn.com.glsx.kafka.queue.consumer;
import cn.com.glsx.kafka.constant.Constants;
import cn.com.glsx.kafka.protocol.kafkaSvr;
import cn.com.glsx.kafka.queue.base.MessageReceiver;
import cn.com.glsx.kafka.service.BloomFilterService;
import cn.com.glsx.kafka.service.TransitHandleService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.glsx.plat.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* GPS和基站数据消费
* topic : gps_gpsandbs
*
* @author yll
*/
@Slf4j
public class GpsAndBsConsumer extends MessageReceiver {
@Autowired
private RedisUtils redisUtils;
@Autowired
private BloomFilterService bloomFilterService;
@Autowired
private TransitHandleService transitHandleService;
@Override
public void processMessage(String topic, byte[] message) {
log.info("Gps基站数据开始消费===topic" + topic);
try {
kafkaSvr.GPSAndBSReq gpsAndBsReq = kafkaSvr.GPSAndBSReq.parseFrom(message);
// 判断是否是新E盾的设备号 如果不是则过滤掉
if (!bloomFilterService.isExits(gpsAndBsReq.getDevicesn())) {
// log.info("设备号" + gpsAndBsReq.getDevicesn() + "不在新E盾,不入库");
return;
}
DeviceLocationDto deviceLocationDto = setGpsAndBsData(gpsAndBsReq);
if (ObjectUtil.isNull(deviceLocationDto)) {
return;
}
// 写入redis 并且比较 gps和基站时间的先后
transitHandleService.handleGpsAndBsData(deviceLocationDto);
} catch (Exception e) {
log.error("Gps基站数据消费异常===topic:{},{}", topic, e.getMessage());
e.printStackTrace();
}
}
private DeviceLocationDto setGpsAndBsData(kafkaSvr.GPSAndBSReq gpsAndBsReq) {
kafkaSvr.GPSDATA gpsData = gpsAndBsReq.getGpsdata();
if (null == gpsData) {
return null;
}
log.info("设备号:{},速度:{}", gpsAndBsReq.getDevicesn(), gpsAndBsReq.getGpsdata().getSpeed());
Date time = new Date(gpsData.getTime() * 1000L);
//当前时间如果大于今日,过滤
Calendar c = Calendar.getInstance();
Date now = new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
if (time.after(now)) {
return null;
}
DeviceLocationDto deviceLocationDto = new DeviceLocationDto();
deviceLocationDto.setSn(gpsAndBsReq.getDevicesn());
if (gpsData.getValid().equals(Constants.GPS_IS)) {
//gps有效定位 封装Gps数据
deviceLocationDto.setLastGpsTime(time);
}
List<kafkaSvr.BASESTDATA> bsDataList = gpsAndBsReq.getBstdataList();
if (CollectionUtil.isNotEmpty(bsDataList)) {
// 封装基站数据
deviceLocationDto.setLastBsTime(time);
}
// 如果最后GPS时间和最后基站时间都为 null,则过滤掉数据
if (ObjectUtil.isNull(deviceLocationDto.getLastGpsTime()) && ObjectUtil.isNull(deviceLocationDto.getLastBsTime())) {
return null;
}
return deviceLocationDto;
}
}