Skip to content

Commit

Permalink
adding battery message sent to GUI after rover responds from ACK (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwdv authored Feb 3, 2023
1 parent 0aa2ebf commit 49db13b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ public class Constants {

public static final String ROVER_SOCKET_NAME = "Rover";
public static final String BOARD_SOCKET_NAME = "Gameboard";
public static final String GUI_BATTERY_PCT = "battery";

}
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,9 @@ public synchronized void handleMessage(final String message, final Session sessi
this.gestureSession = session;
break;
case Constants.ROVER_ACK:
if (parsedMsg.length == 3) {
String batteryPercentage = parsedMsg[2];
String batteryVoltage = parsedMsg[1];
try {
this.percentageBatteryLeft = Integer.parseInt(batteryPercentage);
this.batteryVoltage = Float.parseFloat(batteryVoltage);
} catch (NumberFormatException nfe) {
LOGGER.log(Level.SEVERE, "Battery voltage {0} or Battery Percentage not valid: {1}",
new Object[] { batteryVoltage, batteryPercentage });
this.percentageBatteryLeft = 100;
this.batteryVoltage = 0.0f;
}
}
parseBatteryMeasurements(parsedMsg);
this.sendTextToGuiSocket(Constants.GUI_BATTERY_PCT + Constants.SOCKET_MESSAGE_DATA_DELIMITER
+ this.getBatteryPercentage());
this.roverClient.getEventManager().subscribe(GameEvent.SOCKET_DISCONNECT, this);
break;
case Constants.GAMEBOARD_ACK:
Expand Down Expand Up @@ -330,6 +320,22 @@ public synchronized void handleMessage(final String message, final Session sessi
}
}

private void parseBatteryMeasurements(final String[] parsedMsg) {
if (parsedMsg.length == 3) {
String batteryPercentage = parsedMsg[2];
String batteryVoltage = parsedMsg[1];
try {
this.percentageBatteryLeft = Integer.parseInt(batteryPercentage);
this.batteryVoltage = Float.parseFloat(batteryVoltage);
} catch (NumberFormatException nfe) {
LOGGER.log(Level.SEVERE, "Battery voltage {0} or Battery Percentage not valid: {1}",
new Object[] { batteryVoltage, batteryPercentage });
this.percentageBatteryLeft = 100;
this.batteryVoltage = 0.0f;
}
}
}

private void updateBoardAndGame(String msgID) {
this.sendBoardColour(msgID);
this.currentGame.processColour(msgID);
Expand Down

0 comments on commit 49db13b

Please sign in to comment.