Merge remote-tracking branch 'origin/main'

This commit is contained in:
wangwenhua 2025-09-19 11:13:05 +08:00
commit 8ce2e384f4
8 changed files with 266 additions and 104 deletions

View File

@ -1,89 +1,86 @@
<!DOCTYPE html>
<html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Google Map 绘制路线</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title>WS实时推送轨迹 - Google Map</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#map {
height: 100vh;
width: 100vw;
max-width: 100%;
}
html, body { height: 100%; margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
// 你的坐标点数据
const coordinates = [
[121.148362,25.007064],
[121.147496,25.00752],
[121.147192,25.007612],
[121.146891,25.00789],
[121.147089,25.008096],
[121.148939,25.010199],
[121.149814,25.01108],
[121.150415,25.011655],
[121.150568,25.011768],
[121.151121,25.012033],
[121.151473,25.012199],
[121.153264,25.012971],
[121.154028,25.013381],
[121.155039,25.01385],
[121.155173,25.013938],
[121.155642,25.014328],
[121.156141,25.01477],
[121.156821,25.015341],
[121.156284,25.015809],
[121.156031,25.01598],
[121.15582,25.016178],
[121.15923,25.019152],
[121.158644,25.019264],
[121.157153,25.019622]
].map(([lng, lat]) => ({lat: lat, lng: lng}));
// 初始化轨迹
let path = [];
let map, marker, polyline;
function initMap() {
// 地图居中点
const center = coordinates[Math.floor(coordinates.length / 2)];
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: center,
mapTypeId: 'roadmap'
map = new google.maps.Map(document.getElementById("map"), {
zoom: 14,
center: {lat: 23.0, lng: 121.1}
});
// 绘制路线
const routePath = new google.maps.Polyline({
path: coordinates,
polyline = new google.maps.Polyline({
path: path,
geodesic: true,
strokeColor: '#FF0000',
strokeColor: "#e53935",
strokeOpacity: 0.8,
strokeWeight: 4
strokeWeight: 4,
map: map
});
routePath.setMap(map);
// 起点和终点标记
new google.maps.Marker({
position: coordinates[0],
marker = new google.maps.Marker({
map: map,
label: "起"
});
new google.maps.Marker({
position: coordinates[coordinates.length - 1],
map: map,
label: "终"
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
scaledSize: new google.maps.Size(32, 32)
}
});
}
// 连接WebSocket
function connectWS() {
// 换成你的ws地址
const ws = new WebSocket("ws://127.0.0.1:8099/ws/2746/b9164a7f-f14a-4df0-ab25-e7dae9881796");
ws.onopen = () => console.log("WebSocket已连接");
ws.onmessage = (evt) => {
// 假设后端发过来的是一个点 [lng, lat] 或一组点 [[lng,lat],...]
let data = JSON.parse(evt.data);
if(data.cmdType==='update_path'){
let points = [];
if (Array.isArray(data[0])) {
// 一组点
points = data.map(([lng, lat]) => ({lat, lng}));
} else {
// 单个点
const [lng, lat] = data;
points = [{lat, lng}];
}
// 更新轨迹
points.forEach(pt => path.push(pt));
polyline.setPath(path);
if (path.length === 1) map.setCenter(path[0]);
marker.setPosition(path[path.length - 1]);
map.panTo(path[path.length - 1]);
}
};
ws.onerror = (e) => console.log("WS错误", e);
ws.onclose = () => {
console.log("WS断开3秒后重连");
setTimeout(connectWS, 3000);
};
}
window.initMap = initMap;
window.onload = () => {
// 保证Google Map加载完后再连ws
setTimeout(connectWS, 1000);
};
</script>
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
<!-- 你的 Google Map API KEY -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDIN8ap3znRdSCwW4p3q4ZCpxy3mjBJKNQ&callback=initMap" async defer></script>
</body>
</html>

View File

@ -1,5 +1,8 @@
package com.hivekion;
import java.util.NavigableMap;
import java.util.TreeMap;
/**
* [类的简要说明]
* <p>
@ -12,13 +15,12 @@ package com.hivekion;
public class Test {
public static void main(String[] args) {
String test = "id\n"
+ "general_time\n"
+ "from_resource_id\n"
+ "supplier_type\n"
+ "supplier_num\n"
+ "lat\n"
+ "lng";
System.out.println(test.toUpperCase());
TreeMap<Double, String> treeMap = new TreeMap<Double, String>();
treeMap.put(new Double("1"), "1");
treeMap.put(new Double("5"), "1");
treeMap.put(new Double("9"), "1");
treeMap.put(new Double("20"), "1");
treeMap.put(new Double("21"), "1");
System.out.println(treeMap.ceilingKey(new Double("22"))); ;
}
}

View File

@ -40,6 +40,7 @@ public class MultiPointGeoPosition {
}
public static double[] interpolate(double lat1, double lon1, double lat2, double lon2, double f) {
lat1 = toRadians(lat1);
lon1 = toRadians(lon1);
lat2 = toRadians(lat2);
@ -118,4 +119,21 @@ public class MultiPointGeoPosition {
PositionResult pos = getPosition(points, speed, time);
System.out.printf("当前位置:纬度 %.6f, 经度 %.6f, 是否到达:%s\n", pos.latitude, pos.longitude, pos.reached ? "" : "");
}
/**
* 沿球面大圆从起点(lat1, lon1)出发沿着到终点(lat2, lon2)方向距离为d()的位置点
* 若d >= 起点到终点距离则返回终点
* @param lat1 起点纬度
* @param lon1 起点经度
* @param lat2 终点纬度
* @param lon2 终点经度
* @param d 距起点的距离
* @return double[]{纬度, 经度}
*/
public static double[] pointAlong(double lat1, double lon1, double lat2, double lon2, double d) {
double L = haversine(lat1, lon1, lat2, lon2);
if (d <= 0) return new double[] {lat1, lon1};
if (d >= L) return new double[] {lat2, lon2};
double f = d / L;
return interpolate(lat1, lon1, lat2, lon2, f);
}
}

View File

@ -0,0 +1,24 @@
package com.hivekion.enums;
import lombok.Getter;
/**
* [类的简要说明]
* <p>
* [详细描述可选]
* <p>
*
* @author LiDongYU
* @since 2025/7/22
*/
public enum WsCmdTypeEnum {
PATH_UPDATE("path_update"),
PATH_FINISHED("path_finished"),
PATH_INIT("path_init");
@Getter
private final String code;
WsCmdTypeEnum(String code) {
this.code = code;
}
}

View File

@ -7,13 +7,21 @@ import com.alibaba.fastjson2.JSONObject;
import com.hivekion.Global;
import com.hivekion.common.MultiPointGeoPosition;
import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.enums.WsCmdTypeEnum;
import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioTask;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
@ -29,12 +37,19 @@ import org.springframework.core.env.Environment;
@Slf4j
public class MoveRootTask extends AbtParentTask implements TaskAction {
/**
* 速度 换算为100Km/小时
*/
private final double SPEED = 27;
/**
* 距离和坐标的对应关系
*/
private final TreeMap<Double, Coordinate> distanceInfoMap = new TreeMap<>();
/**
* 开始点坐标
*/
private final AtomicReference<Double> startPoint = new AtomicReference<>();
private final double SPEED = 170;//速度
private double accumulatedDistance = 0;//累计距离
private final Map<Double, String> distanceInfoMap = new TreeMap<Double, String>();//距离和坐标点对应关系
private Double beforeLng = null;//上一次经度
private Double beforeLat = null; //上一次纬度
public MoveRootTask(ScenarioTask scenarioTask, String roomId) {
super(scenarioTask, roomId);
@ -53,9 +68,7 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
*/
private void initPath() {
try {
beforeLng = Double.parseDouble(scenarioTask.getFromLng());
beforeLat = Double.parseDouble(scenarioTask.getFromLat());
//累计距离
String url = SpringUtil.getBean(Environment.class).getProperty("path.planning.url");
String params = url + "?"
+ "profile=car"
@ -65,35 +78,56 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
+ scenarioTask.getToLng()
+ "&points_encoded=false"
+ "&algorithm=alternative_route&alternative_route.max_paths=3";
log.info("params::{}", params);
//获取路线信息
String result = webClient.get().uri(params)
.retrieve()
.bodyToMono(String.class)
.block();
log.info("result:{}", result);
JSONObject pointJson = JSON.parseObject(result);
//获取路径点
if (pointJson != null) {
JSONObject pointsObj = pointJson.getJSONArray("paths").getJSONObject(0)
.getJSONObject("points");
JSONArray coordinates = pointsObj.getJSONArray("coordinates");
//组装信息
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("resourceId", scenarioTask.getResourceId());
dataMap.put("points", coordinates);
//推送路径任务
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create("path_init", roomId, scenarioTask.getScenarioId(), pointsObj));
JSONArray coordinates = pointsObj.getJSONArray("coordinates");
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_INIT.getCode(), roomId,
scenarioTask.getScenarioId(), dataMap));
log.info("init::{}", JSON.toJSONString(coordinates));
//计算各个点的累计距离和坐标的对应关系
double beforeLng = Double.parseDouble(scenarioTask.getFromLng());
double beforeLat = Double.parseDouble(scenarioTask.getFromLat());
double total = 0;
for (int i = 0; i < coordinates.size(); i++) {
JSONArray coordinate = coordinates.getJSONArray(i);
Double lng = coordinate.getDouble(0);
Double lat = coordinate.getDouble(1);
double distance = MultiPointGeoPosition.haversine(beforeLat, beforeLng, lng, lat);
distanceInfoMap.put(distance, lng + "," + lat);
double distance = MultiPointGeoPosition.haversine(beforeLat, beforeLng, lat, lng);
//当前总距离
total = total + distance;
//定义坐标对象
Coordinate coordinateInfo = new Coordinate();
coordinateInfo.setLat(lat);
coordinateInfo.setLng(lng);
//记录距离和数组列表直接的索引关系
distanceInfoMap.put(total, coordinateInfo);
beforeLng = lng;
beforeLat = lat;
}
//设置第一个开始位置
startPoint.set(distanceInfoMap.firstKey());
}
} catch (Exception e) {
log.error("error::", e);
}
@ -104,14 +138,96 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
long duringTime = getDuringTime();
//
double distance = duringTime * SPEED;
try {
if (this.getRoomStatus()) {
long duringTime = getDuringTime();
log.info("duringTime::{}", duringTime);
//跑动距离
double distance = duringTime * SPEED;
//获取大与此距离的第一个路线点key
Entry<Double, Coordinate> endPoint = distanceInfoMap.ceilingEntry(distance);
//ws数据
List<double[]> dataList = new ArrayList<>();
HashMap<Object, Object> dataMap = new HashMap<>();
dataMap.put("resourceId", scenarioTask.getResourceId());
dataMap.put("points", dataList);
if (Double.compare(distance, endPoint.getKey()) < 0) {
//获取小于最大值的第一个key
Double lowerKey = distanceInfoMap.lowerKey(endPoint.getKey());
// log.info("distance::{},lowerKey::{},endPoint{}",distance,lowerKey,endPoint.getKey());
//获取从上一个开始节点到lowKey的数据
NavigableMap<Double, Coordinate> subPathMap = distanceInfoMap.subMap(startPoint.get(),
true, lowerKey, true);
for (Double key : subPathMap.keySet()) {
Coordinate coordinate = subPathMap.get(key);
dataList.add(new double[]{coordinate.getLng(), coordinate.getLat()});
}
double diff =distance - lowerKey ;
//插入值
double[] insertPoints = MultiPointGeoPosition.pointAlong(
distanceInfoMap.get(lowerKey).getLat(), distanceInfoMap.get(lowerKey).getLng(),
endPoint.getValue().getLat(), endPoint.getValue().getLng(), diff);
dataList.add(new double[]{insertPoints[1], insertPoints[0]});
Coordinate coordinate = new Coordinate();
coordinate.setLat(insertPoints[0]);
coordinate.setLng(insertPoints[1]);
distanceInfoMap.put(distance, coordinate);
startPoint.set(distance);
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId,
scenarioTask.getScenarioId(), dataMap));
} else if (Double.compare(distance, endPoint.getKey()) == 0) {
NavigableMap<Double, Coordinate> subPathMap = distanceInfoMap.subMap(startPoint.get(),
true, endPoint.getKey(), true);
for (Double key : subPathMap.keySet()) {
Coordinate coordinate = subPathMap.get(key);
dataList.add(new double[]{coordinate.getLng(), coordinate.getLat()});
}
startPoint.set(endPoint.getKey());
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId,
scenarioTask.getScenarioId(), dataMap));
} else {
//完成路径
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_FINISHED.getCode(), roomId,
scenarioTask.getScenarioId(), dataMap));
}
}
} catch (Exception e) {
log.error("error::", e);
}
}, 0, 1, TimeUnit.SECONDS);
//房间统一管理定时器房间关闭后定时器销毁
addScheduledExecutorServiceRefenceToRoom(schedule);
}
}
@Data
class Coordinate {
double lng;
double lat;
}

