-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<EventDTO> 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<List<EventDTO>>() {} | ||
); | ||
for (EventDTO event : events) { | ||
System.out.println(event); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/spacecraft/services/LatitudeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<LatitudeDTO> 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<List<LatitudeDTO>>() {} | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/spacecraft/services/LongitudeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<LongitudeDTO> 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<List<LongitudeDTO>>() {} | ||
); | ||
for (LongitudeDTO longitudeDTO: longitudes){ | ||
System.out.println(longitudeDTO); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/spacecraft/services/PositionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |