simulation-backend/src/main/java/com/hivekion/room/bean/MoveRootTask.java

118 lines
3.6 KiB
Java
Raw Normal View History

2025-09-18 10:47:37 +08:00
package com.hivekion.room.bean;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hivekion.Global;
import com.hivekion.common.MultiPointGeoPosition;
import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioTask;
import java.util.Map;
import java.util.TreeMap;
2025-09-18 11:41:46 +08:00
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
2025-09-18 13:51:58 +08:00
import java.util.concurrent.TimeUnit;
2025-09-18 10:47:37 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
/**
* [类的简要说明]
* <p>
* [详细描述可选]
* <p>
*
* @author LiDongYU
* @since 2025/7/22
*/
@Slf4j
public class MoveRootTask extends AbtParentTask implements TaskAction {
2025-09-18 13:51:58 +08:00
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; //上一次纬度
2025-09-18 10:47:37 +08:00
public MoveRootTask(ScenarioTask scenarioTask, String roomId) {
super(scenarioTask, roomId);
}
@Override
public void doSomeThing() {
log.info("move task running");
2025-09-18 13:51:58 +08:00
initPath(); //初始化路径
updatePath(); //更新路径
}
/**
* 初始化路径
*/
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"
+ "&point=" + scenarioTask.getFromLat() + ","
+ scenarioTask.getFromLng()
+ "&point=" + scenarioTask.getToLat() + ","
+ 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");
//推送路径任务
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create("path_init", roomId, scenarioTask.getScenarioId(), pointsObj));
JSONArray coordinates = pointsObj.getJSONArray("coordinates");
for (int i = 0; i < coordinates.size(); i++) {
JSONArray coordinate = coordinates.getJSONArray(i);
Double lng = coordinate.getDouble(0);
Double lat = coordinate.getDouble(1);
2025-09-18 10:47:37 +08:00
double distance = MultiPointGeoPosition.haversine(beforeLat, beforeLng, lng, lat);
distanceInfoMap.put(distance, lng + "," + lat);
2025-09-18 13:51:58 +08:00
beforeLng = lng;
beforeLat = lat;
2025-09-18 10:47:37 +08:00
2025-09-18 13:51:58 +08:00
}
2025-09-18 10:47:37 +08:00
}
2025-09-18 13:51:58 +08:00
} catch (Exception e) {
log.error("error::", e);
2025-09-18 10:47:37 +08:00
}
}
2025-09-18 13:51:58 +08:00
private void updatePath() {
2025-09-18 10:47:37 +08:00
2025-09-18 13:51:58 +08:00
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
2025-09-18 14:10:33 +08:00
long duringTime = getDuringTime();
//
double distance = duringTime * SPEED;
2025-09-18 10:47:37 +08:00
2025-09-18 13:51:58 +08:00
}, 0, 1, TimeUnit.SECONDS);
2025-09-18 10:47:37 +08:00
2025-09-18 13:51:58 +08:00
//房间统一管理定时器;房间关闭后,定时器销毁
addScheduledExecutorServiceRefenceToRoom(schedule);
}
2025-09-18 10:47:37 +08:00
}