View File

@ -132,7 +132,7 @@ public class Room implements AutoCloseable {
if (magValue != null) {
this.mag = magValue.getMag();
}
log.info("mag:{}", mag);
long curTime = duringTime.addAndGet(this.mag);
sendRemainTime((totalTime.get() - curTime));

View File

@ -148,7 +148,7 @@ public class ScenarioTaskServiceImpl extends
try {
long diff = Duration.between(scenario.getStartTime(),task.getStartTime())
.getSeconds();
log.info("diff::{},taskType::{}",diff,task.getTaskType());
switch (task.getTaskType()) {
//移动任务
case "1":

View File

@ -66,12 +66,17 @@ public class StatisticServiceImpl implements StatisticService {
return new StatisticBean();
}
//获取人员信息
List<Fightpowerstaff> staffList = fightpowerstaffService.queryByOrgIds(orgList);
int sum = staffList.stream()
.mapToInt(Fightpowerstaff::getNumber)
.sum();
statisticBean.getPerson().setCurrent(sum);
statisticBean.getPerson().setTotal(sum);
if(!orgList.isEmpty()){
List<Fightpowerstaff> staffList = fightpowerstaffService.queryByOrgIds(orgList);
int sum = staffList.stream()
.mapToInt(Fightpowerstaff::getNumber)
.sum();
statisticBean.getPerson().setCurrent(sum);
statisticBean.getPerson().setTotal(sum);
}else{
return statisticBean;
}
//获取物资信息
List<OrgSupplier> suppliers = orgSupplierService.selectByOrgIds(orgList);