fix: 地图显示功能优化。

This commit is contained in:
tianyongbao
2025-10-19 12:34:21 +08:00
parent 3c8f10a7a4
commit 9e0f104527
5 changed files with 155 additions and 22 deletions

View File

@@ -39,15 +39,6 @@ spring:
# 共享配置 # 共享配置
shared-configs: shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 管理端点配置
management:
health:
db:
enabled: false # 临时禁用数据库健康检查
endpoint:
health:
show-details: always
# 用于服务监控可在线查看日志,该配置用于生产环境 # 用于服务监控可在线查看日志,该配置用于生产环境
logging: logging:
file: file:

View File

@@ -44,12 +44,32 @@ public class TdEngineController extends BaseController
return AjaxResult.error("数据列表不能为空"); return AjaxResult.error("数据列表不能为空");
} }
try { try {
tdEngineService.batchInsertDeviceSensorData(dataList); int count = tdEngineService.batchInsertDeviceSensorData(dataList);
return AjaxResult.success("成功插入 " + dataList.size() + " 条数据"); return AjaxResult.success("成功插入 " + count + " 条数据");
} catch (Exception e) { } catch (Exception e) {
logger.error("批量插入数据失败", e); logger.error("批量插入数据失败", e);
return AjaxResult.error("批量插入数据失败: " + e.getMessage()); return AjaxResult.error("批量插入数据失败: " + e.getMessage());
} }
} }
/**
* 根据设备ID获取历史轨迹按间隔采样
*/
@GetMapping("/trajectory/{deviceId}")
@ApiOperation(value = "获取设备历史轨迹", notes = "根据设备ID查询历史移动轨迹数据按间隔采样返回指定数量的数据")
public AjaxResult getDeviceTrajectory(
@PathVariable("deviceId") String deviceId,
@RequestParam(value = "startTime", required = false) String startTime,
@RequestParam(value = "endTime", required = false) String endTime,
@RequestParam(value = "sampleCount", required = false, defaultValue = "10") Integer sampleCount)
{
try {
List<DeviceSensorData> trajectory = tdEngineService.getDeviceTrajectory(deviceId, startTime, endTime, sampleCount);
return AjaxResult.success(trajectory);
} catch (Exception e) {
logger.error("获取设备轨迹失败", e);
return AjaxResult.error("获取设备轨迹失败: " + e.getMessage());
}
}
} }

View File

@@ -59,5 +59,17 @@ public interface TdEngineMapper
*/ */
int batchInsertDeviceSensorData(@Param("dataList") List<DeviceSensorData> dataList); int batchInsertDeviceSensorData(@Param("dataList") List<DeviceSensorData> dataList);
/**
* 根据设备ID获取历史轨迹
*
* @param deviceId 设备ID
* @param startTime 开始时间
* @param endTime 结束时间
* @return 历史轨迹数据列表
*/
List<DeviceSensorData> getDeviceTrajectory(@Param("deviceId") String deviceId,
@Param("startTime") String startTime,
@Param("endTime") String endTime);
} }

View File

