TransitHandleService.java
5.04 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
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
package cn.com.glsx.kafka.service;
import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.queue.remote.RemoteDeviceLocationService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import cn.hutool.core.date.DateUtil;
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 org.springframework.stereotype.Service;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
/**
* Gps和基站数据处理Service
*
* @author Yanglinlong
* @date 2021/12/17 19:43
*/
@Slf4j
@Service
public class TransitHandleService {
@Autowired
private RedisUtils redisUtils;
@Autowired
private RemoteDeviceLocationService remoteDeviceLocationService;
/**
* 缓存 map
*/
private static ConcurrentHashMap<String, DeviceLocationDto> deviceLocationMap = new ConcurrentHashMap<>();
/**
* 缓存数据入库时间差
*/
private static long tempTime = System.currentTimeMillis();
/**
* 间隔时间
*/
private static final int STEP = 60000;
/**
* 处理GPS和基站的数据
*
* @param deviceLocationDto Gps和基站的数据
*/
public void handleGpsAndBsData(DeviceLocationDto deviceLocationDto, Integer topicId) {
DeviceLocationDto deviceLocationCache = (DeviceLocationDto) redisUtils.get(setKey(deviceLocationDto.getSn()));
if (ObjectUtil.isNull(deviceLocationCache)) {
if (TopicsEnum.DEVICE_STATE.getId().equals(topicId)) {
log.error("该设备{}消费行驶状态前没有消费Gps或基站数据", deviceLocationDto.getSn());
return;
}
// 缓存中没有 就是第一次插入数据 设置创建时间同步更新时间
deviceLocationDto.setCreatedTime(new Date());
log.info("报警消费数据===sn:" + deviceLocationDto.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastBsTime()));
redisUtils.set(setKey(deviceLocationDto.getSn()), deviceLocationDto);
deviceLocationMap.put(deviceLocationDto.getSn(), deviceLocationDto);
} else {
// gps时间比缓存中Gps的时间大就更新缓存的时间
boolean flag = false;
Date lastGpsTime = deviceLocationDto.getLastGpsTime();
Date lastGpsTimeCache = deviceLocationCache.getLastGpsTime();
if (judgeDate(lastGpsTime, lastGpsTimeCache) && lastGpsTime.after(lastGpsTimeCache)) {
deviceLocationCache.setLastGpsTime(lastGpsTime);
flag = true;
}
//基站时间比缓存中基站的时间大就更新缓存的时间
Date lastBsTime = deviceLocationDto.getLastBsTime();
Date lastBsTimeCache = deviceLocationCache.getLastBsTime();
if (judgeDate(lastBsTime, lastBsTimeCache) && lastBsTime.after(lastBsTimeCache)) {
deviceLocationCache.setLastBsTime(lastBsTime);
flag = true;
}
// 停驶状态更新 同步设置flag为true
if (TopicsEnum.DEVICE_STATE.getId().equals(topicId)) {
deviceLocationCache.setStayStatus(deviceLocationDto.getStayStatus());
deviceLocationCache.setStayStatusUpdatetime(deviceLocationDto.getStayStatusUpdatetime());
flag = true;
}
// 判断是否有数据更新 有数据更新重新设置回缓存
if (flag) {
// 同步更新时间
log.info("报警消费数据===sn:" + deviceLocationCache.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastBsTime()));
redisUtils.set(setKey(deviceLocationCache.getSn()), deviceLocationCache);
deviceLocationMap.put(deviceLocationCache.getSn(), deviceLocationCache);
}
}
// 当数据大小为500个 或者 时间间隔3秒 的时候 保存一次
if (System.currentTimeMillis() >= tempTime + STEP) {
log.info("开始插入或者更新Gps和基站的数据大小===" + deviceLocationMap.size());
remoteDeviceLocationService.saveOrUpdateDeviceLocationData(deviceLocationMap.values());
// 清空缓存map
deviceLocationMap.clear();
// 临时时间设置为此次插入数据库的时间
tempTime = System.currentTimeMillis();
}
}
/**
* 判断两个时间是否都不为null
*
* @param dateOne time
* @param dateTwo time
* @return
*/
private Boolean judgeDate(Date dateOne, Date dateTwo) {
return !ObjectUtil.isNull(dateOne) && !ObjectUtil.isNull(dateTwo);
}
/**
* 设置key值
*
* @param key key
* @return
*/
private String setKey(String key) {
return "gpsbs_" + key;
}
}