Skip to content

Commit

Permalink
simbrief auto-populate flight stats
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mckeown committed Oct 22, 2023
1 parent 73d98f7 commit ab2da5b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api/v1/flightplan")
@RequestMapping("/api/v1")
public class FlightPlanController {

private final RestTemplate restTemplate;
Expand All @@ -27,7 +27,7 @@ public FlightPlanController() {
this.xmlMapper = new XmlMapper();
}

@GetMapping("/{pilotID}")
@GetMapping("flightplan/{pilotID}")
public ResponseEntity<?> getFlightPlan(@PathVariable String pilotID) {
if (pilotID == null || pilotID.isEmpty()) {
return ResponseEntity.badRequest().body("Invalid pilot ID");
Expand All @@ -52,4 +52,27 @@ public ResponseEntity<?> getFlightPlan(@PathVariable String pilotID) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while processing your request.");
}
}

@GetMapping("/flightplan-json/{pilotID}")
public ResponseEntity<?> getFlightPlanJSON(@PathVariable String pilotID) {
if (pilotID == null || pilotID.isEmpty()) {
return ResponseEntity.badRequest().body("Invalid pilot ID");
}

try {
// Fetch JSON data directly from Simbrief using the "&json=1" parameter
String jsonData = restTemplate.getForObject("https://www.simbrief.com/api/xml.fetcher.php?userid=" + pilotID + "&json=1", String.class);

// Return the JSON data
return ResponseEntity.ok(jsonMapper.readTree(jsonData));

} catch (HttpStatusCodeException e) {
// This will handle errors returned from the Simbrief API (e.g., 404, 500, etc.)
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsString());

} catch (Exception e) {
// For other exceptions
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while processing your request.");
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<!-- Google Fonts-->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap" rel="stylesheet">
<!-- Moment.js-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<!-- Optional AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<!-- Custom files-->
Expand Down
21 changes: 20 additions & 1 deletion src/main/resources/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ app.controller('ChecklistController', ['$scope', '$sce', '$timeout', '$http', '$
$scope.icao = '';
$scope.simbriefPilotId = '';
$scope.flightPlanData = '';
$scope.flightPlanJSONData = '';
$scope.flightPlanTrustedHtml = '';
$scope.airportData = {};
$scope.airportInfo = {};
Expand Down Expand Up @@ -909,14 +910,32 @@ $scope.$watch('calculatedBoardingDateTime', function(newVal, oldVal) {
.then(function(response) {
// Handle the returned data here
$scope.flightPlanData = response.data;
console.log($scope.flightPlanData);
if ($scope.flightPlanData && $scope.flightPlanData.text) {
$scope.flightPlanTrustedHtml = $sce.trustAsHtml($scope.flightPlanData.text.plan_html);
}
})
.catch(function(error) {
console.error('Error fetching flight plan:', error);

// Display the toast with an error message
displaySimbriefErrorToast();
});
$http.get('/api/v1/flightplan-json/' + $scope.simbriefPilotId)
.then(function(response) {
// Handle the returned data here
$scope.flightPlanJSONData = response.data;
$scope.callSign = $scope.flightPlanJSONData.atc.callsign;
$scope.departureIcao = $scope.flightPlanJSONData.origin.icao_code;
$scope.arrivalIcao = $scope.flightPlanJSONData.destination.icao_code;
$scope.scheduledBoardingDateTime = moment.unix($scope.flightPlanJSONData.times.sched_out).toDate();
$scope.scheduledDepartureDateTime = moment.unix($scope.flightPlanJSONData.times.sched_off).toDate();
$scope.scheduledArrivalDateTime = moment.unix($scope.flightPlanJSONData.times.sched_on).toDate();
$scope.scheduledGateArrivalDateTime = moment.unix($scope.flightPlanJSONData.times.sched_in).toDate();

})
.catch(function(error) {
console.error('Error fetching flight plan:', error);

// Display the toast with an error message
displaySimbriefErrorToast();
});
Expand Down

0 comments on commit ab2da5b

Please sign in to comment.