@@ -2,29 +2,36 @@ package com.intc.tdengine.service;
import com.intc.tdengine.domain.DeviceSensorData; import com.intc.tdengine.domain.DeviceSensorData;
import com.intc.tdengine.mapper.TdEngineMapper; import com.intc.tdengine.mapper.TdEngineMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @ClassName TdEngineService * @ClassName TdEngineService
* @Author YaphetS * @Author YaphetS
* @Date 2023/4/17 17:17 * @Date 2023/4/17 17:17
* @Version 1.0 * @Version 1.0
* @Description TODO * @Description TDEngine数据库服务
*/ */
@Slf4j
@Service @Service
public class TdEngineService { public class TdEngineService {
@Resource @Resource
private TdEngineMapper tdEngineMapper; private TdEngineMapper tdEngineMapper;
public List<DeviceSensorData> getLastData() { public List<DeviceSensorData> getLastData() {
List<DeviceSensorData> list = new ArrayList<>(); try {
list = tdEngineMapper.getLastData(); return tdEngineMapper.getLastData();
return list; } catch (Exception e) {
log.error("获取最新数据失败", e);
return new ArrayList<>();
}
} }
/** /**
@@ -33,7 +40,100 @@ public class TdEngineService {
* @param dataList 数据列表 * @param dataList 数据列表
* @return 影响行数 * @return 影响行数
*/ */
public void batchInsertDeviceSensorData(List<DeviceSensorData> dataList) { @Transactional(rollbackFor = Exception.class)
tdEngineMapper.batchInsertDeviceSensorData(dataList); public int batchInsertDeviceSensorData(List<DeviceSensorData> dataList) {
if (dataList == null || dataList.isEmpty()) {
log.warn("批量插入数据列表为空");
return 0;
}
try {
// 为空的时间字段赋值当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date());
for (DeviceSensorData data : dataList) {
if (!StringUtils.hasText(data.getTime())) {
data.setTime(currentTime);
}
if (!StringUtils.hasText(data.getCreateTime())) {
data.setCreateTime(currentTime);
}
// 检查并创建设备对应的子表
if (StringUtils.hasText(data.getDeviceId())) {
try {
tdEngineMapper.createTable(data);
log.debug("创建或确认设备表: t_{}", data.getDeviceId());
} catch (Exception e) {
// 表已存在时会抛异常,忽略即可
log.debug("设备表 t_{} 已存在或创建失败: {}", data.getDeviceId(), e.getMessage());
}
}
}
log.info("开始批量插入 {} 条数据", dataList.size());
int result = tdEngineMapper.batchInsertDeviceSensorData(dataList);
log.info("批量插入成功,影响 {} 行", result);
return result;
} catch (Exception e) {
log.error("批量插入数据失败,数据量: {}", dataList.size(), e);
throw new RuntimeException("批量插入数据失败: " + e.getMessage(), e);
}
}
/**
* 根据设备ID获取历史轨迹按固定间隔采样
*
* @param deviceId 设备ID
* @param startTime 开始时间
* @param endTime 结束时间
* @param sampleCount 采样数量(返回多少条数据)
* @return 历史轨迹数据列表
*/
public List<DeviceSensorData> getDeviceTrajectory(String deviceId, String startTime, String endTime, Integer sampleCount) {
if (!StringUtils.hasText(deviceId)) {
log.warn("设备ID为空无法查询轨迹");
return new ArrayList<>();
}
// 设置默认采样数量为10
if (sampleCount == null || sampleCount <= 0) {
sampleCount = 10;
}
try {
log.info("查询设备 {} 的历史轨迹,时间范围: {} - {},采样数量: {}", deviceId, startTime, endTime, sampleCount);
// 先查询所有数据
List<DeviceSensorData> allData = tdEngineMapper.getDeviceTrajectory(deviceId, startTime, endTime);
int totalCount = allData.size();
log.info("设备 {} 查询到 {} 条原始轨迹数据", deviceId, totalCount);
// 如果数据少于等于采样数量,直接返回
if (totalCount <= sampleCount) {
return allData;
}
// 按固定间隔采样
List<DeviceSensorData> sampledData = new ArrayList<>();
double interval = (double) (totalCount - 1) / (sampleCount - 1); // (n-1)个间隔n个点
for (int i = 0; i < sampleCount; i++) {
int index = (int) Math.round(i * interval);
// 确保索引不越界
if (index >= totalCount) {
index = totalCount - 1;
}
sampledData.add(allData.get(index));
}
log.info("设备 {} 采样后返回 {} 条轨迹数据", deviceId, sampledData.size());
return sampledData;
} catch (Exception e) {
log.error("查询设备轨迹失败设备ID: {}", deviceId, e);
return new ArrayList<>();
}
} }
} }

View File

@@ -31,13 +31,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<insert id="batchInsertDeviceSensorData"> <insert id="batchInsertDeviceSensorData">
insert into insert into
<foreach collection="dataList" item="data" open="" close="" separator=" "> <foreach collection="dataList" item="data" open="" close="" separator=" ">
`bds`.t_#{data.deviceId} bds.t_${data.deviceId}
using bds.sensor_data using bds.sensor_data
tags(#{data.deviceId}) tags(#{data.deviceId})
(time, createTime, longitude, latitude) values (#{data.time}, ${data.createTime}, ${data.longitude}, ${data.latitude}) (time, createTime, longitude, latitude) values (#{data.time}, #{data.createTime}, #{data.longitude}, #{data.latitude})
</foreach> </foreach>
</insert> </insert>
@@ -45,10 +45,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
create table if not exists create table if not exists
`bds`.t_#{deviceId} bds.t_${deviceId}
using bds.sensor_data using bds.sensor_data
tags(#{deviceId}) tags(#{deviceId})
</update> </update>
<select id="getDeviceTrajectory" resultType="DeviceSensorData">
select time, createTime, longitude, latitude, deviceId
from bds.t_${deviceId}
<where>
<if test="startTime != null and startTime != ''">AND `time` >= #{startTime}</if>
<if test="endTime != null and endTime != ''">AND `time` &lt;= #{endTime}</if>
</where>
order by time asc
</select>
</mapper> </mapper>