master
fst1996 2023-12-05 16:34:36 +08:00
parent 869f4391a2
commit dd49a1109f
4 changed files with 121 additions and 55 deletions

View File

@ -0,0 +1,17 @@
package com.god.business.common.constant;
/**
* @description:
* @Author fst
* @date 2023/12/5 10:48
*/
public class EventConstant {
//电子围栏事件
public static final String FENCE = "Fence";
//故障报警事件
public static final String FAULT = "FaultAlarm";
//实时轨迹事件
public static final String REAL = "RealTimeTrajectory";
}

View File

@ -0,0 +1,48 @@
package com.god.base.server.config;
import com.god.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashSet;
/**
*
* @Author fst
* @date 2023/12/5 10:10
*/
@Component
public class EventInit {
@Autowired
private RedisService redisService;
/**
*
* @param carVinId
* @param event
*/
public void insertEvent(String carVinId,String event){
//获取车辆事件集合
HashSet<String> set = redisService.getCacheMapValue("EVENT", carVinId);
//添加事件
set.add(event);
//更新事件
redisService.setCacheMapValue("EVENT",carVinId,set);
}
/**
*
* @param carVinId
* @param event
*/
public void delEvent(String carVinId,String event){
//获取车辆事件集合
HashSet<String> set = redisService.getCacheMapValue("EVENT", carVinId);
//添加事件
set.remove(event);
//更新事件
redisService.setCacheMapValue("EVENT",carVinId,set);
}
}

View File

@ -1,35 +1,44 @@
//package com.god.base.server.consumer;
//
//import lombok.extern.java.Log;
//import lombok.extern.log4j.Log4j;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.amqp.rabbit.annotation.Queue;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.amqp.rabbit.annotation.RabbitListeners;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//
///**
// * Description:
// *
// * @Author: sun-cool-boy
// * @Date: 2023/11/29
// * @info:
// */
//@Component
//@Log4j2
//public class RabbitConsumer {
//
// @RabbitListener(queuesToDeclare = {@Queue("OUT_FENCE")})
// public void one(String msg){
// log.info("监听到消息:{} , 队列名:{}",msg,"OUT_FENCE");
// System.out.println(msg);
// }
//
// @RabbitListener(queuesToDeclare = {@Queue("IN_FENCE")})
// public void two(String msg){
// log.info("监听到消息:{} , 队列名:{}",msg,"IN_FENCE");
// System.out.println(msg);
// }
//}
package com.god.base.server.consumer;
import com.rabbitmq.client.Channel;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
/**
* Description:
*
* @Author: sun-cool-boy
* @Date: 2023/11/29
* @info:
*/
@Component
@Log4j2
public class RabbitConsumer {
@RabbitListener(queuesToDeclare = {@Queue("OUT_FENCE")})
public void one(String msg, Message message, Channel channel){
try {
log.info("监听到消息:{} , 队列名:{}",msg,"OUT_FENCE");
log.info("车辆驶出围栏,消息是:{}",msg);
//手动确认
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@RabbitListener(queuesToDeclare = {@Queue("IN_FENCE")})
public void two(String msg){
log.info("监听到消息:{} , 队列名:{}",msg,"IN_FENCE");
log.info("车辆驶入围栏,消息是:{}",msg);
}
}

View File

@ -1,14 +1,18 @@
package com.god.base.server.service.impl;
import com.god.base.server.config.EventInit;
import com.god.business.common.constant.EventConstant;
import com.god.common.core.domain.Result;
import com.god.common.redis.service.RedisService;
import com.god.base.server.service.RealTimeTrajectoryServer;
import io.swagger.v3.oas.annotations.servers.Server;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
@ -23,7 +27,10 @@ import java.util.List;
@Service
public class RealTimeTrajectoryServerImpl implements RealTimeTrajectoryServer{
public static final String EVENT = "event";//事件存储前缀
@Autowired
private EventInit eventInit;
public static final String EVENT = "EVENT";//事件存储前缀
public static final String REAL_TIME_TRAJECTORY = "RealTimeTrajectory";//实时轨迹事件
@Resource
@ -36,18 +43,8 @@ public class RealTimeTrajectoryServerImpl implements RealTimeTrajectoryServer{
*/
@Override
public Result addRealTimeTrajectoryEvent(String vin) {
//拿到 vin对应的绑定事件
List<Object> cacheList = redisService.getCacheList(EVENT + vin);
//类型转换成String
List<String> list = cacheList
.stream()
.map(String::valueOf).
toList();
//添加该事件
list.add(REAL_TIME_TRAJECTORY);
//重新存入
long l = redisService.setCacheList(EVENT + vin, cacheList);
return l > cacheList.size() ? Result.success():Result.error();
eventInit.insertEvent(vin,EventConstant.REAL);
return Result.success();
}
/**
@ -57,12 +54,7 @@ public class RealTimeTrajectoryServerImpl implements RealTimeTrajectoryServer{
*/
@Override
public Result delRealTimeTrajectoryEvent(String vin) {
List<String> list = redisService.getCacheList(EVENT + vin)
.stream()
.filter(obj -> !REAL_TIME_TRAJECTORY.equals(String.valueOf(obj)))
.map(String::valueOf)
.toList();
long l = redisService.setCacheList(EVENT + vin, list);
return l > 0 ?Result.success():Result.error();
eventInit.delEvent(vin,EventConstant.REAL);
return Result.success();
}
}