wip routing service
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2025-12-30 18:41:30 +01:00
parent 6bd78c597f
commit 6eb36f5b58
2 changed files with 107 additions and 38 deletions
@@ -1,10 +1,15 @@
package SuMoC.backend.carta;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import sumoc.backend.carta.client.api.CartaApi;
import sumoc.backend.carta.client.api.MapApi;
import sumoc.backend.carta.client.model.Direction;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.CompletableFuture;
@Service
public class CartaClient {
@@ -25,46 +30,45 @@ public class CartaClient {
* @param alternatives Number of alternatives
* @param walkingSpeed
* @param transferTime
* @param accessible
* @param parking
* @param type 0=tram, 1=subway, 2=rail, 3=bus, 4=ferry, 5=cablecar, 6=gondola, 7=funicular, 8=trolleybus, 9=monorail
* @param type 0=tram, 1=subway, 2=rail, 3=bus, 4=ferry, 5=cablecar, 6=gondola, 7=funicular, 8=trolleybus, 9=monorail - after inspecting innamoruhr 10 = add bikes?
* @param range
* @param location
* @param location Context for name resolution
* @return
*/
public Direction getRoute(
@Async
public CompletableFuture<Direction> asyncGetRoute(
String origin,
String destination,
String mode,
Long arrivalTime,
Long departureTime,
LocalDateTime arrivalTime,
LocalDateTime departureTime,
Integer alternatives,
Double walkingSpeed,
Integer transferTime,
Boolean accessible,
Boolean parking,
String type,
Integer range,
String location
) {
try {
String uid = null; // Optional user identifier
return cartaApi.getDirection(
return CompletableFuture.completedFuture(cartaApi.getDirection(
origin,
destination,
mode, // possible values: walking, driving, cycling
arrivalTime,
departureTime,
mode.toLowerCase(), // possible values: walking, driving, cycling
arrivalTime.toEpochSecond(ZoneOffset.UTC),
departureTime.toEpochSecond(ZoneOffset.UTC),
alternatives,
walkingSpeed,
transferTime,
accessible,
parking,
false,
false,
type,
range,
location,
uid
);
null
));
// } catch (InterruptedException e) {
// return CompletableFuture.failedFuture(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -3,11 +3,16 @@ package SuMoC.backend.routing;
import SuMoC.backend.carta.CartaClient;
import SuMoC.backend.routing.dtos.RouteDTO;
import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO;
import SuMoC.backend.trip.Modality;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sumoc.backend.carta.client.model.Direction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@Service
public class RoutingService {
@@ -19,29 +24,89 @@ public class RoutingService {
this.cartaClient = cartaClient;
}
// // TODO make async for parallel weather calls
// private Direction getDirectionFromCarta(RoutingAlternativeRequestDTO requestDTO) {
// return cartaClient.getRoute(
// requestDTO.getOrigin(),
// requestDTO.getDestination(),
// "walking", // mode
// null, // arrivalTime
// null, // departureTime
// 1, // alternatives
// null, // walkingSpeed
// null, // transferTime
// null, // accessible
// null, // parking
// null, // type
// null, // range
// null // location
// );
// }
/**
* Makes an individual call to the CARTA API and returns a direction future.
*/
private CompletableFuture<Direction> getCartaFuture(
RoutingAlternativeRequestDTO requestDTO,
String mode,
String types,
int alternatives
) {
return cartaClient.asyncGetRoute(
requestDTO.getOriginLat() + "," + requestDTO.getOriginLon(),
requestDTO.getDestinationLat() + "," + requestDTO.getDestinationLon(),
mode,
requestDTO.getArrivalTime(),
requestDTO.getDepartureTime(),
alternatives, // alternatives
null, // walkingSpeed
null, // transferTime TODO what is default? adapt?
types,
null, // range
requestDTO.getOriginLat() + "," + requestDTO.getOriginLon()
);
}
/**
* Makes concurrent direction calls to CARTA for all modes requested + car as baseline.
*/
private HashMap<String, Direction> getPossibleDirectionsFromCarta(RoutingAlternativeRequestDTO requestDTO) {
Map<String, CompletableFuture<Direction>> futuresByMode = new HashMap<>();
assert requestDTO.getRoutingPreferencesDTO() != null;
// serves as baseline
futuresByMode.put("car", getCartaFuture(requestDTO, "driving", "", 1));
// type string for carta api
List<String> typeList = new ArrayList<>();
// add request futures for all modes request by user + assemble type string for a possible transit directions request
for (Modality modality : requestDTO.getRoutingPreferencesDTO().getModalitiesToConsider()) {
switch (modality) {
case WALK -> futuresByMode.put("walk", getCartaFuture(requestDTO, "walking", "", 1));
case BIKE -> futuresByMode.put("bike", getCartaFuture(requestDTO, "cycling", "", 1));
case BUS -> {
typeList.add("3"); // bus
typeList.add("8"); // trolleybus
}
// case ESCOOTER -> typeList.add("X"); TODO - check if this and bike should be included in transit or separate
case TRAM -> typeList.add("0");
case TRAIN -> {
typeList.add("1"); // subway
typeList.add("2"); // rail
typeList.add("9"); // monorail
}
}
}
if (!typeList.isEmpty()) { // if any public transit the app allows toggling is selcted we add the funny transit types and make the transit request
typeList.add("4"); // Ferry
typeList.add("5"); // cablecar
typeList.add("6"); // gondola
typeList.add("7"); // funicular
String types = String.join(",", typeList);
futuresByMode.put("transit", getCartaFuture(requestDTO, "transit", types, 3));
}
// return all future results as list when resvoled
HashMap<String, Direction> directions = new HashMap<>();
for (Map.Entry<String, CompletableFuture<Direction>> entry : futuresByMode.entrySet()) {
directions.put(entry.getKey(), entry.getValue().join());
}
return directions;
}
public List<RouteDTO> getAlternativeRoute(RoutingAlternativeRequestDTO requestDTO) {
// TODO make parallel requests for different modes + compare with carta web how they request mode combined travel
HashMap<String, Direction> directionHashMap = getPossibleDirectionsFromCarta(requestDTO);
// if consider weather
// get weather for origin
// filter applicable routes (car is only baseline, never suggested)
// certain threshold how much more delay is okay
// apply physical preference if set
// convert remaining routes to routedto
// var dto = getDirectionFromCarta(requestDTO);
return new ArrayList<RouteDTO>();
}
}