fix: Allow passing timestamp

This commit is contained in:
js0ny 2025-12-06 02:44:03 +00:00
parent 8e462fedc1
commit 3509c556a1
5 changed files with 40 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package io.github.js0ny.ilp_coursework.data.common;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import io.github.js0ny.ilp_coursework.data.response.DeliveryPathResponse;
@ -62,4 +63,27 @@ public record DroneEvent(
}
return events;
}
public static List<DroneEvent> fromPathResponseWithTimestamps(
DeliveryPathResponse resp, Map<Integer, LocalDateTime> deliveryTimestamps) {
List<DroneEvent> events = new java.util.ArrayList<>();
for (var p : resp.dronePaths()) {
String id = String.valueOf(p.droneId());
for (var d : p.deliveries()) {
LocalDateTime timestamp = deliveryTimestamps.get(d.deliveryId());
// Fallback to current time if the delivery does not carry a timestamp.
System.out.println("Generated event for drone " + id + " at " + timestamp.toString());
LocalDateTime current = timestamp != null ? timestamp : LocalDateTime.now();
for (var coord : d.flightPath()) {
events.add(new DroneEvent(
id,
coord.lat(),
coord.lng(),
current.toString()));
current = current.plusSeconds(1);
}
}
}
return events;
}
}

View file

@ -31,6 +31,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.time.LocalDateTime;
/**
* Class that handles calculations about deliverypath
@ -126,6 +127,7 @@ public class PathFinderService {
return new DeliveryPathResponse(0f, 0, new DronePath[0]);
}
Map<Integer, LocalDateTime> deliveryTimestamps = new HashMap<>();
for (var r : records) {
if (isRestricted(r.delivery())) {
throw new IllegalStateException(
@ -133,6 +135,9 @@ public class PathFinderService {
+ r.id()
+ " is located within a restricted area and cannot be fulfilled");
}
if (r.date() != null && r.time() != null) {
deliveryTimestamps.put(r.id(), LocalDateTime.of(r.date(), r.time()));
}
}
Map<String, List<MedDispatchRecRequest>> assigned = assignDeliveries(records);
@ -177,7 +182,7 @@ public class PathFinderService {
var resp = new DeliveryPathResponse(totalCost, totalMoves, paths.toArray(new DronePath[0]));
telemetryService.sendEventAsyncByPathResponse(resp);
telemetryService.sendEventAsyncByPathResponse(resp, deliveryTimestamps);
return resp;
}

View file

@ -4,6 +4,7 @@ import java.net.http.HttpClient;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.Map;
import org.springframework.stereotype.Service;
@ -40,6 +41,14 @@ public class TelemetryService {
}
}
public void sendEventAsyncByPathResponse(DeliveryPathResponse resp,
Map<Integer, LocalDateTime> deliveryTimestamps) {
var events = DroneEvent.fromPathResponseWithTimestamps(resp, deliveryTimestamps);
for (var event : events) {
sendEventAsync(event);
}
}
public void sendEventAsync(DroneEvent event) {
CompletableFuture.runAsync(() -> {
try {