feat: 优化代码

server_five_liuyunhu
lijiayao 2024-04-08 19:50:45 +08:00
parent 599d89b6e4
commit 05b2baf32e
14 changed files with 264 additions and 259 deletions

View File

@ -8,7 +8,6 @@ import org.springframework.stereotype.Component;
* @Date: 2024/4/7
* @Description:
*/
public interface AnalyzeEventContents {
/**

View File

@ -1,5 +1,6 @@
package com.couplet.analyze.common.event;
import com.couplet.common.domain.request.RealTimeDataRequest;
import com.couplet.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
@ -38,6 +39,12 @@ public class AnalyzeEventCache {
public void removeEvent(String vin,String eventName){
redisService.deleteSet(encode(vin), eventName);
}
/**
*
*/
public void queryEvent(String vin, final RealTimeDataRequest eventName){
redisService.setCacheObject(encode(vin), eventName);
}
/**
*
*
@ -47,4 +54,5 @@ public class AnalyzeEventCache {
return redisService.getCacheSet(encode(vin));
}
}

View File

@ -1,203 +1,203 @@
package com.couplet.analyze.msg.consumer;
import com.alibaba.fastjson.JSONObject;
import com.couplet.analyze.msg.domain.CoupletMsgData;
import com.couplet.analyze.msg.utils.MsgUtils;
import com.couplet.common.domain.CoupletTroubleCode;
import com.couplet.common.redis.service.RedisService;
import com.couplet.remote.RemoteTroubleService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
/**
* @author DongXiaoDong
* @version 1.0
* @date 2024/4/6 15:37
* @description
*/
@Component
@Slf4j
public class CodeConsumer {
//package com.couplet.analyze.msg.consumer;
//
//import com.alibaba.fastjson.JSONObject;
//import com.couplet.analyze.msg.domain.CoupletMsgData;
//import com.couplet.analyze.msg.utils.MsgUtils;
//import com.couplet.common.domain.CoupletTroubleCode;
//import com.couplet.common.redis.service.RedisService;
//import com.couplet.remote.RemoteTroubleService;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.rabbit.annotation.Queue;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.amqp.core.Message;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.concurrent.CompletableFuture;
//
///**
// * @author DongXiaoDong
// * @version 1.0
// * @date 2024/4/6 15:37
// * @description
// */
//@Component
//@Slf4j
//public class CodeConsumer {
//// @Autowired
//// private RedisTemplate<String, String> redisTemplate;
//
// @Autowired
// private RedisTemplate<String, String> redisTemplate;
@Autowired
private RedisService redisService;
@Autowired
private RemoteTroubleService remoteTroubleService;
@RabbitListener(queuesToDeclare = {@Queue("couplet-code-queue")})
public void sendLogQueueConsumer(Message message, CoupletMsgData msgData, Channel channel) {
log.info("日志队列:{},接收到的消息:{},开始消费...","couplet-code-queue", JSONObject.toJSONString(msgData));
long start = System.currentTimeMillis();
String messageId = message.getMessageProperties().getMessageId();
try {
boolean addToSetIfNotExists = redisService.addToSetIfNotExists("couplet-code-queue", messageId);
if (addToSetIfNotExists) {
//异步保存记录
CompletableFuture.runAsync(() -> {
CoupletTroubleCode troubleCode = new CoupletTroubleCode();
troubleCode.setTroubleStartTime(new Date());
troubleCode.setTroubleVin(msgData.getVin());
// 随机生成故障码
String faultCode = MsgUtils.generateGTA();
troubleCode.setTroubleCode(faultCode);
switch (msgData.getVehicleStatus()) {
case 0:
troubleCode.setTroublePosition("190");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getChargingStatus()) {
case 0:
troubleCode.setTroublePosition("191");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getOperatingStatus()) {
case 0:
troubleCode.setTroublePosition("192");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getSocStatus()) {
case 0:
troubleCode.setTroublePosition("193");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getChargingEnergyStorageStatus()) {
case 0:
troubleCode.setTroublePosition("194");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getDriveMotorStatus()) {
case 0:
troubleCode.setTroublePosition("195");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getPositionStatus()) {
case 0:
troubleCode.setTroublePosition("196");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getEasStatus()) {
case 0:
troubleCode.setTroublePosition("197");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getPtcStatus()) {
case 0:
troubleCode.setTroublePosition("198");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getEpsStatus()) {
case 0:
troubleCode.setTroublePosition("199");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getAbsStatus()) {
case 0:
troubleCode.setTroublePosition("200");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getMcuStatus()) {
case 0:
troubleCode.setTroublePosition("201");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getHeatingStatus()) {
case 0:
troubleCode.setTroublePosition("202");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getBatteryStatus()) {
case 0:
troubleCode.setTroublePosition("203");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getBatteryInsulationStatus()) {
case 0:
troubleCode.setTroublePosition("204");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getDcdcStatus()) {
case 0:
troubleCode.setTroublePosition("205");
troubleCode.setProcessingState(0);
break;
default:
switch (msgData.getChgStatus()) {
case 0:
troubleCode.setTroublePosition("206");
troubleCode.setProcessingState(0);
break;
default:
// do nothing
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
break;
}
remoteTroubleService.newFaultData(troubleCode);
});
}
long end = System.currentTimeMillis();
log.info("日志队列:{},接收到的消息:{},消费完成,耗时:{}毫秒","couplet-code-queue", JSONObject.toJSONString(msgData), (end-start));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
// private RedisService redisService;
// @Autowired
// private RemoteTroubleService remoteTroubleService;
//
// @RabbitListener(queuesToDeclare = {@Queue("couplet-code-queue")})
// public void sendLogQueueConsumer(Message message, CoupletMsgData msgData, Channel channel) {
// log.info("日志队列:{},接收到的消息:{},开始消费...","couplet-code-queue", JSONObject.toJSONString(msgData));
// long start = System.currentTimeMillis();
//
// String messageId = message.getMessageProperties().getMessageId();
//
// try {
// boolean addToSetIfNotExists = redisService.addToSetIfNotExists("couplet-code-queue", messageId);
// if (addToSetIfNotExists) {
// //异步保存记录
// CompletableFuture.runAsync(() -> {
// CoupletTroubleCode troubleCode = new CoupletTroubleCode();
// troubleCode.setTroubleStartTime(new Date());
// troubleCode.setTroubleVin(msgData.getVin());
// // 随机生成故障码
// String faultCode = MsgUtils.generateGTA();
// troubleCode.setTroubleCode(faultCode);
//
// switch (msgData.getVehicleStatus()) {
// case 0:
// troubleCode.setTroublePosition("190");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getChargingStatus()) {
// case 0:
// troubleCode.setTroublePosition("191");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getOperatingStatus()) {
// case 0:
// troubleCode.setTroublePosition("192");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getSocStatus()) {
// case 0:
// troubleCode.setTroublePosition("193");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getChargingEnergyStorageStatus()) {
// case 0:
// troubleCode.setTroublePosition("194");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getDriveMotorStatus()) {
// case 0:
// troubleCode.setTroublePosition("195");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getPositionStatus()) {
// case 0:
// troubleCode.setTroublePosition("196");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getEasStatus()) {
// case 0:
// troubleCode.setTroublePosition("197");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getPtcStatus()) {
// case 0:
// troubleCode.setTroublePosition("198");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getEpsStatus()) {
// case 0:
// troubleCode.setTroublePosition("199");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getAbsStatus()) {
// case 0:
// troubleCode.setTroublePosition("200");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getMcuStatus()) {
// case 0:
// troubleCode.setTroublePosition("201");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getHeatingStatus()) {
// case 0:
// troubleCode.setTroublePosition("202");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getBatteryStatus()) {
// case 0:
// troubleCode.setTroublePosition("203");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getBatteryInsulationStatus()) {
// case 0:
// troubleCode.setTroublePosition("204");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getDcdcStatus()) {
// case 0:
// troubleCode.setTroublePosition("205");
// troubleCode.setProcessingState(0);
// break;
// default:
// switch (msgData.getChgStatus()) {
// case 0:
// troubleCode.setTroublePosition("206");
// troubleCode.setProcessingState(0);
// break;
// default:
// // do nothing
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// break;
// }
// remoteTroubleService.newFaultData(troubleCode);
// });
// }
// long end = System.currentTimeMillis();
// log.info("日志队列:{},接收到的消息:{},消费完成,耗时:{}毫秒","couplet-code-queue", JSONObject.toJSONString(msgData), (end-start));
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
//}

View File

@ -4,8 +4,6 @@ import com.couplet.analyze.msg.domain.CoupletMsgData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
/**
* @Author: LiJiaYao
* @Date: 2024/4/2
@ -23,6 +21,6 @@ public interface IncidentMapper {
* vin
*/
// CoupletMsgData queryByIncident(RealTimeDataRequest realTimeDataRequest);
CoupletMsgData queryByIncident(@Param("vin") String vin, @Param("createTime") Date createTime);
CoupletMsgData queryByIncident(String vin);
}

View File

@ -1,5 +1,7 @@
package com.couplet.analyze.msg.model;
import com.couplet.analyze.common.contents.AnalyzeEventContents;
import com.couplet.analyze.common.event.AnalyzeEventCache;
import com.couplet.analyze.msg.domain.CoupletMsgData;
import com.couplet.analyze.msg.service.IncidentService;
import com.couplet.common.core.exception.vehicle.VehicleException;
@ -36,7 +38,6 @@ public class ModelsKafkaMessage {
@Autowired
private AnalyzeEventCache analyzeEventCache;
/**
*
*
@ -52,9 +53,7 @@ public class ModelsKafkaMessage {
//创建消费者
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
try {
//订阅主题
consumer.subscribe(Collections.singletonList(TOPIC_NAME));
//持续消费消息

View File

@ -13,14 +13,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @Author: LiJiaYao
* @Date: 2024/4/2
* @Description:
*/
@Service("electronic-fence")
@Service("electronic_fence")
@Log4j2
public class ElectronicFenceServiceImpl implements IncidentService {
@ -39,30 +41,21 @@ public class ElectronicFenceServiceImpl implements IncidentService {
log.info("电子围栏事件开始.......");
String fenceKey="fence";
// redisService.getCacheSet()
ArrayList<Fence> fences = new ArrayList<>();
String fenceKey = "车辆vin和电子围栏:" + coupletMsgData.getVin();
if (redisService.hasKey(fenceKey)) {
Set<Fence> cacheSet = redisService.getCacheSet(fenceKey);
log.info("电子围栏事件redis存在.......");
// redisService.getCacheObject();
// for (String s : fence) {
// Fence parseObject = JSON.parseObject(s, Fence.class);
// fences.add(parseObject);
// }
// jingdu;
// longitude;
// weidu;
// latitude;
for (Fence data : fences) {
String fenceLongitudeLatitude = data.getFenceLongitudeLatitude();
for (Fence fence : cacheSet) {
String fenceLongitudeLatitude = fence.getFenceLongitudeLatitude();
/**
*
*
*/
String[] split = fenceLongitudeLatitude.split(";");
for (String s : split) {
String[] strings = s.split(",");
if (strings.length == 2) {
// 经度
@ -83,12 +76,9 @@ public class ElectronicFenceServiceImpl implements IncidentService {
}
}
}
// log.info("更改的电子围栏内容是:"+fence);
log.info("电子围栏事件结束.......");
}
log.info("电子围栏事件结束.......");
}
/**

View File

@ -4,23 +4,21 @@ import com.couplet.analyze.msg.domain.CoupletMsgData;
import com.couplet.analyze.msg.mapper.IncidentMapper;
import com.couplet.analyze.msg.service.IncidentService;
import com.couplet.analyze.msg.service.impl.realTimeData.RealTimeJudge;
import com.couplet.common.core.utils.StringUtils;
import com.couplet.common.domain.request.RealTimeDataRequest;
import com.couplet.common.redis.service.RedisService;
import com.couplet.remote.RemoteRealTimeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* @Author: LiJiaYao
* @Date: 2024/4/2
* @Description:
*/
@Service("real-time-data")
@Service("real_time_data")
@Log4j2
public class RealTimeDataServiceImpl implements IncidentService {
@ -32,8 +30,6 @@ public class RealTimeDataServiceImpl implements IncidentService {
@Autowired
private RedisService redisService;
@Autowired
private RemoteRealTimeService remoteRealTimeService;
/**
*
@ -46,17 +42,20 @@ public class RealTimeDataServiceImpl implements IncidentService {
log.info("实时数据事件开始.....");
RealTimeDataRequest cacheObject = redisService.getCacheObject("vin:" + coupletMsgData.getVin());
//判断是否有缓存数据
if (RealTimeJudge.isJudge(coupletMsgData.getVin())) {
log.info("有实时数据,值为:[{}]开始传输实时数据", coupletMsgData.getVin());
//判断数据是否一致,
if (RealTimeJudge.addRealTime(cacheObject)) {
log.info("[{}]有缓存数据,值为:[{}],且缓存数据与实时数据一致,开始传输实时数据", coupletMsgData.getVin(), cacheObject);
CoupletMsgData query = incidentMapper.queryByIncident(coupletMsgData.getVin(), new Date());
redisService.setCacheObject("vin:" + cacheObject.getVin(), query);
CoupletMsgData query = incidentMapper.queryByIncident(coupletMsgData.getVin());
redisService.setCacheObject("vin:query:" + cacheObject.getVin(), query);
redisService.expire("vin:"+cacheObject.getVin(),10, TimeUnit.MINUTES);
} else {
log.info("[{}]有缓存数据,值为:[{}],且缓存数据与实时数据不一致,开始传输实时数据", coupletMsgData.getVin(), cacheObject);
}
if (RealTimeJudge.isJudge(coupletMsgData.getVin())) {
log.info("有实时数据,值为:[{}]开始传输实时数据", coupletMsgData.getVin());
}
log.info("[{}]开始传输实时数据", coupletMsgData.getVin());
log.info("实时数据事件结束.....");

View File

@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
* @Date: 2024/4/2
* @Description:
*/
@Service("stored-event")
@Service("stored_event")
@Log4j2
public class StoredEventServiceImpl implements IncidentService {
@Autowired

View File

@ -72,7 +72,11 @@
</insert>
<select id="queryByIncident" resultType="com.couplet.analyze.msg.domain.CoupletMsgData"
parameterType="com.couplet.common.domain.request.RealTimeDataRequest">
SELECT * FROM couplet_msg_data WHERE vin=#{vin} and create_time = #{createTime}
SELECT *
FROM couplet_msg_data
WHERE vin = #{vin}
ORDER BY create_time DESC
LIMIT 1;
</select>

View File

@ -101,6 +101,10 @@
<groupId>com.couplet</groupId>
<artifactId>couplet-modules-mq</artifactId>
</dependency>
<dependency>
<groupId>com.couplet</groupId>
<artifactId>couplet-common-event</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,6 +1,8 @@
package com.couplet.business.server.service.impl;
import afu.org.checkerframework.checker.units.qual.A;
import com.alibaba.fastjson.JSON;
import com.couplet.analyze.common.event.AnalyzeEventCache;
import com.couplet.business.server.mapper.VehicleDetectionMapper;
import com.couplet.business.server.service.VehicleDetectionService;
import com.couplet.business.server.service.VehicleManageService;
@ -17,6 +19,7 @@ import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author fufanrui
@ -31,10 +34,14 @@ public class VehicleDetectionServiceImpl implements VehicleDetectionService{
private VehicleDetectionMapper vehicleDetectionMapper;
@Autowired
private RedisService redisService;
@Autowired
private AnalyzeEventCache analyzeEventCache;
@Override
public List<Vehicle> detectionList() {
return vehicleDetectionMapper.detectionList();
List<Vehicle> vehicles = vehicleDetectionMapper.detectionList();
return vehicles;
}
@Override
@ -43,8 +50,9 @@ public class VehicleDetectionServiceImpl implements VehicleDetectionService{
Long userId = SecurityUtils.getUserId();
realTimeDataRequest.setVin(vin);
realTimeDataRequest.setUserId(userId);
// analyzeEventCache.queryEvent("查询实时数据"+vin,realTimeDataRequest);
// redisService.expire("查询实时数据"+vin,4,TimeUnit.MINUTES);
redisService.setVinAndUserId(realTimeDataRequest);
}
@Override

View File

@ -16,11 +16,9 @@ spring:
discovery:
# 服务注册地址
server-addr: 121.89.211.230:8848
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
config:
# 配置中心地址
server-addr: 121.89.211.230:8848
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
# 配置文件格式
file-extension: yml
# 共享配置

View File

@ -15,7 +15,6 @@ spring:
discovery:
# 服务注册地址
server-addr: 121.89.211.230:8848
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
config:
# 配置中心地址
server-addr: 121.89.211.230:8848
@ -24,7 +23,6 @@ spring:
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
main:
allow-bean-definition-overriding: true
logging: