Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #35 from DevOrc/master
Browse files Browse the repository at this point in the history
Add robot connection label
  • Loading branch information
ncharlton02 authored May 13, 2018
2 parents d43114a + 1007ecc commit c81d4f7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ public DriverStationBackend(ThreadChannel tc) {
public void run() {
logger.setLevel(Settings.LOG_LEVEL);
loadConfig();
generateDriverStationName();
field.setDriverStationName(name);

running = true;
sendMessageToFrontend(new InterThreadMessage("conn_status", false));
sendMessageToFrontend(new InterThreadMessage("field_conn_status", false));

connectToField();

Expand Down Expand Up @@ -81,9 +83,8 @@ private void connectToField() {
}

private void onFieldConnectionCreated() {
generateDriverStationName();
logger.info("Established Field-DS Connection! Starting main loop");
sendMessageToFrontend(new InterThreadMessage("conn_status", true));
sendMessageToFrontend(new InterThreadMessage("field_conn_status", true));
}

private void sleepCatchException(long time) {
Expand Down Expand Up @@ -139,6 +140,8 @@ private void checkConnections() {
connectToField();
}

robot.checkIfStillConnected();

if (!robot.connected())
attemptRobotConnection();
}
Expand All @@ -147,17 +150,17 @@ private void attemptRobotConnection() {
if (robot.inConnectionAttemptTimeout())
return;

if (robot.attemptConnection("localhost")) {

} else {
if (!robot.attemptConnection("localhost")) {
logger.info("Failed to connect to the robot! Waiting 3 seconds...");
}
}

private void onFieldConnectionLost() {
sendMessageToFrontend(new InterThreadMessage("conn_status", false));
sendMessageToFrontend(new InterThreadMessage("field_conn_status", false));
sendMessageToFrontend(new InterThreadMessage("robot_conn_status", false));

logger.info("Lost Connection to Field!");
robot.getRobotConnection().endConnection();
field.updateMatchStatus(ArchetypalMessages.enterNewMatchPeriod(TimePeriod.DISABLED));
}

Expand Down Expand Up @@ -201,7 +204,8 @@ private void onFrontendMessageReceived(InterThreadMessage m) {

public void shutdown() {
field.shutdown();
robot.getRobotConnection().endConnection();
if (robot.getRobotConnection() != null)
robot.getRobotConnection().endConnection();
running = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ private void onBackendMessageReceived(InterThreadMessage m) {
stage.setTitle("DS: " + newPeriod.toString());
controller.enterNewPeriod(newPeriod);
break;
case "conn_status":
controller.setConnectionStatus((boolean) m.getData());
case "field_conn_status":
controller.setFieldConnectionStatus((boolean) m.getData());
break;
case "robot_conn_status":
controller.setRobotConnectionStatus((boolean) m.getData());
break;
default:
log.warning("Unknown Message Recieved on Frontend: " + name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Field implements ConnectionListener {

private Connection fieldConnection;
private TimePeriod currentPeriod;
private String driverStationName;
private final DriverStationBackend backend;

public Field(DriverStationBackend b) {
Expand All @@ -38,6 +39,7 @@ public boolean tryToCreateConnection(String ip) {
try {
Socket socket = new Socket(ip, Resources.SERVER_PORT);
setFieldConnection(new Connection(socket, this));
fieldConnection.sendMessage(ArchetypalMessages.setName(driverStationName));
logger.info("Found connection!");
return true;
} catch (IOException e) {
Expand Down Expand Up @@ -71,10 +73,6 @@ private void updateNetworkingTable() {
}
}

public void setDriverStationName(String name) {
fieldConnection.sendMessage(ArchetypalMessages.setName(name));
}

public void updateMatchStatus(Message message) {
String timePeriod = (String) message.getData("new_period");
currentPeriod = TimePeriod.fromString(timePeriod);
Expand Down Expand Up @@ -103,4 +101,8 @@ public void setFieldConnection(Connection fieldConnection) {
public TimePeriod getCurrentPeriod() {
return currentPeriod;
}

public void setDriverStationName(String driverStationName) {
this.driverStationName = driverStationName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ public class GuiController {
public TextField statusLabel;

@FXML
public TextField connectedLabel;
public TextField fieldLabel;

@FXML
public TextField robotLabel;

public void init() {
statusLabel.setMinHeight(85);
statusLabel.setMaxHeight(85);
statusLabel.setFocusTraversable(false);
nameLabel.setFocusTraversable(false);
robotLabel.setFocusTraversable(false);

enterNewPeriod(TimePeriod.DISABLED);
setConnectionStatus(false);
setFieldConnectionStatus(false);
setRobotConnectionStatus(false);

Platform.runLater(new Runnable() {
@Override
Expand Down Expand Up @@ -64,16 +69,26 @@ public void setName(String newName) {
nameLabel.setText("Name: " + newName);
}

public void setConnectionStatus(boolean connected) {
public void setFieldConnectionStatus(boolean connected) {
if (Settings.nonFieldMode) {
connectedLabel.setText("Non Field Mode!");
connectedLabel.setStyle("-fx-background-color:blue");
fieldLabel.setText("Non Field Mode!");
fieldLabel.setStyle("-fx-background-color:blue");
} else if (connected) {
connectedLabel.setText("Field: Connected!");
connectedLabel.setStyle("-fx-background-color:green");
fieldLabel.setText("Field: Connected!");
fieldLabel.setStyle("-fx-background-color:green");
} else {
fieldLabel.setText("Field: Not Connected!");
fieldLabel.setStyle("-fx-background-color:red");
}
}

public void setRobotConnectionStatus(boolean connected) {
if (connected) {
robotLabel.setText("Robot: Connected!");
robotLabel.setStyle("-fx-background-color:green");
} else {
connectedLabel.setText("Field: Not Connected!");
connectedLabel.setStyle("-fx-background-color:red");
robotLabel.setText("Robot: Not Connected!");
robotLabel.setStyle("-fx-background-color:red");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.inspirerobotics.sumobots.driverstation.DriverStationBackend;
import org.inspirerobotics.sumobots.library.Resources;
import org.inspirerobotics.sumobots.library.concurrent.InterThreadMessage;
import org.inspirerobotics.sumobots.library.networking.connection.Connection;
import org.inspirerobotics.sumobots.library.networking.connection.ConnectionListener;
import org.inspirerobotics.sumobots.library.networking.message.Message;
Expand All @@ -19,6 +20,7 @@ public class Robot implements ConnectionListener {
private Connection robotConnection;
private final DriverStationBackend backend;
private long lastConnectionAttempt;
private boolean previouslyConnected;

public Robot(DriverStationBackend b) {
backend = b;
Expand Down Expand Up @@ -63,6 +65,13 @@ public void addStatsToNetworkTable(NetworkTable table) {
table.put("robot_name", robotConnection.getConnectionName());
}

public void checkIfStillConnected() {
if (previouslyConnected && !connected()) {
previouslyConnected = false;
backend.sendMessageToFrontend(new InterThreadMessage("robot_conn_status", false));
}
}

public boolean connected() {
if (getRobotConnection() == null) {
return false;
Expand All @@ -76,6 +85,8 @@ private void onConnectionMade(Socket s) {
setRobotConnection(new Connection(s, this));
logger.info("Found connection!");
lastConnectionAttempt = 0;
previouslyConnected = true;
backend.sendMessageToFrontend(new InterThreadMessage("robot_conn_status", true));
}

private Socket createSocket(String ip) throws IOException {
Expand Down
16 changes: 9 additions & 7 deletions sumobots/driverstation/src/main/resources/fxml/root.fxml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<fx:root minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../css/style.css" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<fx:root minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" stylesheets="@../css/style.css" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1">
<children>
<BorderPane id="root" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
Expand All @@ -15,8 +16,9 @@
<bottom>
<HBox alignment="CENTER_LEFT">
<children>
<TextField fx:id="nameLabel" alignment="CENTER" editable="false" styleClass="stdTextfield" text="Name: DS 1243" BorderPane.alignment="CENTER" HBox.hgrow="ALWAYS" />
<TextField fx:id="connectedLabel" alignment="CENTER" editable="false" layoutX="10.0" layoutY="10.0" styleClass="stdTextfield" text="Connected?" />
<TextField fx:id="robotLabel" alignment="CENTER" editable="false" styleClass="stdTextfield" text="No Robot Found" BorderPane.alignment="CENTER" HBox.hgrow="SOMETIMES" />
<TextField fx:id="nameLabel" alignment="CENTER" editable="false" styleClass="stdTextfield" text="Name" BorderPane.alignment="CENTER" HBox.hgrow="ALWAYS" />
<TextField fx:id="fieldLabel" alignment="CENTER" editable="false" layoutX="10.0" layoutY="10.0" styleClass="stdTextfield" text="No Field Found" HBox.hgrow="SOMETIMES"/>
</children>
</HBox>
</bottom>
Expand Down
16 changes: 9 additions & 7 deletions sumobots/driverstation/target/classes/fxml/root.fxml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<fx:root minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../css/style.css" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<fx:root minHeight="-Infinity" minWidth="-Infinity" prefHeight="1500.0" prefWidth="900.0" stylesheets="@../css/style.css" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1">
<children>
<BorderPane id="root" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
Expand All @@ -15,8 +16,9 @@
<bottom>
<HBox alignment="CENTER_LEFT">
<children>
<TextField fx:id="nameLabel" alignment="CENTER" editable="false" styleClass="stdTextfield" text="Name: DS 1243" BorderPane.alignment="CENTER" HBox.hgrow="ALWAYS" />
<TextField fx:id="connectedLabel" alignment="CENTER" editable="false" layoutX="10.0" layoutY="10.0" styleClass="stdTextfield" text="Connected?" />
<TextField fx:id="robotLabel" alignment="CENTER" editable="false" styleClass="stdTextfield" text="No Robot Found" BorderPane.alignment="CENTER" HBox.hgrow="SOMETIMES" />
<TextField fx:id="nameLabel" alignment="CENTER" editable="false" styleClass="stdTextfield" text="Name" BorderPane.alignment="CENTER" HBox.hgrow="ALWAYS" />
<TextField fx:id="fieldLabel" alignment="CENTER" editable="false" layoutX="10.0" layoutY="10.0" styleClass="stdTextfield" text="No Field Found" HBox.hgrow="SOMETIMES"/>
</children>
</HBox>
</bottom>
Expand Down

0 comments on commit c81d4f7

Please sign in to comment.