feat: 电子围栏报警

server_five_liuyunhu
lijiayao 2024-04-08 11:26:11 +08:00
parent 366d97e8d2
commit 5c65c974c6
10 changed files with 219 additions and 174 deletions

View File

@ -63,6 +63,13 @@ public interface RemoteVehicleService {
*/ */
@PostMapping("/vehicleAndLogo/queryByLogoIds/{vehicleId}") @PostMapping("/vehicleAndLogo/queryByLogoIds/{vehicleId}")
public Result<List<Long>> queryByLogoIds(@PathVariable("vehicleId") Long vehicleId); public Result<List<Long>> queryByLogoIds(@PathVariable("vehicleId") Long vehicleId);
/**
* id
* @param
* @return
*/
// @PostMapping("/vehicleAndLogo/queryByLogoIds/{vehicleId}")
// public Result<List<Long>> queryByLogoIds(@PathVariable("vehicleId") Long vehicleId);
@PostMapping("/list") @PostMapping("/list")
public Result list(@RequestBody VehicleListParams listParams); public Result list(@RequestBody VehicleListParams listParams);

View File

@ -1,27 +1,31 @@
package com.couplet.analyze.common.contents; package com.couplet.analyze.common.contents;
import org.springframework.stereotype.Component;
/** /**
* @Author: LiJiaYao * @Author: LiJiaYao
* @Date: 2024/4/7 * @Date: 2024/4/7
* @Description: * @Description:
*/ */
@Component
public class AnalyzeEventContents { public class AnalyzeEventContents {
/** /**
* *
*/ */
String BREAKDOWN = "breakdown"; static final String BREAKDOWN = "breakdown";
/** /**
* *
*/ */
String ELECTRONIC_FENCE = "electronic-fence"; static final String ELECTRONIC_FENCE = "electronic-fence";
/** /**
* *
*/ */
String REAL_TIME_DATA = "real-time-data"; static final String REAL_TIME_DATA = "real-time-data";
/** /**
* *
*/ */
String STORED_EVENT = "stored-event"; static final String STORED_EVENT = "stored-event";
} }

View File

@ -43,8 +43,7 @@ public class AnalyzeEventCache {
* *
* @return * @return
*/ */
public Set<Object> getEventList(String vin){ public Set<String> getEventList(String vin){
return redisService.getCacheSet(encode(vin)); return redisService.getCacheSet(encode(vin));
} }

View File

@ -182,7 +182,32 @@ public class RedisService {
} }
return setOperation; return setOperation;
} }
/**
* Set
*
* @param key
* @param setValue
*
* @return
*/
public <T> BoundSetOperations<String, T> setCacheSet (final String key, final T setValue) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
setOperation.add(setValue);
return setOperation;
}
/**
* Set
*
* @param key
* @param setValue
*
* @return
*/
public <T> void deleteSet(String key, String setValue) {
BoundSetOperations setOperations = redisTemplate.boundSetOps(key);
setOperations.remove(setValue);
}
/** /**
* set * set
* *
@ -194,6 +219,8 @@ public class RedisService {
return redisTemplate.opsForSet().members(key); return redisTemplate.opsForSet().members(key);
} }
/** /**
* Map * Map
* *
@ -280,4 +307,6 @@ public class RedisService {
String key = "vin:"+realTimeDataRequest.getVin()+"userId:"+realTimeDataRequest.getUserId(); String key = "vin:"+realTimeDataRequest.getVin()+"userId:"+realTimeDataRequest.getUserId();
redisTemplate.opsForValue().set(key, realTimeDataRequest); redisTemplate.opsForValue().set(key, realTimeDataRequest);
} }
} }

View File

@ -96,6 +96,10 @@
<groupId>org.springframework.kafka</groupId> <groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId> <artifactId>spring-kafka</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.couplet</groupId>
<artifactId>couplet-common-business</artifactId>
</dependency>
</dependencies> </dependencies>

View File

@ -1,150 +1,150 @@
package com.couplet.analyze.msg.consumer; //package com.couplet.analyze.msg.consumer;
//
import com.alibaba.fastjson.JSONObject; //import com.alibaba.fastjson.JSONObject;
import com.couplet.analyze.msg.domain.CoupletMsgData; //import com.couplet.analyze.msg.domain.CoupletMsgData;
import com.couplet.analyze.msg.utils.MsgUtils; //import com.couplet.analyze.msg.utils.MsgUtils;
import com.couplet.common.domain.CoupletTroubleCode; //import com.couplet.common.domain.CoupletTroubleCode;
import com.couplet.common.redis.service.RedisService; //import com.couplet.common.redis.service.RedisService;
import com.couplet.remote.RemoteTroubleService; //import com.couplet.remote.RemoteTroubleService;
import com.rabbitmq.client.Channel; //import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue; //import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener; //import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; //import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.amqp.core.Message; //import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component; //import org.springframework.stereotype.Component;
//
import java.util.Date; //import java.util.Date;
import java.util.concurrent.CompletableFuture; //import java.util.concurrent.CompletableFuture;
//
/** ///**
* @author DongXiaoDong // * @author DongXiaoDong
* @version 1.0 // * @version 1.0
* @date 2024/4/6 15:37 // * @date 2024/4/6 15:37
* @description // * @description
*/ // */
@Component //@Component
@Slf4j //@Slf4j
public class CodeConsumer { //public class CodeConsumer {
//// @Autowired
//// private RedisTemplate<String, String> redisTemplate;
//
// @Autowired // @Autowired
// private RedisTemplate<String, String> redisTemplate; // private RedisService redisService;
// @Autowired
@Autowired // private RemoteTroubleService remoteTroubleService;
private RedisService redisService; //
@Autowired // @RabbitListener(queuesToDeclare = {@Queue("couplet-code-queue")})
private RemoteTroubleService remoteTroubleService; // public void sendLogQueueConsumer(Message message, CoupletMsgData msgData, Channel channel) {
// log.info("日志队列:{},接收到的消息:{},开始消费...","couplet-code-queue", JSONObject.toJSONString(msgData));
@RabbitListener(queuesToDeclare = {@Queue("couplet-code-queue")}) // long start = System.currentTimeMillis();
public void sendLogQueueConsumer(Message message, CoupletMsgData msgData, Channel channel) { //
log.info("日志队列:{},接收到的消息:{},开始消费...","couplet-code-queue", JSONObject.toJSONString(msgData)); // String messageId = message.getMessageProperties().getMessageId();
long start = System.currentTimeMillis(); //
// try {
String messageId = message.getMessageProperties().getMessageId(); // boolean addToSetIfNotExists = redisService.addToSetIfNotExists("couplet-code-queue", messageId);
// if (addToSetIfNotExists) {
try { // //异步保存记录
boolean addToSetIfNotExists = redisService.addToSetIfNotExists("couplet-code-queue", messageId); // CompletableFuture.runAsync(() -> {
if (addToSetIfNotExists) { // CoupletTroubleCode troubleCode = new CoupletTroubleCode();
//异步保存记录 // troubleCode.setTroubleStartTime(new Date());
CompletableFuture.runAsync(() -> { // troubleCode.setTroubleVin(msgData.getVin());
CoupletTroubleCode troubleCode = new CoupletTroubleCode(); // // 随机生成故障码
troubleCode.setTroubleStartTime(new Date()); // String faultCode = MsgUtils.generateGTA();
troubleCode.setTroubleVin(msgData.getVin()); // troubleCode.setTroubleCode(faultCode);
// 随机生成故障码 //
String faultCode = MsgUtils.generateGTA(); // // 检查车辆状态若为0则设置故障位置为"190"
troubleCode.setTroubleCode(faultCode); // if(msgData.getVehicleStatus() == 0) {
// troubleCode.setTroublePosition("190");
// 检查车辆状态若为0则设置故障位置为"190" // }
if(msgData.getVehicleStatus() == 0) { //
troubleCode.setTroublePosition("190"); // // 检查充电状态若为0则设置故障位置为"191"
} // if (msgData.getChargingStatus() == 0) {
// troubleCode.setTroublePosition("191");
// 检查充电状态若为0则设置故障位置为"191" // }
if (msgData.getChargingStatus() == 0) { //
troubleCode.setTroublePosition("191"); // // 检查运行状态若为0则设置故障位置为"192"
} // if (msgData.getOperatingStatus() == 0) {
// troubleCode.setTroublePosition("192");
// 检查运行状态若为0则设置故障位置为"192" // }
if (msgData.getOperatingStatus() == 0) { //
troubleCode.setTroublePosition("192"); // // 检查电池荷电状态SOC, 若为0则设置故障位置为"193"
} // if (msgData.getSocStatus() == 0) {
// troubleCode.setTroublePosition("193");
// 检查电池荷电状态SOC, 若为0则设置故障位置为"193" // }
if (msgData.getSocStatus() == 0) { //
troubleCode.setTroublePosition("193"); // // 检查充电能源存储状态若为0则设置故障位置为"194"
} // if (msgData.getChargingEnergyStorageStatus() == 0) {
// troubleCode.setTroublePosition("194");
// 检查充电能源存储状态若为0则设置故障位置为"194" // }
if (msgData.getChargingEnergyStorageStatus() == 0) { //
troubleCode.setTroublePosition("194"); // // 检查驱动电机状态若为0则设置故障位置为"195"
} // if (msgData.getDriveMotorStatus() == 0) {
// troubleCode.setTroublePosition("195");
// 检查驱动电机状态若为0则设置故障位置为"195" // }
if (msgData.getDriveMotorStatus() == 0) { //
troubleCode.setTroublePosition("195"); // // 检查定位状态若为0则设置故障位置为"196"
} // if (msgData.getPositionStatus() == 0) {
// troubleCode.setTroublePosition("196");
// 检查定位状态若为0则设置故障位置为"196" // }
if (msgData.getPositionStatus() == 0) { //
troubleCode.setTroublePosition("196"); // // 检查电子驻车系统EAS状态若为0则设置故障位置为"197"
} // if (msgData.getEasStatus() == 0) {
// troubleCode.setTroublePosition("197");
// 检查电子驻车系统EAS状态若为0则设置故障位置为"197" // }
if (msgData.getEasStatus() == 0) { //
troubleCode.setTroublePosition("197"); // // 检查PTC正温度系数热敏电阻状态若为0则设置故障位置为"198"
} // if (msgData.getPtcStatus() == 0) {
// troubleCode.setTroublePosition("198");
// 检查PTC正温度系数热敏电阻状态若为0则设置故障位置为"198" // }
if (msgData.getPtcStatus() == 0) { //
troubleCode.setTroublePosition("198"); // // 检查电动助力转向系统EPS状态若为0则设置故障位置为"199"
} // if (msgData.getEpsStatus() == 0) {
// troubleCode.setTroublePosition("199");
// 检查电动助力转向系统EPS状态若为0则设置故障位置为"199" // }
if (msgData.getEpsStatus() == 0) { //
troubleCode.setTroublePosition("199"); // // 检查防抱死制动系统ABS状态若为0则设置故障位置为"200"
} // if (msgData.getAbsStatus() == 0) {
// troubleCode.setTroublePosition("200");
// 检查防抱死制动系统ABS状态若为0则设置故障位置为"200" // }
if (msgData.getAbsStatus() == 0) { //
troubleCode.setTroublePosition("200"); // // 检查主控制器MCU状态若为0则设置故障位置为"201"
} // if (msgData.getMcuStatus() == 0) {
// troubleCode.setTroublePosition("201");
// 检查主控制器MCU状态若为0则设置故障位置为"201" // }
if (msgData.getMcuStatus() == 0) { //
troubleCode.setTroublePosition("201"); // // 检查加热状态若为0则设置故障位置为"202"
} // if (msgData.getHeatingStatus() == 0) {
// troubleCode.setTroublePosition("202");
// 检查加热状态若为0则设置故障位置为"202" // }
if (msgData.getHeatingStatus() == 0) { //
troubleCode.setTroublePosition("202"); // // 检查电池状态若为0则设置故障位置为"203"
} // if (msgData.getBatteryStatus() == 0) {
// troubleCode.setTroublePosition("203");
// 检查电池状态若为0则设置故障位置为"203" // }
if (msgData.getBatteryStatus() == 0) { //
troubleCode.setTroublePosition("203"); // // 检查电池绝缘状态若为0则设置故障位置为"204"
} // if (msgData.getBatteryInsulationStatus() == 0) {
// troubleCode.setTroublePosition("204");
// 检查电池绝缘状态若为0则设置故障位置为"204" // }
if (msgData.getBatteryInsulationStatus() == 0) { //
troubleCode.setTroublePosition("204"); // // 检查直流-直流转换器DC/DC状态若为0则设置故障位置为"205"
} // if (msgData.getDcdcStatus() == 0) {
// troubleCode.setTroublePosition("205");
// 检查直流-直流转换器DC/DC状态若为0则设置故障位置为"205" // }
if (msgData.getDcdcStatus() == 0) { //
troubleCode.setTroublePosition("205"); // // 检查充电机CHG状态若为0则设置故障位置为"206"
} // if (msgData.getChgStatus() == 0) {
// troubleCode.setTroublePosition("206");
// 检查充电机CHG状态若为0则设置故障位置为"206" // }
if (msgData.getChgStatus() == 0) { // remoteTroubleService.newFaultData(troubleCode);
troubleCode.setTroublePosition("206"); // });
} // }
remoteTroubleService.newFaultData(troubleCode); // long end = System.currentTimeMillis();
}); // log.info("日志队列:{},接收到的消息:{},消费完成,耗时:{}毫秒","couplet-code-queue", JSONObject.toJSONString(msgData), (end-start));
} // } catch (Exception e) {
long end = System.currentTimeMillis(); // throw new RuntimeException(e);
log.info("日志队列:{},接收到的消息:{},消费完成,耗时:{}毫秒","couplet-code-queue", JSONObject.toJSONString(msgData), (end-start)); // }
} catch (Exception e) { // }
throw new RuntimeException(e); //}
}
}
}

