浏览代码

雷达数据实时更新任务修改

master
CMM 1 个月前
父节点
当前提交
9bdfb26d39
共有 5 个文件被更改,包括 49 次插入102 次删除
  1. +10
    -7
      ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/helper/RadarDataHelper.java
  2. +12
    -2
      ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/manage/RadarManage.java
  3. +24
    -91
      ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/task/RadarRealTimeDataUpdateTask.java
  4. +1
    -1
      ningda-yw-api/src/main/java/com/ningdatech/carapi/road/manage/RoadMonitorManage.java
  5. +2
    -1
      ningda-yw-api/src/main/resources/security/auth-prod.yml

+ 10
- 7
ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/helper/RadarDataHelper.java 查看文件

@@ -1,6 +1,7 @@
package com.ningdatech.carapi.radar.helper;

import com.google.common.collect.Lists;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.carapi.radar.model.vo.RadarDataVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@@ -468,13 +469,15 @@ public class RadarDataHelper {
// 把第7和第8个字节拼接成16进制字符串 再转换为整数
byte[] millisecondData = extractHexData(segment, 30, 2);
String millisecondHexString = byteArrayToHexString(millisecondData);
int millisecondValue = Integer.parseInt(millisecondHexString, 16);
// 创建位掩码来提取第0到9位
int millisecondMask = 0x3FF;
// 使用位掩码提取第0到第9位
millisecondValue = millisecondValue & millisecondMask;
// 数据 0~999 对应 0~999 毫秒
System.out.println("毫秒:" + millisecondValue);
if (!StrPool.EMPTY.equals(millisecondHexString)) {
int millisecondValue = Integer.parseInt(millisecondHexString, 16);
// 创建位掩码来提取第0到9位
int millisecondMask = 0x3FF;
// 使用位掩码提取第0到第9位
millisecondValue = millisecondValue & millisecondMask;
// 数据 0~999 对应 0~999 毫秒
System.out.println("毫秒:" + millisecondValue);
}
// 拼接成LocalDateTime 类型的数据
String localDateTime = yearValue + "-" + monthValue + "-" + dayValue + " " + hourValue + ":" + minuteValue + ":" + secondValue;
return localDateTime;


+ 12
- 2
ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/manage/RadarManage.java 查看文件

@@ -216,8 +216,18 @@ public class RadarManage {
}
return radarDataList.stream().map(d -> {
RadarObjectDataVO vo = new RadarObjectDataVO();
// TODO 待接入雷达数据后 转换为中文 当前以原始编号返回
vo.setTargetLane(d.getTargetLane());
// 转换为中文
String targetLane = d.getTargetLane();
// 如果是以1开头 表示上行(靠近雷达) 拼接上行 + 所属车道编号
if (StringUtils.startsWith(targetLane, "1")){
vo.setTargetLane("上行" + targetLane);
}
// 如果是以3开头 表示下行(远离雷达) 拼接下行 + 所属车道编号
else if (StringUtils.startsWith(targetLane, "3")){
vo.setTargetLane("下行" + targetLane);
}else {
vo.setTargetLane("未知车道");
}
vo.setCarType(RadarCarTypeEnum.getDescByCode(d.getCarType()));
vo.setTargetSpeed(d.getTargetSpeed());
vo.setTargetLatitude(d.getTargetLatitude());


+ 24
- 91
ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/task/RadarRealTimeDataUpdateTask.java 查看文件

@@ -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;
}

}

+ 1
- 1
ningda-yw-api/src/main/java/com/ningdatech/carapi/road/manage/RoadMonitorManage.java 查看文件

@@ -576,7 +576,7 @@ public class RoadMonitorManage {
fileInputStream.close();
servletOutputStream.close();
} catch (IOException e) {
throw new BizException("文件下载失败! {}",e);
log.error("文件下载失败! {}",e.getMessage());
}
}
}

+ 2
- 1
ningda-yw-api/src/main/resources/security/auth-prod.yml 查看文件

@@ -45,4 +45,5 @@ security:
- /api/car-rpt/**
- /open/api/**
- /radar/**
- /gps/**
- /gps/**
- /road-monitor/**

正在加载...
取消
保存