|
|
@@ -40,29 +40,13 @@ public class RadarRealTimeDataUpdateTask { |
|
|
|
@Value("${task.switch.is-open}") |
|
|
|
private boolean flag; |
|
|
|
|
|
|
|
@Value("${task.gps-data-pull.domain}") |
|
|
|
private String domain; |
|
|
|
|
|
|
|
@Value("${task.gps-data-pull.real-time-data-url}") |
|
|
|
private String realTimeDataUrl; |
|
|
|
|
|
|
|
@Value("${task.gps-data-pull.key}") |
|
|
|
private String key; |
|
|
|
|
|
|
|
private final GpsDataPullManage gpsDataPullManage; |
|
|
|
private final IRadarOriginalDataService radarOriginalDataService; |
|
|
|
private final RadarDataHelper radarDataHelper; |
|
|
|
private final IRadarDataService radarDataService; |
|
|
|
|
|
|
|
@Value("${spring.datasource.url}") |
|
|
|
private String dataBaseUrl; |
|
|
|
@Value("${spring.datasource.username}") |
|
|
|
private String username; |
|
|
|
@Value("${spring.datasource.password}") |
|
|
|
private String password; |
|
|
|
|
|
|
|
// 定时更新雷达实时数据 每1分钟一次 |
|
|
|
@Scheduled(cron = "0 */1 * * * ?") |
|
|
|
|
|
|
|
public void doTask() { |
|
|
|
if (!flag){ |
|
|
|
log.info("雷达实时数据更新定时任务未开启!"); |
|
|
@@ -78,7 +62,6 @@ public class RadarRealTimeDataUpdateTask { |
|
|
|
Map<String, List<RadarOriginalData>> dataMap = dataList.stream().collect(Collectors.groupingBy(RadarOriginalData::getArmIp)); |
|
|
|
List<RadarOriginalData> finalDataList = Lists.newArrayList(); |
|
|
|
for (Map.Entry<String, List<RadarOriginalData>> entry : dataMap.entrySet()) { |
|
|
|
RadarOriginalData data = new RadarOriginalData(); |
|
|
|
// 针对每个盒子分别解析数据 |
|
|
|
String armIp = entry.getKey(); |
|
|
|
List<RadarOriginalData> datas = entry.getValue(); |
|
|
@@ -93,29 +76,21 @@ public class RadarRealTimeDataUpdateTask { |
|
|
|
List<RadarOriginalData> dataTypeDatas = dataTypeEntry.getValue(); |
|
|
|
if (Objects.nonNull(dataTypeDatas) && CollUtil.isNotEmpty(dataTypeDatas)) { |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 数据类型:{},数据量:{}",dataType,dataTypeDatas.size()); |
|
|
|
// 按照创建时间排序 取最新的一条数据 |
|
|
|
RadarOriginalData radarOriginalData1 = dataTypeDatas.get(0); |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 数据类型:{},最新数据:{}",dataType,radarOriginalData1); |
|
|
|
// 按照创建时间排序 取最新的一条数据 |
|
|
|
RadarOriginalData radarOriginalData = dataTypeDatas.stream().filter(d -> Objects.nonNull(d) && Objects.nonNull(d.getCreateOn())).max(Comparator.comparing(RadarOriginalData::getCreateOn)).orElse(null); |
|
|
|
if (Objects.nonNull(radarOriginalData)) { |
|
|
|
data.setId(radarOriginalData.getId()); |
|
|
|
data.setData(radarOriginalData.getData()); |
|
|
|
data.setArmIp(armIp); |
|
|
|
data.setDataType(dataType); |
|
|
|
finalDataList.add(data); |
|
|
|
// 解析所有原始数据 |
|
|
|
for (RadarOriginalData radarOriginalData : dataTypeDatas) { |
|
|
|
if (Objects.nonNull(radarOriginalData)) { |
|
|
|
RadarOriginalData data = new RadarOriginalData(); |
|
|
|
data.setId(radarOriginalData.getId()); |
|
|
|
data.setData(radarOriginalData.getData()); |
|
|
|
data.setArmIp(armIp); |
|
|
|
data.setDataType(dataType); |
|
|
|
finalDataList.add(data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
List<Long> finalIdList = finalDataList.stream().map(RadarOriginalData::getId).collect(Collectors.toList()); |
|
|
|
// 过滤出dataList中其他数据 从表中删除 |
|
|
|
if (CollUtil.isNotEmpty(finalIdList)) { |
|
|
|
List<Long> removeIdList = dataList.stream().map(RadarOriginalData::getId).filter(id -> !finalIdList.contains(id)).collect(Collectors.toList()); |
|
|
|
radarOriginalDataService.removeBatchByIds(removeIdList); |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 删除数据条数:{}", removeIdList.size()); |
|
|
|
} |
|
|
|
// 处理数据 |
|
|
|
List<RadarData> objects = Lists.newArrayList(); |
|
|
|
for (RadarOriginalData radarOriginalData : finalDataList) { |
|
|
@@ -172,8 +147,21 @@ public class RadarRealTimeDataUpdateTask { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 将dataList从表中删除 |
|
|
|
if (CollUtil.isNotEmpty(dataList)) { |
|
|
|
// 删除此次查询的数据 |
|
|
|
List<Long> removeIdList = dataList.stream().map(RadarOriginalData::getId).collect(Collectors.toList()); |
|
|
|
radarOriginalDataService.removeBatchByIds(removeIdList); |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 删除数据条数:{}", removeIdList.size()); |
|
|
|
} |
|
|
|
|
|
|
|
// 插入数据 |
|
|
|
if (CollUtil.isNotEmpty(objects)) { |
|
|
|
// 先删除旧数据 |
|
|
|
List<Long> idList = radarDataService.list(Wrappers.lambdaQuery(RadarData.class).select(RadarData::getId)).stream().map(RadarData::getId).collect(Collectors.toList()); |
|
|
|
radarDataService.removeBatchByIds(idList); |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 删除旧数据条数:{}",idList.size()); |
|
|
|
radarDataService.saveBatch(objects); |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 插入数据条数:{}",objects.size()); |
|
|
|
} |
|
|
@@ -181,60 +169,5 @@ public class RadarRealTimeDataUpdateTask { |
|
|
|
log.info("=========== 雷达实时数据更新 ======== 任务结束 {}s",stopWatch.getTotalTimeSeconds()); |
|
|
|
} |
|
|
|
|
|
|
|
private void truncateTable() { |
|
|
|
// SQL 语句清空表 |
|
|
|
String truncateTableSQL = "TRUNCATE TABLE nd_data_access_gps_real_time_data"; |
|
|
|
|
|
|
|
Connection conn = null; |
|
|
|
Statement stmt = null; |
|
|
|
|
|
|
|
try { |
|
|
|
// 加载数据库驱动 |
|
|
|
Class.forName("com.mysql.cj.jdbc.Driver"); |
|
|
|
// 建立连接 |
|
|
|
conn = DriverManager.getConnection(dataBaseUrl, username, password); |
|
|
|
// 创建 Statement 对象 |
|
|
|
stmt = conn.createStatement(); |
|
|
|
// 执行 SQL 语句 |
|
|
|
stmt.executeUpdate(truncateTableSQL); |
|
|
|
log.info("表已被清空,自增主键已重置。"); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Error truncating table: {}", e.getMessage()); |
|
|
|
} finally { |
|
|
|
// 关闭资源 |
|
|
|
try { |
|
|
|
if (stmt != null) { |
|
|
|
stmt.close(); |
|
|
|
} |
|
|
|
if (conn != null) { |
|
|
|
conn.close(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Error closing resources: {}", e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private Map<String, String> assemblyParams(String key, String sLon, String eLon, String sLat, String eLat) { |
|
|
|
long timeNow = System.currentTimeMillis() / 1000; |
|
|
|
String token = gpsDataPullManage.generateMD5Token(timeNow, key); |
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<>(); |
|
|
|
params.put("timeNow", String.valueOf(timeNow)); |
|
|
|
params.put("token", token); |
|
|
|
if (StringUtils.isNotBlank(sLon)) { |
|
|
|
params.put("Slon", sLon); |
|
|
|
} |
|
|
|
if (StringUtils.isNotBlank(eLon)) { |
|
|
|
params.put("Elon", eLon); |
|
|
|
} |
|
|
|
if (StringUtils.isNotBlank(sLat)) { |
|
|
|
params.put("Slat", sLat); |
|
|
|
} |
|
|
|
if (StringUtils.isNotBlank(eLat)) { |
|
|
|
params.put("Elat", eLat); |
|
|
|
} |
|
|
|
return params; |
|
|
|
} |
|
|
|
|
|
|
|
} |