View File

@ -1,6 +1,7 @@
package com.couplet.analyze.msg.model; package com.couplet.analyze.msg.model;
import com.couplet.analyze.common.contents.AnalyzeEventContents; 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.domain.CoupletMsgData;
import com.couplet.analyze.msg.service.IncidentService; import com.couplet.analyze.msg.service.IncidentService;
import com.couplet.common.core.exception.vehicle.VehicleException; import com.couplet.common.core.exception.vehicle.VehicleException;
@ -34,19 +35,16 @@ public class ModelsKafkaMessage {
private static final String TOPIC_NAME = "online"; private static final String TOPIC_NAME = "online";
private static final String BOOTSTRAP_SERVERS = "39.103.133.136:9092"; private static final String BOOTSTRAP_SERVERS = "39.103.133.136:9092";
static ArrayList<String> strings = new ArrayList<>() { @Autowired
{ private AnalyzeEventContents analyzeEventContents;
add("breakdown"); @Autowired
add("electronic-fence"); private AnalyzeEventCache analyzeEventCache;
add("real-time-data");
add("stored-event");
}
};
// @Autowired // @Autowired
// private RabbitTemplate rabbitTemplate; // private RabbitTemplate rabbitTemplate;
/** /**
* *
*
* @return * @return
*/ */
@Scheduled(fixedDelay = 50) @Scheduled(fixedDelay = 50)
@ -64,7 +62,6 @@ public class ModelsKafkaMessage {
//订阅主题 //订阅主题
consumer.subscribe(Collections.singletonList(TOPIC_NAME)); consumer.subscribe(Collections.singletonList(TOPIC_NAME));
//持续消费消息 //持续消费消息
while (true) { while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
@ -74,11 +71,12 @@ public class ModelsKafkaMessage {
List<CoupletMsgData> coupletMsgDataList = sendMsg(str); List<CoupletMsgData> coupletMsgDataList = sendMsg(str);
for (CoupletMsgData msgData : coupletMsgDataList) { for (CoupletMsgData msgData : coupletMsgDataList) {
log.info("解析到车辆数据:{}", msgData); log.info("解析到车辆数据:{}", msgData);
for (String string : strings) { Set<String> eventList = analyzeEventCache.getEventList(msgData.getVin());
for (String string : eventList) {
IncidentService incidentService = SpringUtils.getBean(string); IncidentService incidentService = SpringUtils.getBean(string);
incidentService.incident(msgData); incidentService.incident(msgData);
} }
//发送消息 // 发送消息
// rabbitTemplate.convertAndSend("couplet-code-queue",msgData,message -> { // rabbitTemplate.convertAndSend("couplet-code-queue",msgData,message -> {
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString()); // message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
// return message; // return message;
@ -87,7 +85,7 @@ public class ModelsKafkaMessage {
try { try {
sleep(100); sleep(100);
} catch (Exception e) { } catch (Exception e) {
throw new VehicleException("睡眠失败"+e); throw new VehicleException("睡眠失败" + e);
} }
} }
}); });

