Skip to content

Commit

Permalink
fix: ACARS interaction to be intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
LazoYoung committed Feb 12, 2024
1 parent 863af38 commit 0751aa6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ErrorResponse(String ident, Status status, String response) {
}

public String getResponse() {
return response;
return this.toString();
}

public void setResponse(String response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public DispatchPage(Window window) {

var simulatorBoard = new Dashboard(window, 20);
var dispatcher = new Dispatcher(window, 20);
// todo place the panel elsewhere
// var simvarMonitor = new SimvarMonitor(window);

setLeftComponent(simulatorBoard);
setRightComponent(dispatcher);
Expand Down
62 changes: 19 additions & 43 deletions src/main/java/com/flylazo/naru_acars/gui/panel/ACARS_Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,55 +142,18 @@ private void disconnectServer() {
}

private void onConnected(SocketContext context) {
this.connectBtn.setText("Disconnect");
super.setButtonListener(this.connectBtn, this::disconnectServer);

if (this.charterCheckbox.isSelected()) {
this.service.startFlight(context, ServiceType.CHARTER, this::getStartResponse, this::handleStartError);
} else {
this.startScheduledFlight(context);
}

final var service = this.charterCheckbox.isSelected() ? ServiceType.CHARTER : ServiceType.SCHEDULE;
final var props = Properties.read();
final var server = (VirtualAirline) this.serverCombo.getSelectedItem();

this.service.startFlight(service, this::getStartResponse, this::handleStartError);
this.connectBtn.setText("Disconnect");
super.setButtonListener(this.connectBtn, this::disconnectServer);
props.setAcarsAPI(this.apiInput.getText());
props.setVirtualAirline(server != null ? server.getId() : 0);
props.save();
}

private void startScheduledFlight(SocketContext context) {
this.service.fetchBooking(context, response -> {
final var plan = response.getFlightPlan();
plan.markAsBooked();

if (FlightPlan.getDispatched().isBooked()) {
this.service.startFlight(context, ServiceType.SCHEDULE, this::getStartResponse, this::handleStartError);
} else {
SwingUtilities.invokeLater(() -> {
String title = "Flightplan mismatch";
String message = "Would you like to import the flightplan?";
int option = showConfirmDialog(this.window, message, title, YES_NO_OPTION);

if (option == YES_OPTION) {
FlightPlan.submit(plan);
this.service.startFlight(context, ServiceType.SCHEDULE, this::getStartResponse, this::handleStartError);
} else {
this.service.disconnect();
}
});
}
}, this::handleBookingError);
}

private void handleBookingError(ErrorResponse response) {
if (response.getStatus() == Status.NOT_FOUND) {
this.service.disconnect();
this.window.showDialog(WARNING_MESSAGE, "Booking schedule not found!");
} else {
this.logger.log(Level.SEVERE, response.getResponse());
}
}

private void getStartResponse(Response response) {
this.window.showDialog(INFORMATION_MESSAGE, "Welcome back, captain!");
}
Expand All @@ -205,12 +168,15 @@ private void handleStartError(ErrorResponse response) {
this.window.showDialog(WARNING_MESSAGE, message);
this.service.disconnect();
} else if (status == Status.BAD_STATE) {
final var message = "Would you like to resume flight?";
final boolean isCharter = this.service.getServiceType() == ServiceType.CHARTER;
final var message = isCharter ? "Would you like to restart flight?" : "Would you like to resume flight?";
final var title = "ACARS message";
final int choice = JOptionPane.showConfirmDialog(this.window, message, title, YES_NO_OPTION);

if (choice == NO_OPTION) {
this.service.disconnect();
} else if (isCharter) {
this.restartFlight();
} else {
this.service.startTracking();
}
Expand All @@ -220,6 +186,16 @@ private void handleStartError(ErrorResponse response) {
}
}

private void restartFlight() {
final var service = this.service.getServiceType();

this.service.cancelFlight(response -> {
this.service.startFlight(service, this::getStartResponse, this::handleStartError);
}, error -> {
this.window.showDialog(ERROR_MESSAGE, error.getResponse());
});
}

private void onClose() {
this.connectBtn.setText(CONNECT);
super.setButtonListener(this.connectBtn, this::connectServer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ private void importBooking() {
this.sendActionMessage("Loading...", Color.black);

if (this.acarsService.isConnected()) {
var context = this.acarsService.getContext();
this.acarsService.fetchBooking(context, this::getBookingResponse, r -> this.handleBookingError(r.getResponse()));
this.acarsService.fetchBooking(this::getBookingResponse, r -> this.handleBookingError(r.getResponse()));
} else {
final var props = Properties.read();
final var server = VirtualAirline.getById(props.getVirtualAirline());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.annotation.Nullable;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -69,7 +70,7 @@ public SocketConnector getConnector(VirtualAirline airline) throws IllegalStateE
}

public SocketContext getContext() {
return context;
return this.context;
}

public SocketListener getListener() {
Expand All @@ -82,8 +83,8 @@ public SocketListener getListener() {
* @param callback takes {@link BookingResponse} as a callback upon successful response
* @param errorHandler takes {@link ErrorResponse} as a callback to handle any exception that may arise
*/
public void fetchBooking(SocketContext context, Consumer<BookingResponse> callback, Consumer<ErrorResponse> errorHandler) {
var message = new SocketMessage<BookingResponse>(context);
public void fetchBooking(Consumer<BookingResponse> callback, Consumer<ErrorResponse> errorHandler) {
var message = new SocketMessage<BookingResponse>(this.context);
var request = new Request()
.withIntent("fetch")
.withBulk(new FetchBulk("booking"));
Expand All @@ -97,9 +98,9 @@ public void fetchBooking(SocketContext context, Consumer<BookingResponse> callba
}
}

public void startFlight(SocketContext context, ServiceType serviceType, Consumer<Response> callback, Consumer<ErrorResponse> errorHandler) {
boolean scheduled = serviceType.equals(ServiceType.SCHEDULE);
var message = new SocketMessage<>(context);
public void startFlight(ServiceType serviceType, Consumer<Response> callback, Consumer<ErrorResponse> errorHandler) {
boolean scheduled = Objects.equals(serviceType, ServiceType.SCHEDULE);
var message = new SocketMessage<>(this.context);
var flightPlan = FlightPlan.getDispatched();
var request = new Request()
.withIntent("start")
Expand All @@ -108,7 +109,7 @@ public void startFlight(SocketContext context, ServiceType serviceType, Consumer

try {
message.whenSuccess(response -> {
startTracking();
this.startTracking();
callback.accept(response);
})
.whenError(r -> {
Expand All @@ -121,6 +122,22 @@ public void startFlight(SocketContext context, ServiceType serviceType, Consumer
}
}

public void cancelFlight(Consumer<Response> callback, Consumer<ErrorResponse> errorHandler) {
var message = new SocketMessage<>(this.context);
var request = new Request().withIntent("cancel");

try {
message.whenSuccess(response -> {
this.stopBeacon();
callback.accept(response);
})
.whenError(errorHandler)
.send(request);
} catch (JsonProcessingException e) {
this.logger.log(Level.SEVERE, "Socket error!", e);
}
}

public void startTracking() {
this.getListener().notifyEstablish();
this.startBeacon();
Expand Down

0 comments on commit 0751aa6

Please sign in to comment.