Skip to content

Commit

Permalink
Allow host name or IP address for team number (wpilibsuite#140)
Browse files Browse the repository at this point in the history
* Allow host name or IP address for team number

This makes it easier to connect to simulation, and acts the same as
shuffleboard, OutlineViewer, and the Driver Station

* Check for team number or hostname with regex
  • Loading branch information
sciencewhiz authored Sep 15, 2020
1 parent 79c5a52 commit 5145be8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
20 changes: 8 additions & 12 deletions src/main/java/edu/wpi/first/smartdashboard/SmartDashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.wpi.first.smartdashboard.extensions.FileSniffer;
import edu.wpi.first.smartdashboard.gui.DashboardFrame;
import edu.wpi.first.smartdashboard.properties.IntegerProperty;
import edu.wpi.first.smartdashboard.properties.StringProperty;
import edu.wpi.first.smartdashboard.robot.Robot;
import edu.wpi.first.wpiutil.CombinedRuntimeLoader;
import edu.wpi.first.wpiutil.WPIUtilJNI;
Expand Down Expand Up @@ -108,27 +109,22 @@ public void run() {
} else {
monitor.setProgress(600);
monitor.setNote("Getting Team Number");
IntegerProperty teamProp = frame.getPrefs().team;
int teamNumber = teamProp.getValue();
StringProperty teamProp = frame.getPrefs().team;
String teamNumber = teamProp.getValue();

teamNumberLoop:
while (teamNumber <= 0) {
try {
String input = JOptionPane.showInputDialog("Input Team Number");
while (teamNumber.equals("0")) {
String input = JOptionPane.showInputDialog("Input Team Number\\Host");
if (input == null) {
teamNumber = 0;
break teamNumberLoop;
}
teamNumber = Integer.parseInt(input);
} catch (Exception e) {
// TODO
}
teamNumber = input;
}

monitor.setProgress(650);
monitor.setNote("Connecting to robot of team: " + teamNumber);
monitor.setNote("Connecting to robot: " + teamNumber);
Robot.setHost(teamNumber);
teamProp.setValue(teamNumber);
Robot.setTeam(teamNumber);
}

try {
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/edu/wpi/first/smartdashboard/gui/DashboardPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.wpi.first.smartdashboard.properties.IntegerProperty;
import edu.wpi.first.smartdashboard.properties.Property;
import edu.wpi.first.smartdashboard.properties.PropertyHolder;
import edu.wpi.first.smartdashboard.properties.StringProperty;
import edu.wpi.first.smartdashboard.robot.Robot;
import java.io.File;
import java.util.LinkedHashMap;
Expand All @@ -28,7 +29,7 @@ public class DashboardPrefs implements PropertyHolder {
}

private Map<String, Property> properties = new LinkedHashMap<String, Property>();
public final IntegerProperty team = new IntegerProperty(this, "Team Number", 0);
public final StringProperty team = new StringProperty(this, "Team Number/Host", "0");
public final BooleanProperty hideMenu = new BooleanProperty(this, "Hide Menu", false);
public final BooleanProperty autoShowWidgets
= new BooleanProperty(this, "Automatically Show Widgets", true);
Expand Down Expand Up @@ -77,7 +78,7 @@ public Map<String, Property> getProperties() {
}

public boolean validatePropertyChange(Property property, Object value) {
if (property == team || property == width || property == height) {
if (property == width || property == height) {
return (Integer) value > 0;
} else if (property == grid_widths || property == grid_heights) {
int[] values = (int[]) value;
Expand All @@ -100,6 +101,12 @@ public boolean validatePropertyChange(Property property, Object value) {
"Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, false);
return result == JOptionPane.YES_OPTION;
}
} else if (property == team) {
return ((String)value).matches("^\\d{1,5}$") | //matches team number 5 digits or less
((String) value).matches("^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|"
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$");
//matches hostname.
//regex from https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
}
return true;
}
Expand All @@ -116,7 +123,7 @@ public void propertyChanged(Property property) {
} else if (property == height) {
frame.setSize(frame.getWidth(), height.getValue());
} else if (property == team) {
Robot.setTeam(team.getValue());
Robot.setHost(team.getValue());
} else if (property == hideMenu) {
frame.setShouldHideMenu(hideMenu.getValue());
} else if (property == logToCSV) {
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/edu/wpi/first/smartdashboard/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ public static void setTeam(int team) {
}

public static void setHost(String host) {
_host = host;
System.out.println("Host: " + host);
NetworkTable.setIPAddress(host);
if (host.matches("^\\d{1,5}$") ) { //matches team number 5 digits or less
try {
int teamNumber = Integer.parseInt(host);
setTeam(teamNumber);
} catch (NumberFormatException ex) {
//should not get here, protected by regex
ex.printStackTrace();
}
} else {
_host = host;
System.out.println("Host: " + host);
NetworkTable.setIPAddress(host);
}
}

public static String getHost() {
Expand Down

0 comments on commit 5145be8

Please sign in to comment.