View File

@ -4,6 +4,9 @@ import com.couplet.analyze.msg.domain.CoupletMsgData;
import com.couplet.analyze.msg.mapper.IncidentMapper; import com.couplet.analyze.msg.mapper.IncidentMapper;
import com.couplet.analyze.msg.service.IncidentService; import com.couplet.analyze.msg.service.IncidentService;
import com.couplet.analyze.msg.service.impl.realTimeData.RealTimeJudge; 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 lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
@ -25,7 +28,7 @@ public class RealTimeDataServiceImpl implements IncidentService {
private IncidentMapper incidentMapper; private IncidentMapper incidentMapper;
@Autowired @Autowired
private StringRedisTemplate redisTemplate; private RedisService redisService;
/** /**
* *
@ -37,6 +40,11 @@ public class RealTimeDataServiceImpl implements IncidentService {
log.info("实时数据事件开始....."); log.info("实时数据事件开始.....");
RealTimeDataRequest cacheObject = redisService.getCacheObject("vin:" + coupletMsgData.getVin());
if (StringUtils.isArray(cacheObject)){
log.info("[{}]有缓存数据,值为:[{}]", coupletMsgData.getVin(), cacheObject);
}
if (RealTimeJudge.isJudge(coupletMsgData.getVin())){ if (RealTimeJudge.isJudge(coupletMsgData.getVin())){
log.info("有实时数据,值为:[{}]开始传输实时数据", coupletMsgData.getVin()); log.info("有实时数据,值为:[{}]开始传输实时数据", coupletMsgData.getVin());
} }

View File

@ -15,7 +15,6 @@ spring:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 121.89.211.230:8848 server-addr: 121.89.211.230:8848
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 121.89.211.230:8848 server-addr: 121.89.211.230:8848
@ -24,7 +23,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}
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
main: main:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
rabbitmq: rabbitmq:

View File

@ -15,7 +15,6 @@ spring:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 121.89.211.230:8848 server-addr: 121.89.211.230:8848
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 121.89.211.230:8848 server-addr: 121.89.211.230:8848
@ -24,7 +23,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}
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
main: main:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
logging: logging:
@ -38,7 +36,7 @@ mqtt:
# broker: mqtt://115.159.47.13:1883 # broker: mqtt://115.159.47.13:1883
username: username:
password: password:
clientId: fluxMq clientId: xiaoYao
qos: 0 qos: 0
topic: test topic: xiaoYao