diff --git a/src/main/java/com/spacecraft/services/EventService.java b/src/main/java/com/spacecraft/services/EventService.java new file mode 100644 index 0000000..78251d8 --- /dev/null +++ b/src/main/java/com/spacecraft/services/EventService.java @@ -0,0 +1,34 @@ +package com.spacecraft.services; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.spacecraft.dtos.EventDTO; +import com.spacecraft.dtos.LongitudeDTO; +import jakarta.annotation.PostConstruct; +import lombok.Getter; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +@Service +@Getter +public class EventService { + + private List events; + + @PostConstruct + public void loadData() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + events = objectMapper.readValue( + Files.readAllBytes(Paths.get("src/main/java/com/spacecraft/persistence/events.json")), + new TypeReference>() {} + ); + for (EventDTO event : events) { + System.out.println(event); + } + } + +} diff --git a/src/main/java/com/spacecraft/services/LatitudeService.java b/src/main/java/com/spacecraft/services/LatitudeService.java new file mode 100644 index 0000000..b058052 --- /dev/null +++ b/src/main/java/com/spacecraft/services/LatitudeService.java @@ -0,0 +1,29 @@ +package com.spacecraft.services; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.spacecraft.dtos.LatitudeDTO; +import jakarta.annotation.PostConstruct; +import lombok.Getter; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +@Service +@Getter +public class LatitudeService { + + private List latitudes; + + @PostConstruct + public void loadData() throws IOException{ + ObjectMapper objectMapper = new ObjectMapper(); + latitudes = objectMapper.readValue( + Files.readAllBytes(Paths.get("src/main/java/com/spacecraft/persistence/latitudes.json")), + new TypeReference>() {} + ); + } +} diff --git a/src/main/java/com/spacecraft/services/LongitudeService.java b/src/main/java/com/spacecraft/services/LongitudeService.java new file mode 100644 index 0000000..f12f089 --- /dev/null +++ b/src/main/java/com/spacecraft/services/LongitudeService.java @@ -0,0 +1,32 @@ +package com.spacecraft.services; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.spacecraft.dtos.LongitudeDTO; +import jakarta.annotation.PostConstruct; +import lombok.Getter; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +@Service +@Getter +public class LongitudeService { + + public List longitudes; + + @PostConstruct + public void loadData() throws IOException{ + ObjectMapper objectMapper = new ObjectMapper(); + longitudes = objectMapper.readValue( + Files.readAllBytes(Paths.get("src/main/java/com/spacecraft/persistence/longitudes.json")), + new TypeReference>() {} + ); + for (LongitudeDTO longitudeDTO: longitudes){ + System.out.println(longitudeDTO); + } + } +} diff --git a/src/main/java/com/spacecraft/services/PositionService.java b/src/main/java/com/spacecraft/services/PositionService.java new file mode 100644 index 0000000..cefafc0 --- /dev/null +++ b/src/main/java/com/spacecraft/services/PositionService.java @@ -0,0 +1,45 @@ +package com.spacecraft.services; + +import com.spacecraft.dtos.LatitudeDTO; +import com.spacecraft.dtos.LongitudeDTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Comparator; + +@Service +public class PositionService { + private DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; + private final LatitudeService latitudeService; + private final LongitudeService longitudeService; + + @Autowired + public PositionService(LatitudeService latitudeService, + LongitudeService longitudeService){ + this.latitudeService = latitudeService; + this.longitudeService = longitudeService; + } + + public LatitudeDTO getClosestEventLatitude(String eventTime) { + LocalDateTime eventDateTime = LocalDateTime.parse(eventTime, formatter); + + return latitudeService.getLatitudes().stream() + .min(Comparator.comparing(lat -> Math.abs(ChronoUnit.SECONDS.between( + LocalDateTime.parse(lat.getTimestamp(), formatter), + eventDateTime)))) + .orElse(null); + } + + public LongitudeDTO getClosestEventLongitude(String eventTime){ + LocalDateTime eventDateTime = LocalDateTime.parse(eventTime, formatter); + + return longitudeService.getLongitudes().stream() + .min(Comparator.comparing(lon -> Math.abs(ChronoUnit.SECONDS.between( + LocalDateTime.parse(lon.getTimestamp(), formatter), + eventDateTime)))) + .orElse(null); + } +}