-
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.
It is yet incomplete, as it takes the data from a file in its resources. It would ideally use data received from frontend. But still, given a gpx file, we can parse it into an Activity.
- Loading branch information
1 parent
2e4e6c5
commit cd31650
Showing
7 changed files
with
230 additions
and
6 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 |
---|---|---|
|
@@ -31,3 +31,4 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
src/main/resources/static/track.gpx |
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
38 changes: 38 additions & 0 deletions
38
src/main/java/codeurjc_students/ATRA/controller/ActivityController.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,38 @@ | ||
package codeurjc_students.ATRA.controller; | ||
|
||
import codeurjc_students.ATRA.model.Activity; | ||
import codeurjc_students.ATRA.service.ActivityService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.security.Principal; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/api/activities") | ||
public class ActivityController { | ||
|
||
@Autowired | ||
private ActivityService activityService; | ||
|
||
public Activity getActivity(){ | ||
return null; | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Activity> createActivity(Principal principal){ | ||
activityService.newActivity("target\\classes\\static\\track.gpx", principal.getName()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
public ResponseEntity<Activity> modifyActivity(){return null;} | ||
|
||
public ResponseEntity<Activity> deleteActivity(){ | ||
return null; | ||
} | ||
|
||
} | ||
|
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
48 changes: 48 additions & 0 deletions
48
src/main/java/codeurjc_students/ATRA/model/auxiliary/DataPoint.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,48 @@ | ||
package codeurjc_students.ATRA.model.auxiliary; | ||
|
||
import codeurjc_students.ATRA.service.MapToStringConverter; | ||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.time.Instant; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Parallel to a GPX's trkpt, stores values for each metric at a specific time. | ||
*/ | ||
@Data | ||
@Setter | ||
@Getter | ||
@Embeddable | ||
public class DataPoint { | ||
private Instant _time; | ||
|
||
private Double _lat; | ||
private Double _long; | ||
private Double _ele; | ||
|
||
private int hr; | ||
private int cad; | ||
|
||
@Convert(converter = MapToStringConverter.class) //this means that, when serializing, it will be converted into a String | ||
private Map<String, String> other = new HashMap<>(); | ||
|
||
public void put(String key, String value){ | ||
switch (key) { | ||
case "lat" : _lat = Double.valueOf(value); break; | ||
case "long": _long = Double.valueOf(value); break; | ||
case "ele" : _ele = Double.valueOf(value); break; | ||
|
||
case "time" : _time = Instant.parse(value); break; | ||
|
||
case "hr" : hr = Integer.parseInt(value); break; | ||
case "cad" : cad = Integer.parseInt(value); break; | ||
|
||
default: other.put(key,value); | ||
} | ||
} | ||
|
||
} |
91 changes: 85 additions & 6 deletions
91
src/main/java/codeurjc_students/ATRA/service/ActivityService.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 |
---|---|---|
@@ -1,37 +1,116 @@ | ||
package codeurjc_students.ATRA.service; | ||
|
||
import codeurjc_students.ATRA.model.Activity; | ||
import codeurjc_students.ATRA.model.User; | ||
import codeurjc_students.ATRA.model.auxiliary.DataPoint; | ||
import codeurjc_students.ATRA.repository.ActivityRepository; | ||
import io.jenetics.jpx.GPX; | ||
import io.jenetics.jpx.WayPoint; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class ActivityService { | ||
|
||
@Autowired | ||
private ActivityRepository repository; | ||
private ActivityRepository activityRepository; | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
|
||
public Optional<Activity> findById(long id) { | ||
return repository.findById(id); | ||
return activityRepository.findById(id); | ||
} | ||
|
||
public boolean exists(long id) { | ||
return repository.existsById(id); | ||
return activityRepository.existsById(id); | ||
} | ||
|
||
public List<Activity> findAll() { | ||
return repository.findAll(); | ||
return activityRepository.findAll(); | ||
} | ||
|
||
public void save(Activity activity) { | ||
repository.save(activity); | ||
activityRepository.save(activity); | ||
} | ||
|
||
public void delete(long id) { | ||
repository.deleteById(id); | ||
activityRepository.deleteById(id); | ||
} | ||
|
||
|
||
public Activity newActivity(String pathString, String username){ | ||
final GPX gpx; | ||
try { | ||
gpx = GPX.read(Paths.get(pathString)); | ||
} catch (IOException e) {throw new RuntimeException(e);} | ||
List<WayPoint> pts = gpx.getTracks().get(0).getSegments().get(0).getPoints(); | ||
|
||
|
||
Activity activity = new Activity(); | ||
//set user | ||
Optional<User> userOpt = userService.findByUserName(username); | ||
if (userOpt.isEmpty()) return null; //or throw exception caught above | ||
activity.setUser(userOpt.get().getId()); | ||
|
||
//process the metadata | ||
gpx.getMetadata().ifPresent(metadata -> activity.setStartTime(metadata.getTime().get())); | ||
activity.setName(gpx.getTracks().get(0).getName().get()); | ||
activity.setType(gpx.getTracks().get(0).getType().get()); | ||
//process the waypoints | ||
for (WayPoint pt: pts) { | ||
//add each waypoint to activity | ||
addWayPoint(activity, pt); | ||
} | ||
activityRepository.save(activity); | ||
return activity; | ||
} | ||
|
||
private void addWayPoint(Activity activity, WayPoint pt) { | ||
//processes the WayPoint and adds it to activity in ATRA format | ||
DataPoint dataPoint = new DataPoint(); | ||
//handle lat, long, ele | ||
double latitude = pt.getLatitude().doubleValue(); | ||
double longitude = pt.getLongitude().doubleValue(); | ||
double elevation = (pt.getElevation().isPresent() ? pt.getElevation().get().doubleValue() : 0.0); | ||
|
||
dataPoint.put("lat", Double.toString(latitude)); | ||
dataPoint.put("long", Double.toString(longitude)); | ||
dataPoint.put("ele", Double.toString(elevation)); | ||
|
||
//hanlde time | ||
Optional<Instant> timeOpt = pt.getTime(); | ||
timeOpt.ifPresent(instant -> dataPoint.put("time", instant.toString())); | ||
|
||
//handle extensions | ||
Optional<Document> extensions = pt.getExtensions(); | ||
if (extensions.isEmpty()) return; | ||
Element element = extensions.get().getDocumentElement(); | ||
|
||
Node currentMetric = element.getFirstChild().getChildNodes().item(0); | ||
while (currentMetric!=null) { | ||
//extract the value | ||
String metric = currentMetric.getNodeName(); | ||
String metricValue = currentMetric.getFirstChild().getNodeValue(); | ||
|
||
if (metric.startsWith("gpxtpx:")) metric = metric.substring(7); | ||
else System.out.println("Found a metric that does not start with 'gpxtcx:'"); //ideally throw an exception or sth but for now this works | ||
|
||
dataPoint.put(metric, metricValue); | ||
currentMetric = currentMetric.getNextSibling(); | ||
} | ||
|
||
activity.addDataPoint(dataPoint); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/codeurjc_students/ATRA/service/MapToStringConverter.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 codeurjc_students.ATRA.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Converter; | ||
|
||
import java.util.Map; | ||
|
||
@Converter | ||
public class MapToStringConverter implements AttributeConverter<Map<String,String>, String> { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
@Override | ||
public String convertToDatabaseColumn(Map<String, String> map) { | ||
try { | ||
return objectMapper.writeValueAsString(map); | ||
} catch (JsonProcessingException e) { | ||
System.out.println("There was an error reading some JSON"); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, String> convertToEntityAttribute(String s) { | ||
try { | ||
return objectMapper.readValue(s, new TypeReference<>() {}); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |