新增围栏监听功能,判断车辆是否驶入指定区域
- 添加FenceGroup类,用于判断点是否在多边形围栏内 - 实现FenceListener监听器,监听车辆位置事件 - 在CarFenceGroupsResp中增加启动状态字段 - 通过新增的监听功能,判断车辆是否驶入或驶出指定区域xxy
parent
2a85b2bb67
commit
8e2ba733d9
|
@ -97,5 +97,11 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-modules-enterprise-common</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package com.muyu.carData.listener;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.muyu.carData.event.EsSaveEvent;
|
||||
import com.muyu.carData.event.EventListener;
|
||||
import com.muyu.carData.util.CacheUtil;
|
||||
import com.muyu.carData.util.FenceGroup;
|
||||
import com.muyu.domain.req.FenceGroupReq;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.muyu.domain.resp.CarFenceGroupsResp;
|
||||
import java.util.List;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class FenceListener implements EventListener {
|
||||
|
||||
@Autowired
|
||||
private CacheUtil cacheUtil;
|
||||
|
||||
|
||||
/**
|
||||
* 监听事件
|
||||
* @param event
|
||||
*/
|
||||
@Override
|
||||
public void onEvent(EsSaveEvent event) {
|
||||
try {
|
||||
log.info("开始事件运行监听");
|
||||
|
||||
JSONObject data = event.getData();
|
||||
String vin = (String) data.get("VIN");
|
||||
Integer longitude = (Integer) data.get("longitude");
|
||||
Integer latitude = (Integer) data.get("latitude");
|
||||
HashMap<String, Object> map = (HashMap<String, Object>) cacheUtil.get(vin);
|
||||
if (map != null) {
|
||||
CarFenceGroupsResp fence = (CarFenceGroupsResp) map.get("fence");
|
||||
String status = fence.getStatus(); //启动状态(1.驶入 2.驶出)
|
||||
String fenceText = fence.getFenceText();
|
||||
String[] split = fenceText.split(";");
|
||||
ArrayList<Point> points = new ArrayList<>();
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
String[] latitudeAndLongitude = split[i].split(",");
|
||||
Integer longi = Integer.valueOf(latitudeAndLongitude[0]);
|
||||
Integer lati = Integer.valueOf(latitudeAndLongitude[1]);
|
||||
Point point = new Point(longi, lati);
|
||||
points.add(point);
|
||||
}
|
||||
List<List<Point>> fences = List.of(
|
||||
points
|
||||
);
|
||||
FenceGroup fenceGroup = new FenceGroup(fences);
|
||||
Point testPoint = new Point(longitude, latitude); // 应该在第一个围栏内
|
||||
System.out.println(fenceGroup.isPointInFenceGroup(testPoint)); // 应该输出true
|
||||
}
|
||||
}catch(NumberFormatException e){
|
||||
log.error("数据格式异常,数据发送错误",e);
|
||||
throw new RuntimeException(e);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EsSaveEvent event) {
|
||||
onEvent(event);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.muyu.carData.util;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
public class FenceGroup {
|
||||
private List<List<Point>> fences;
|
||||
|
||||
public FenceGroup(List<List<Point>> fences){
|
||||
this.fences = fences;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断点是否在围栏组内。
|
||||
* @param point 检查的点。
|
||||
* @return 如果点在任何围栏内返回true,否则返回false。
|
||||
*/
|
||||
public boolean isPointInFenceGroup(Point point) {
|
||||
for (List<Point> fence : fences) {
|
||||
if (isPointInsidePolygon(point, fence)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 辅助方法,判断点是否在多边形内。
|
||||
* @param point 检查的点。
|
||||
* @param polygon 多边形的顶点列表。
|
||||
* @return 如果点在多边形内返回true,否则返回false。
|
||||
*/
|
||||
private boolean isPointInsidePolygon(Point point, List<Point> polygon) {
|
||||
int n = polygon.size();
|
||||
boolean inside = false;
|
||||
|
||||
Point p1 = polygon.get(0);
|
||||
for (int i = 1; i <= n; i++) {
|
||||
Point p2 = polygon.get(i % n);
|
||||
if (point.getY() > Math.min(p1.getY(), p2.getY())) {
|
||||
if (point.getY() <= Math.max(p1.getY(), p2.getY())) {
|
||||
if (point.getX() <= Math.max(p1.getX(), p2.getX())) {
|
||||
if (p1.getY() != p2.getY()) {
|
||||
double xinters = (point.getY() - p1.getY()) * (p2.getX() - p1.getX()) / (p2.getY() - p1.getY()) + p1.getX();
|
||||
if (p1.getX() == p2.getX() || point.getX() <= xinters) {
|
||||
inside = !inside;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
p1 = p2;
|
||||
}
|
||||
return inside;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -31,7 +31,7 @@ public class CarFenceGroupsResp {
|
|||
*/
|
||||
private Integer priority;
|
||||
/**
|
||||
* 启动状态
|
||||
* 启动状态(1.驶入 2.驶出)
|
||||
*/
|
||||
private String status;
|
||||
/**
|
||||
|
|
|
@ -53,4 +53,17 @@ public interface CarMessageService extends IService<CarMessage> {
|
|||
* 获取报文最终信息
|
||||
*/
|
||||
JSONObject inciseCarMessage(String testString);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue