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;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
/**
* [类的简要说明]
*
* [详细描述,可选]
*
*
* @author LiDongYU
* @since 2025/7/22
*/
@Slf4j
public class MoveRootTask extends AbtParentTask implements TaskAction {
private final double SPEED = 170;//速度
private double accumulatedDistance = 0;//累计距离
private final Map distanceInfoMap = new TreeMap();//距离和坐标点对应关系
private Double beforeLng = null;//上一次经度
private Double beforeLat = null; //上一次纬度
public MoveRootTask(ScenarioTask scenarioTask, String roomId) {
super(scenarioTask, roomId);
}
@Override
public void doSomeThing() {
log.info("move task running");
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);
double distance = MultiPointGeoPosition.haversine(beforeLat, beforeLng, lng, lat);
distanceInfoMap.put(distance, lng + "," + lat);
beforeLng = lng;
beforeLat = lat;
}
}
} catch (Exception e) {
log.error("error::", e);
}
}
private void updatePath() {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
long duringTime = getDuringTime();
//
double distance = duringTime * SPEED;
}, 0, 1, TimeUnit.SECONDS);
//房间统一管理定时器;房间关闭后,定时器销毁
addScheduledExecutorServiceRefenceToRoom(schedule);
}
}