From 9bdfb26d3988aed940c95d10e3071dd711e4693e Mon Sep 17 00:00:00 2001 From: CMM <2198256324@qq.com> Date: Sun, 17 Nov 2024 21:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=B7=E8=BE=BE=E6=95=B0=E6=8D=AE=E5=AE=9E?= =?UTF-8?q?=E6=97=B6=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carapi/radar/helper/RadarDataHelper.java | 17 +-- .../carapi/radar/manage/RadarManage.java | 14 ++- .../radar/task/RadarRealTimeDataUpdateTask.java | 115 +++++---------------- .../carapi/road/manage/RoadMonitorManage.java | 2 +- .../src/main/resources/security/auth-prod.yml | 3 +- 5 files changed, 49 insertions(+), 102 deletions(-) diff --git a/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/helper/RadarDataHelper.java b/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/helper/RadarDataHelper.java index d7b7fe0..e65c61d 100644 --- a/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/helper/RadarDataHelper.java +++ b/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; diff --git a/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/manage/RadarManage.java b/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/manage/RadarManage.java index 9139d18..79ca1bb 100644 --- a/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/manage/RadarManage.java +++ b/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()); diff --git a/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/task/RadarRealTimeDataUpdateTask.java b/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/task/RadarRealTimeDataUpdateTask.java index 45efbfd..0f0479f 100644 --- a/ningda-yw-api/src/main/java/com/ningdatech/carapi/radar/task/RadarRealTimeDataUpdateTask.java +++ b/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> dataMap = dataList.stream().collect(Collectors.groupingBy(RadarOriginalData::getArmIp)); List finalDataList = Lists.newArrayList(); for (Map.Entry> entry : dataMap.entrySet()) { - RadarOriginalData data = new RadarOriginalData(); // 针对每个盒子分别解析数据 String armIp = entry.getKey(); List datas = entry.getValue(); @@ -93,29 +76,21 @@ public class RadarRealTimeDataUpdateTask { List 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 finalIdList = finalDataList.stream().map(RadarOriginalData::getId).collect(Collectors.toList()); - // 过滤出dataList中其他数据 从表中删除 - if (CollUtil.isNotEmpty(finalIdList)) { - List removeIdList = dataList.stream().map(RadarOriginalData::getId).filter(id -> !finalIdList.contains(id)).collect(Collectors.toList()); - radarOriginalDataService.removeBatchByIds(removeIdList); - log.info("=========== 雷达实时数据更新 ======== 删除数据条数:{}", removeIdList.size()); - } // 处理数据 List objects = Lists.newArrayList(); for (RadarOriginalData radarOriginalData : finalDataList) { @@ -172,8 +147,21 @@ public class RadarRealTimeDataUpdateTask { } } } + + // 将dataList从表中删除 + if (CollUtil.isNotEmpty(dataList)) { + // 删除此次查询的数据 + List removeIdList = dataList.stream().map(RadarOriginalData::getId).collect(Collectors.toList()); + radarOriginalDataService.removeBatchByIds(removeIdList); + log.info("=========== 雷达实时数据更新 ======== 删除数据条数:{}", removeIdList.size()); + } + // 插入数据 if (CollUtil.isNotEmpty(objects)) { + // 先删除旧数据 + List 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 assemblyParams(String key, String sLon, String eLon, String sLat, String eLat) { - long timeNow = System.currentTimeMillis() / 1000; - String token = gpsDataPullManage.generateMD5Token(timeNow, key); - - Map 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; - } } diff --git a/ningda-yw-api/src/main/java/com/ningdatech/carapi/road/manage/RoadMonitorManage.java b/ningda-yw-api/src/main/java/com/ningdatech/carapi/road/manage/RoadMonitorManage.java index e6fb689..8119386 100644 --- a/ningda-yw-api/src/main/java/com/ningdatech/carapi/road/manage/RoadMonitorManage.java +++ b/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()); } } } diff --git a/ningda-yw-api/src/main/resources/security/auth-prod.yml b/ningda-yw-api/src/main/resources/security/auth-prod.yml index b234df2..c967d5d 100644 --- a/ningda-yw-api/src/main/resources/security/auth-prod.yml +++ b/ningda-yw-api/src/main/resources/security/auth-prod.yml @@ -45,4 +45,5 @@ security: - /api/car-rpt/** - /open/api/** - /radar/** - - /gps/** \ No newline at end of file + - /gps/** + - /road-monitor/** \ No newline at end of file