提交围栏业务
parent
4fb4842cfb
commit
5dc24b931d
|
@ -40,5 +40,5 @@ public class ElePoint {
|
|||
/**
|
||||
* 车辆vin
|
||||
*/
|
||||
private Long carVinId;
|
||||
private String carVinId;
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@ package com.god.data.config;
|
|||
import com.god.common.redis.service.RedisService;
|
||||
import com.god.data.common.domain.ElePoint;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Configurable;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ public class KafkaConsumerConfig {
|
|||
//设置kafka服务器地址
|
||||
props.put("bootstrap.servers", KAFKA_CON);
|
||||
//每个消费者分配独立的组号
|
||||
props.put("group.id", "fst1");
|
||||
props.put("group.id", "g2");
|
||||
//如果value合法,则自动提交偏移量
|
||||
props.put("enable.auto.commit", "true");
|
||||
//设置多久一次更新被消费消息的偏移量
|
||||
|
|
|
@ -5,12 +5,16 @@ import lombok.extern.log4j.Log4j2;
|
|||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.apache.kafka.common.TopicPartition;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
package com.god.data.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.god.common.redis.service.RedisService;
|
||||
import com.god.data.common.domain.CarMessage;
|
||||
import com.god.data.common.domain.ElePoint;
|
||||
import com.god.data.common.domain.Point;
|
||||
import com.god.data.service.EventService;
|
||||
import com.god.data.utils.GeofencingUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* 电子围栏数据判断事件
|
||||
*
|
||||
* @author LouZhiSHuo
|
||||
* @Date 2023/11/30 19:07
|
||||
**/
|
||||
@Log4j2
|
||||
@Service(value = "Fence")
|
||||
public class ElectronicFenceAnalysis implements EventService {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
@Override
|
||||
public void insert() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CarMessage carMessage) {
|
||||
|
||||
log.info("电子围栏解析事件开始执行,接受的报文数据是:{}",carMessage);
|
||||
|
||||
//获取redis车辆绑定的围栏
|
||||
List<ElePoint> cacheList = redisService.getCacheList("Fence" + carMessage.getVin());
|
||||
|
||||
//循环取出该车辆绑定的围栏信息
|
||||
for (ElePoint elePoint : cacheList) {
|
||||
//获取电子围栏坐标
|
||||
String fenceLocation = elePoint.getFenceLocation();
|
||||
//字符串格式:
|
||||
// '116.336567,39.928286;
|
||||
// 116.401369,39.904587;
|
||||
// 116.330731,39.896224;'
|
||||
//切割围栏字符串
|
||||
String[] split = fenceLocation.split(";");
|
||||
//转换成List类型
|
||||
List<String> strList = Arrays.asList(split);
|
||||
//转换成Point类型存储经纬度
|
||||
List<Point> pointList = strList.stream().map(
|
||||
str -> {
|
||||
String[] pointSplit = str.split(",");
|
||||
return new Point(Double.parseDouble(pointSplit[0]),Double.parseDouble(pointSplit[1]));
|
||||
}).collect(Collectors.toList());
|
||||
//获取车辆当前位置
|
||||
Point point = new Point();
|
||||
point.setLng(carMessage.getLongitude().doubleValue());
|
||||
point.setLat(carMessage.getLatitude().doubleValue());
|
||||
//通过围栏算法进行判断当前点是否在围栏内
|
||||
boolean inPolygon = GeofencingUtils.isInPolygon(point, pointList);
|
||||
//判断当前围栏类型[驶入]
|
||||
if (elePoint.getDriveStatus() == 1) {
|
||||
//如果车辆当前位置在围栏内,则触发车辆事件
|
||||
if (inPolygon){
|
||||
//触发 , 将消息时间存储RabbitMQ中
|
||||
//生产消息
|
||||
rabbitTemplate.convertAndSend("OUT_FENCE", JSON.toJSON(elePoint), message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
}
|
||||
}
|
||||
//判断当前围栏类型[驶出]
|
||||
if (elePoint.getDriveStatus() == 2) {
|
||||
//如果车辆当前位置未在围栏内,则触发车辆事件
|
||||
if (!inPolygon) {
|
||||
//触发 将消息存储RabbitMQ中
|
||||
//生产消息
|
||||
rabbitTemplate.convertAndSend("IN_FENCE",JSON.toJSON(elePoint),message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
log.info("电子围栏事件执行完毕");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String event) {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,97 +1,92 @@
|
|||
package com.god.data.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.god.common.redis.service.RedisService;
|
||||
import com.god.data.common.domain.CarMessage;
|
||||
import com.god.data.common.domain.ElePoint;
|
||||
import com.god.data.common.domain.Point;
|
||||
import com.god.data.service.EventService;
|
||||
import com.god.data.utils.GeofencingUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 电子围栏数据解析事件
|
||||
* @Author fst
|
||||
* @date 2023/11/27 20:43
|
||||
*/
|
||||
@Service(value = "Fence")
|
||||
@Log4j2
|
||||
public class ElectronicFenceEvent implements EventService {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
public void insert() {
|
||||
|
||||
}
|
||||
@Override
|
||||
public void execute(CarMessage carMessage) {
|
||||
log.info("电子围栏数据解析事件执行");
|
||||
//从redis中获取当前车辆的电子围栏数据
|
||||
List<ElePoint> list = redisService.getCacheList("Fence" + carMessage.getVin());
|
||||
//根据当前经纬度和电子围栏数据进行判断,调用算法工具类
|
||||
for (ElePoint elePoint : list) {
|
||||
//获取电子围栏数据
|
||||
String s = elePoint.getFenceLocation();
|
||||
String[] split = s.split(";");
|
||||
//转换为字符串集合,集合中字符串是 经度,纬度
|
||||
List<String> strList = Arrays.asList(split);
|
||||
//把字符串集合转换为Point集合,point存放经纬度
|
||||
List<Point> points = strList.stream()
|
||||
.map(str -> {
|
||||
String[] split1 = str.split(",");
|
||||
return new Point(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
|
||||
}).collect(Collectors.toList());
|
||||
//获取经纬度
|
||||
Point point = new Point();
|
||||
//把当前车辆的经纬度赋值给point对象
|
||||
point.setLng(carMessage.getLongitude().doubleValue());
|
||||
point.setLat(carMessage.getLatitude().doubleValue());
|
||||
//判断车辆当前是否在点赞围栏中
|
||||
boolean inPolygon = GeofencingUtils.isInPolygon(point, points);
|
||||
//判断当前车辆是驶入/驶出围栏
|
||||
if (elePoint.getDriveStatus().equals(1)){
|
||||
//如果不在围栏范围内则,触发驶出电子围栏事件 ,存入rabbitmq中
|
||||
if (inPolygon){
|
||||
rabbitTemplate.convertAndSend("OUT_FENCE", JSON.toJSON(elePoint), message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
}
|
||||
}
|
||||
if (elePoint.getDriveStatus().equals(2)){
|
||||
//如果不在围栏范围内则,触发驶入电子围栏事件 ,存入rabbitmq中
|
||||
if (!inPolygon){
|
||||
rabbitTemplate.convertAndSend("IN_FENCE", JSON.toJSON(elePoint), message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
}
|
||||
}
|
||||
log.info("电子围栏事件结束测试");
|
||||
rabbitTemplate.convertAndSend("FENCE_STATUS", "电子围栏事件结束测试", message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String event) {
|
||||
|
||||
}
|
||||
}
|
||||
//package com.god.data.service.impl;
|
||||
//
|
||||
//import com.alibaba.fastjson2.JSON;
|
||||
//import com.god.common.redis.service.RedisService;
|
||||
//import com.god.data.common.domain.CarMessage;
|
||||
//import com.god.data.common.domain.ElePoint;
|
||||
//import com.god.data.common.domain.Point;
|
||||
//import com.god.data.service.EventService;
|
||||
//import com.god.data.utils.GeofencingUtils;
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.Arrays;
|
||||
//import java.util.List;
|
||||
//import java.util.UUID;
|
||||
//import java.util.stream.Collectors;
|
||||
//
|
||||
///**
|
||||
// * 电子围栏数据解析事件
|
||||
// * @Author fst
|
||||
// * @date 2023/11/27 20:43
|
||||
// */
|
||||
//@Service(value = "Fence")
|
||||
//@Log4j2
|
||||
//public class ElectronicFenceEvent implements EventService {
|
||||
//
|
||||
// @Autowired
|
||||
// private RedisService redisService;
|
||||
//
|
||||
// @Autowired
|
||||
// private RabbitTemplate rabbitTemplate;
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public void insert() {
|
||||
//
|
||||
// }
|
||||
// @Override
|
||||
// public void execute(CarMessage carMessage) {
|
||||
// log.info("电子围栏数据解析事件执行");
|
||||
// //从redis中获取当前车辆的电子围栏数据
|
||||
// List<ElePoint> list = redisService.getCacheList("Fence" + carMessage.getVin());
|
||||
// //根据当前经纬度和电子围栏数据进行判断,调用算法工具类
|
||||
// for (ElePoint elePoint : list) {
|
||||
// //获取电子围栏数据
|
||||
// String s = elePoint.getFenceLocation();
|
||||
// String[] split = s.split(";");
|
||||
// //转换为字符串集合,集合中字符串是 经度,纬度
|
||||
// List<String> strList = Arrays.asList(split);
|
||||
// //把字符串集合转换为Point集合,point存放经纬度
|
||||
// List<Point> points = strList.stream()
|
||||
// .map(str -> {
|
||||
// String[] split1 = str.split(",");
|
||||
// return new Point(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
|
||||
// }).collect(Collectors.toList());
|
||||
// //获取经纬度
|
||||
// Point point = new Point();
|
||||
// //把当前车辆的经纬度赋值给point对象
|
||||
// point.setLng(carMessage.getLongitude().doubleValue());
|
||||
// point.setLat(carMessage.getLatitude().doubleValue());
|
||||
// //判断车辆当前是否在点赞围栏中
|
||||
// boolean inPolygon = GeofencingUtils.isInPolygon(point, points);
|
||||
// //判断当前车辆是驶入/驶出围栏
|
||||
// if (elePoint.getDriveStatus().equals(1)){
|
||||
// //如果不在围栏范围内则,触发驶出电子围栏事件 ,存入rabbitmq中
|
||||
// if (inPolygon){
|
||||
// rabbitTemplate.convertAndSend("OUT_FENCE", JSON.toJSON(elePoint), message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// if (elePoint.getDriveStatus().equals(2)){
|
||||
// //如果不在围栏范围内则,触发驶入电子围栏事件 ,存入rabbitmq中
|
||||
// if (!inPolygon){
|
||||
// rabbitTemplate.convertAndSend("IN_FENCE", JSON.toJSON(elePoint), message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void remove(String event) {
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
|
Loading…
Reference in New Issue