feat(mqtt): 启用生产环境MQTT服务并优化GPS日志保存逻辑
- 在application-prod.yml中将MQTT服务enabled设置为true - 引入StringRedisTemplate用于Redis分布式锁控制-重构GPS轨迹日志保存方法,增加Redis原子锁控制频率 -仅在GPS速度不为0时保存轨迹日志,避免无效数据- 使用Redis setIfAbsent实现2分钟内同一设备只保存一次日志 - 完善日志记录,增加设备、速度、经纬度等关键信息 -优化异常处理,记录具体设备号便于问题追踪
This commit is contained in:
@@ -13,6 +13,7 @@ import com.gxwebsoft.hjm.entity.HjmGpsLog;
|
||||
import com.gxwebsoft.hjm.service.impl.HjmCarServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -44,6 +45,9 @@ public class GpsMessageProcessor {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Resource
|
||||
private HjmCarServiceImpl hjmCarServiceImpl;
|
||||
|
||||
@@ -128,11 +132,8 @@ public class GpsMessageProcessor {
|
||||
if (hjmCarService.updateByGpsNo(car)) {
|
||||
logger.debug("更新车辆GPS定位信息成功: {}", car.getCode());
|
||||
|
||||
// 检查是否需要保存GPS日志(避免重复保存)
|
||||
String keyByGpsNo = redisUtil.get(gps.getImei());
|
||||
if (StrUtil.isBlank(keyByGpsNo)) {
|
||||
saveGpsLogRecord(car, gps);
|
||||
}
|
||||
// 保存GPS轨迹日志(通过Redis控制频率)
|
||||
saveGpsLogRecord(car, gps);
|
||||
} else {
|
||||
logger.warn("更新车辆GPS定位信息失败: {}", car.getCode());
|
||||
}
|
||||
@@ -144,9 +145,31 @@ public class GpsMessageProcessor {
|
||||
|
||||
/**
|
||||
* 保存GPS轨迹日志记录
|
||||
* 使用Redis控制保存频率,同一GPS设备2分钟内只保存一次
|
||||
*/
|
||||
private void saveGpsLogRecord(HjmCar car, Gps gps) {
|
||||
try {
|
||||
// 只有速度不为0时才保存GPS轨迹
|
||||
if (gps.getSpeed().equals("0.000")) {
|
||||
logger.debug("GPS设备{}速度为0,跳过保存轨迹日志", gps.getImei());
|
||||
return;
|
||||
}
|
||||
|
||||
// 构造Redis key,使用GPS设备编号作为key
|
||||
String redisKey = "gps_log:" + gps.getImei();
|
||||
|
||||
// 使用Redis的setIfAbsent原子操作实现分布式锁
|
||||
// 只有key不存在时才设置成功,返回true;key已存在时返回false
|
||||
Boolean lockAcquired = stringRedisTemplate.opsForValue()
|
||||
.setIfAbsent(redisKey, "locked", 2, TimeUnit.MINUTES);
|
||||
|
||||
if (Boolean.FALSE.equals(lockAcquired)) {
|
||||
// 获取锁失败,说明2分钟内已经保存过
|
||||
logger.debug("GPS设备{}在2分钟内已保存过轨迹日志,跳过本次保存", gps.getImei());
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取锁成功,执行保存操作
|
||||
HjmGpsLog log = new HjmGpsLog();
|
||||
log.setTenantId(10519);
|
||||
log.setCarId(car.getId());
|
||||
@@ -157,15 +180,14 @@ public class GpsMessageProcessor {
|
||||
log.setHhmmss(gps.getHhmmss());
|
||||
log.setSpeed(gps.getSpeed());
|
||||
|
||||
if (!log.getSpeed().equals("0.000")) {
|
||||
// 使用Redis,key为GPS设备编号 2分钟内保存一次
|
||||
redisUtil.set(gps.getImei(), "1", 2L, TimeUnit.MINUTES);
|
||||
hjmGpsLogService.save(log);
|
||||
logger.debug("保存GPS轨迹日志成功: {}", gps.getImei());
|
||||
}
|
||||
// 保存数据库
|
||||
hjmGpsLogService.save(log);
|
||||
|
||||
logger.info("保存GPS轨迹日志成功: 设备={}, 速度={}, 经度={}, 纬度={}",
|
||||
gps.getImei(), gps.getSpeed(), gps.getLng(), gps.getLat());
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("保存GPS轨迹日志失败", e);
|
||||
logger.error("保存GPS轨迹日志失败: 设备={}", gps.getImei(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ socketio:
|
||||
|
||||
# MQTT配置
|
||||
mqtt:
|
||||
enabled: false # 启用MQTT服务
|
||||
enabled: true # 启用MQTT服务
|
||||
host: tcp://1.14.159.185:1883
|
||||
username: swdev
|
||||
password: Sw20250523
|
||||
|
||||
Reference in New Issue
Block a user