DeviceActiveService.java
9.07 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
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
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
/**
* 设备激活 service
*
* @author yangll
*/
@Slf4j
@Service
public class DeviceActiveService {
@Autowired
private RemoteGpsBsService remoteGpsBsService;
@Autowired
private VehicleDeviceMapper vehicleDeviceMapper;
/**
* 保存设备激活信息 feign 调用
*
* @param snList 设备号集合
*/
public void saveActiveInfo(List<String> snList) {
if (CollectionUtil.isEmpty(snList)) {
log.info("saveActiveInfo snList is null");
return;
}
saveActiveInfoByFeign(snList);
}
/**
* 保存设备激活信息 by 定时任务调用
*
* @param snList 设备号集合
*/
public void saveActiveInfoByTask(List<String> snList) {
if (CollectionUtil.isEmpty(snList)) {
log.info("saveActiveInfoByTask snList is null");
return;
}
saveActiveInfoSyncByTask(snList);
}
/**
* 多线程补充设备激活经纬度数据和激活地址 by 定时任务调用
*
* @param dataList 设备号集合大小
*/
public void saveActiveInfoSyncByTask(List<String> dataList) {
log.info("###save active info by task data start###");
if (CollectionUtils.isEmpty(dataList)) {
return;
}
StopWatch sw = new StopWatch();
sw.start("saveActiveInfoSyncByTask");
for (String sn : dataList) {
saveActiveInfoByTask(sn);
}
sw.stop();
log.info("###save active info by task data end" + sw.getLastTaskTimeMillis() + "ms###");
}
/**
* 获取7天内没有激活数据的设备号
*
* @return 设备号集合
*/
public List<String> getUnActiveInfoSnList() {
// 获取七天前的日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
String dayTimeFormat = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
log.info("开始数据统计的天数===" + dayTimeFormat);
return vehicleDeviceMapper.getUnActiveInfoSnList(dayTimeFormat);
}
/**
* 手动更新全部设备激活定位数据
*/
public void saveActiveInfoFlushAll() {
List<String> allUnActiveInfoSnList = vehicleDeviceMapper.getAllUnActiveInfoSnList();
log.info("allUnActiveInfoSnList size===" + allUnActiveInfoSnList.size());
saveActiveInfoByTask(allUnActiveInfoSnList);
}
public void saveActiveInfoByFeign(List<String> snList) {
if (CollectionUtil.isEmpty(snList)) {
log.info("snList is null");
return;
}
try {
log.info("saveActiveInfoByFeign 开始同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("saveActiveInfoByFeign lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
log.info("Feign gpsBsData===" + JSONUtil.toJsonStr(gpsBsData));
VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
// 保存激活信息
log.info("active device info gps:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
// 保存激活信息
log.info("active device info bs:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
}
}
});
} catch (Exception e) {
log.info("saveActiveInfoByFeign异常信息===" + e.getMessage(), e);
} finally {
log.info(Thread.currentThread().getName() + "关闭资源");
}
}
/**
* 通过sn同步设备激活信息
*
* @param sn 设备号
*/
public void saveActiveInfoByTask(String sn) {
if (StringUtils.isEmpty(sn)) {
log.info("sn is null");
return;
}
try {
log.info("saveActiveInfoByTask 开始同步激活信息,sn===" + sn);
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getFirstGpsBsBatch(Lists.newArrayList(sn));
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("saveActiveInfoByTask lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
log.info("Task gpsBsData===" + JSONUtil.toJsonStr(gpsBsData));
VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (ObjectUtil.isNotNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
// 保存激活信息
log.info("active device info by task gps:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (ObjectUtil.isNotNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
// 保存激活信息
log.info("active device info by task bs:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
}
}
});
} catch (Exception e) {
log.info("saveActiveInfoByTask异常信息===" + e.getMessage(), e);
} finally {
log.info(Thread.currentThread().getName() + "关闭资源");
}
}
}