Minor IO Changes in Preparation for Public Release

This commit is contained in:
Keenan D. Buckley
2019-02-19 21:06:17 -07:00
parent c9c187601b
commit dbffa28753
+38 -33
View File
@@ -24,7 +24,7 @@ import edu.wpi.first.networktables.NetworkTableInstance;
public class App { public class App {
/// TODO: Always remember to set to false before building /// TODO: Always remember to set to false before building
static boolean isEclipse = true; static boolean isEclipse = false;
String[] robotValues; String[] robotValues;
List<NetworkTableEntry> tableEntrys = new ArrayList<NetworkTableEntry>(); List<NetworkTableEntry> tableEntrys = new ArrayList<NetworkTableEntry>();
@@ -39,50 +39,56 @@ public class App {
static boolean EXIT = false; static boolean EXIT = false;
public static void main(String[] args) { public static void main(String[] args) {
// Thanks to Frezze98 bolalo on StackOverflow for the code to create a batch file if (!isEclipse) {
// to start the program in a command window // Thanks to Frezze98 bolalo on StackOverflow for the code to create a batch file
Console console = System.console(); // to start the program in a command window
if(console == null && !GraphicsEnvironment.isHeadless()) { Console console = System.console();
String filename = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(); if(console == null && !GraphicsEnvironment.isHeadless()) {
try { String filename = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName();
File batch = new File("Launcher.bat"); try {
if(!batch.exists()){ File batch = new File("Launcher.bat");
batch.createNewFile(); if(!batch.exists()){
PrintWriter writer = new PrintWriter(batch); batch.createNewFile();
writer.println("@echo off"); PrintWriter writer = new PrintWriter(batch);
writer.println("java -jar "+ filename + " " + args[0]); writer.println("@echo off");
writer.println("exit"); writer.println("java -jar "+ filename + " " + args[0]);
writer.flush(); writer.println("exit");
} writer.flush();
Runtime.getRuntime().exec("cmd /c start \"\" "+batch.getPath()); }
} catch(IOException e) { Runtime.getRuntime().exec("cmd /c start \"\" "+batch.getPath());
e.printStackTrace(); } catch(IOException e) {
} e.printStackTrace();
} else { }
PORT_NUMBER = Integer.parseInt(args[0]); } else {
PORT_NUMBER = Integer.parseInt(args[0]);
System.out.println("Robopipe Start");
while (!EXIT) {
new App().run();
}
}
} else {
System.out.println("Robopipe Start"); System.out.println("Robopipe Start");
while (!EXIT) { while (!EXIT) {
new App().run(); new App().run();
} }
EXIT = true; }
}
} }
public void run() { public void run() {
try { try {
NetworkTableInstance inst = NetworkTableInstance.getDefault(); NetworkTableInstance inst = NetworkTableInstance.getDefault();
NetworkTable table = inst.getTable("/SmartDashboard"); NetworkTable table = inst.getTable("/SmartDashboard");
System.out.println("Connecting to Unity local server..."); System.out.println("Connecting to local server on port " + PORT_NUMBER + "...");
//serverSocket = new ServerSocket(portNumber); //serverSocket = new ServerSocket(portNumber);
clientSocket = new Socket("127.0.0.1", PORT_NUMBER); clientSocket = new Socket("127.0.0.1", PORT_NUMBER);
os = clientSocket.getOutputStream(); os = clientSocket.getOutputStream();
in = clientSocket.getInputStream(); in = clientSocket.getInputStream();
System.out.println("Connected"); System.out.println("Connected, waiting for request...");
byte[] byteRequest = new byte[100]; byte[] byteRequest = new byte[1000];
in.read(byteRequest); in.read(byteRequest);
String request = new String(byteRequest, StandardCharsets.UTF_8); String request = new String(byteRequest, StandardCharsets.UTF_8);
robotValues = request.split(","); robotValues = request.split(",");
System.out.println("Request Recieved: " + request + robotValues.length); System.out.println("Request Recieved: " + request + " Length: " + robotValues.length);
for (String value : robotValues) { for (String value : robotValues) {
tableEntrys.add(table.getEntry(value.trim())); tableEntrys.add(table.getEntry(value.trim()));
} }
@@ -90,26 +96,25 @@ public class App {
inst.startDSClient(); inst.startDSClient();
while (!EXIT) { while (!EXIT) {
toUnity(); toClient();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void toUnity() throws IOException { private void toClient() throws IOException {
output = ""; output = "";
for (NetworkTableEntry entry : tableEntrys) { for (NetworkTableEntry entry : tableEntrys) {
output += round(entry.getDouble(0), 5); output += round(entry.getDouble(0), 5);
output += "|"; output += ",";
System.out.println("'" + entry.getName() + "'"); System.out.println("'" + entry.getName() + "'");
} }
output = output.substring(0, output.length() - 1); output = output.substring(0, output.length() - 1);
byte[] byteOutput = output.getBytes(StandardCharsets.UTF_8); byte[] byteOutput = output.getBytes(StandardCharsets.UTF_8);
System.out.println("Sending '" + output + "' of length: " + byteOutput.length + " to Unity..."); System.out.println("Sending '" + output + "' of length: " + byteOutput.length + " to local port...");
os.write(byteOutput); os.write(byteOutput);
System.out.println("Waiting for Response..."); System.out.println("Waiting for Response...");
byte[] byteInput = new byte[4]; byte[] byteInput = new byte[4];
in.read(byteInput); in.read(byteInput);