diff --git a/BackSimulation/src/main/java/com/example/BackSimulation/Model/API/APIopenRouteService.java b/BackSimulation/src/main/java/com/example/BackSimulation/Model/API/APIopenRouteService.java index bb11965..5d4d5ef 100644 --- a/BackSimulation/src/main/java/com/example/BackSimulation/Model/API/APIopenRouteService.java +++ b/BackSimulation/src/main/java/com/example/BackSimulation/Model/API/APIopenRouteService.java @@ -1,7 +1,10 @@ package com.example.BackSimulation.Model.API; +import com.example.BackSimulation.Model.MapObjects.Building; +import com.example.BackSimulation.Model.MouvableObject.CoordBigDecimal; import org.json.JSONArray; import org.json.JSONObject; +import org.springframework.aop.scope.ScopedObject; import java.io.IOException; import java.math.BigDecimal; @@ -9,6 +12,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -19,15 +23,34 @@ public class APIopenRouteService { //private static final String tocken = "5b3ce3597851110001cf624898c7eb581706416a9ab7df828faeaae3"; private static final String tocken = "5b3ce3597851110001cf624898d73d0c7634461abc58ae5564b8c48d"; - private static Map >> cachedPath = new HashMap<>(); + private static Map, List >> cachedPath = new HashMap<>(); + + public static List> getPathAPI(Building startBuilding, Building endbuilding){ + List testKeyCach = new ArrayList<>(); + testKeyCach.add(startBuilding.getCoords()); + testKeyCach.add(endbuilding.getCoords()); + + System.out.println(startBuilding); + System.out.println(endbuilding); + System.out.println(cachedPath); + + if( cachedPath.containsKey(testKeyCach)){ + System.out.println("Cached call"); + return cachedPath.get(testKeyCach); + } + System.out.println("Not cached"); + + + cachedPath.put(testKeyCach, getPathAPI(startBuilding.coords.lng.floatValue(), startBuilding.coords.lat.floatValue() + , endbuilding.coords.lng.floatValue(), endbuilding.coords.lat.floatValue())); + + return cachedPath.get(testKeyCach); + } public static List> getPathAPI(float startLong, float startLat, float endLong, float endLat){ String tailURL = "&start="+ startLong + "," + startLat + "&end=" + endLong + "," + endLat; - if (cachedPath.containsKey(tailURL)){ - return cachedPath.get(tailURL); - } String URL = domainName + "?api_key=" + tocken @@ -46,17 +69,29 @@ public static List> getPathAPI(float startLong, float startLat, e.printStackTrace(); } + List> cordsList; JSONObject json = new JSONObject(response.body()); - JSONArray a = (JSONArray) json.get("features"); - JSONObject b = (JSONObject) a.get(0); - JSONObject c = (JSONObject) b.get("geometry"); - JSONArray d = (JSONArray) c.get("coordinates"); + if (json.has("features")) { + JSONArray a = (JSONArray) json.get("features"); + JSONObject b = (JSONObject) a.get(0); + JSONObject c = (JSONObject) b.get("geometry"); + JSONArray d = (JSONArray) c.get("coordinates"); - List> cordsList = (List>)(List)d.toList(); + cordsList = (List>) (List) d.toList(); - // Add path to cach : - cachedPath.put( tailURL , cordsList); + }else{ + // This part cheats a little ! + // if the API is overloaded will give some dummy coords to the agents + System.out.println("No Feature ! "); + + List coords = new ArrayList<>(); + coords.add(BigDecimal.valueOf(startLong)); + coords.add( BigDecimal.valueOf(startLat)); + + cordsList = new ArrayList<>(); + cordsList.add( coords ); + } return cordsList; } diff --git a/BackSimulation/src/main/java/com/example/BackSimulation/Model/MouvableObject/MouvableAgent.java b/BackSimulation/src/main/java/com/example/BackSimulation/Model/MouvableObject/MouvableAgent.java index df78358..8c77014 100644 --- a/BackSimulation/src/main/java/com/example/BackSimulation/Model/MouvableObject/MouvableAgent.java +++ b/BackSimulation/src/main/java/com/example/BackSimulation/Model/MouvableObject/MouvableAgent.java @@ -16,6 +16,7 @@ public class MouvableAgent extends MapObject implements Mouvable{ private List> path; private int indexPath = 0; private boolean hasArrived = false; + private Building lastPostion; private final BigDecimal deplacementMax = APIopenRouteService.deltaPoints.divide( BigDecimal.valueOf(1),10, RoundingMode.HALF_UP ) ; private final BigDecimal sightRadius= APIopenRouteService.deltaPoints.divide( BigDecimal.valueOf(0.5), 10, RoundingMode.HALF_UP) ; @@ -28,15 +29,18 @@ public MouvableAgent( int id, CoordBigDecimal coords){ // PUBLIC METHODES FROM MOUVABLE OBJECT : public void setGoal( Building building ){ - //System.out.println("API build" + building ); - //System.out.println("CONVertion : " + building.coords.lng.doubleValue() + " " + building.coords.lat.doubleValue()); - - this.path = APIopenRouteService.getPathAPI(this.coords.lng.floatValue(), this.coords.lat.floatValue() - , building.coords.lng.floatValue(), building.coords.lat.floatValue()); + if(lastPostion == null) { + System.out.println("NULL"); + this.path = APIopenRouteService.getPathAPI(this.coords.lng.floatValue(), this.coords.lat.floatValue() + , building.coords.lng.floatValue(), building.coords.lat.floatValue()); + + }else { + System.out.println("NOT NULL"); + this.path = APIopenRouteService.getPathAPI(this.lastPostion, building); + } + lastPostion = building; this.hasArrived = false; this.indexPath = 0; - - //System.out.println("Path : " + this.path); } @Override @@ -66,7 +70,6 @@ public void setMoveToGoal(){ if (this.indexPath >= this.path.size()) { this.hasArrived = true; - System.out.println("Yay, mission acomplie !"); } } @@ -101,19 +104,9 @@ public void setObstacle(MouvableAgent otherAgent){ this.dCoords.lat = dLatitude.add(dLatitude.divide(distance, 10, RoundingMode.HALF_UP).multiply(push)); }catch(ArithmeticException e ){ System.out.println("Div 0 : " + e); - } } - - /* - - BigDecimal multiply = dLongitude.divide(distance, 10, RoundingMode.HALF_UP).multiply(push); - this.dCoords.lng = dLongitude.add(multiply); - this.dCoords.lat = dLatitude.add(multiply); - */ - - } public boolean hasArrived(){