2019-09-15 15:22:52 -04:00
|
|
|
package com.chameleonvision;
|
|
|
|
|
|
2019-10-06 17:26:17 -04:00
|
|
|
import com.chameleonvision.network.NetworkManager;
|
2019-10-04 15:55:45 -04:00
|
|
|
import com.chameleonvision.settings.Platform;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
2019-10-01 02:19:32 -04:00
|
|
|
import com.chameleonvision.util.Utilities;
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.vision.camera.CameraManager;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.web.Server;
|
2019-09-26 14:19:30 -04:00
|
|
|
import edu.wpi.cscore.CameraServerCvJNI;
|
|
|
|
|
import edu.wpi.cscore.CameraServerJNI;
|
2019-10-06 17:26:17 -04:00
|
|
|
import edu.wpi.first.networktables.LogMessage;
|
2019-09-25 20:03:41 +03:00
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-26 14:19:30 -04:00
|
|
|
import java.io.IOException;
|
2019-10-06 17:26:17 -04:00
|
|
|
import java.util.function.Consumer;
|
2019-09-26 14:19:30 -04:00
|
|
|
|
2019-09-10 21:52:13 +03:00
|
|
|
public class Main {
|
2019-10-01 02:19:32 -04:00
|
|
|
|
|
|
|
|
private static final String PORT_KEY = "--port"; // expects integer
|
2019-10-01 03:00:42 -04:00
|
|
|
private static final String NT_SERVERMODE_KEY = "--nt-servermode"; // no args for this setting
|
|
|
|
|
private static final String NT_CLIENTMODESERVER_KEY = "--nt-client-server"; // expects String representing an IP address (hostnames will be rejected!)
|
2019-10-01 19:12:29 +03:00
|
|
|
private static final String NETWORK_MANAGE_KEY = "--unmanage-network"; // no args for this setting
|
2019-10-06 17:26:17 -04:00
|
|
|
private static final String IGNORE_ROOT = "--ignore-root"; // no args for this setting
|
2019-10-01 02:19:32 -04:00
|
|
|
|
|
|
|
|
private static final int DEFAULT_PORT = 8888;
|
|
|
|
|
|
|
|
|
|
private static int webserverPort = DEFAULT_PORT;
|
|
|
|
|
private static boolean ntServerMode = false;
|
2019-10-01 19:12:29 +03:00
|
|
|
private static boolean manageNetwork = true;
|
2019-10-06 17:26:17 -04:00
|
|
|
private static boolean ignoreRoot = false;
|
2019-10-01 02:19:32 -04:00
|
|
|
private static String ntClientModeServer = null;
|
|
|
|
|
|
2019-10-06 17:26:17 -04:00
|
|
|
private static class NTLogger implements Consumer<LogMessage> {
|
|
|
|
|
|
|
|
|
|
private boolean hasReportedConnectionFailure = false;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void accept(LogMessage logMessage) {
|
|
|
|
|
if (!hasReportedConnectionFailure && logMessage.message.contains("timed out")) {
|
|
|
|
|
System.err.println("NT Connection has failed!");
|
|
|
|
|
hasReportedConnectionFailure = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static final Platform CurrentPlatform = Platform.getCurrentPlatform();
|
|
|
|
|
|
2019-10-01 02:19:32 -04:00
|
|
|
private static void handleArgs(String[] args) {
|
|
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
|
var key = args[i].toLowerCase();
|
|
|
|
|
String value = null;
|
|
|
|
|
|
|
|
|
|
// this switch handles arguments with a value. Add any settings with a value here.
|
|
|
|
|
switch (key) {
|
|
|
|
|
case PORT_KEY:
|
|
|
|
|
case NT_CLIENTMODESERVER_KEY:
|
|
|
|
|
var potentialValue = args[i + 1];
|
|
|
|
|
// ensures this "value" isnt null, blank, nor another argument
|
|
|
|
|
if (potentialValue != null && !potentialValue.isBlank() && !potentialValue.startsWith("-") & !potentialValue.startsWith("--")) {
|
|
|
|
|
value = potentialValue.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
i++; // increment to skip an 'arg' next go-around of for loop, as that would be this value
|
|
|
|
|
break;
|
|
|
|
|
case NT_SERVERMODE_KEY:
|
2019-10-01 03:00:42 -04:00
|
|
|
case NETWORK_MANAGE_KEY:
|
2019-10-06 17:26:17 -04:00
|
|
|
case IGNORE_ROOT:
|
2019-10-01 02:19:32 -04:00
|
|
|
// nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this switch actually handles the arguments.
|
|
|
|
|
switch (key) {
|
|
|
|
|
case PORT_KEY:
|
2019-10-01 03:00:42 -04:00
|
|
|
System.out.println("INFO - The \"--port\" argument is currently disabled.");
|
|
|
|
|
// try {
|
|
|
|
|
// if (value == null) throw new Exception("Bad or No argument value");
|
|
|
|
|
// webserverPort = Integer.parseInt(value);
|
|
|
|
|
// } catch (Exception ex) {
|
|
|
|
|
// System.err.printf("Argument for port was invalid, starting server at port %d\n", DEFAULT_PORT);
|
|
|
|
|
// }
|
2019-10-01 02:19:32 -04:00
|
|
|
break;
|
|
|
|
|
case NT_SERVERMODE_KEY:
|
|
|
|
|
ntServerMode = true;
|
|
|
|
|
break;
|
|
|
|
|
case NT_CLIENTMODESERVER_KEY:
|
|
|
|
|
if (value != null) {
|
|
|
|
|
if (value.equals("localhost")) {
|
|
|
|
|
ntClientModeServer = "127.0.0.1";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Utilities.isValidIPV4(value)) {
|
|
|
|
|
ntClientModeServer = value;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.err.println("Argument for NT Server Host was invalid, defaulting to team number host");
|
|
|
|
|
break;
|
2019-10-01 03:00:42 -04:00
|
|
|
case NETWORK_MANAGE_KEY:
|
2019-10-01 19:12:29 +03:00
|
|
|
manageNetwork = false;
|
2019-10-01 03:00:42 -04:00
|
|
|
break;
|
2019-10-06 17:26:17 -04:00
|
|
|
case IGNORE_ROOT:
|
|
|
|
|
ignoreRoot = true;
|
2019-10-01 02:19:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2019-10-07 16:36:53 -04:00
|
|
|
if (CurrentPlatform.equals(Platform.UNSUPPORTED)) {
|
|
|
|
|
System.err.printf("Sorry, this platform is not supported. Give these details to the developers.\n%s\n", CurrentPlatform.toString());
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.printf("Starting Chameleon Vision on platform %s\n", CurrentPlatform.toString());
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 02:19:32 -04:00
|
|
|
handleArgs(args);
|
2019-10-04 15:55:45 -04:00
|
|
|
|
2019-10-06 17:26:17 -04:00
|
|
|
if (!CurrentPlatform.isRoot()) {
|
|
|
|
|
if (ignoreRoot) {
|
|
|
|
|
// TODO: should we do this?
|
|
|
|
|
// manageNetwork = false;
|
|
|
|
|
System.out.println("Ignoring root, network will not be managed!");
|
|
|
|
|
} else {
|
|
|
|
|
System.err.println("This program must be run as root!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 02:19:32 -04:00
|
|
|
// Attempt to load the JNI Libraries
|
|
|
|
|
try {
|
2019-10-06 21:49:17 -04:00
|
|
|
if (CurrentPlatform.equals(Platform.LINUX_ARM64))
|
2019-10-01 02:19:32 -04:00
|
|
|
CameraServerJNI.forceLoad();
|
|
|
|
|
CameraServerCvJNI.forceLoad();
|
|
|
|
|
} catch (IOException e) {
|
2019-10-06 17:26:17 -04:00
|
|
|
var errorStr = CurrentPlatform.equals(Platform.UNSUPPORTED) ? "Unsupported platform!" : "Failed to load JNI Libraries!";
|
2019-10-01 02:19:32 -04:00
|
|
|
throw new RuntimeException(errorStr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CameraManager.initializeCameras()) {
|
2019-10-06 17:26:17 -04:00
|
|
|
SettingsManager.initialize();
|
|
|
|
|
NetworkManager.initialize(manageNetwork);
|
2019-10-01 02:19:32 -04:00
|
|
|
CameraManager.initializeThreads();
|
|
|
|
|
|
|
|
|
|
if (ntServerMode) {
|
|
|
|
|
System.out.println("Starting NT Server");
|
|
|
|
|
NetworkTableInstance.getDefault().startServer();
|
|
|
|
|
} else {
|
2019-10-06 17:26:17 -04:00
|
|
|
NetworkTableInstance.getDefault().addLogger(new NTLogger(), 0, 255); // to hide error messages
|
2019-10-01 02:19:32 -04:00
|
|
|
if (ntClientModeServer != null) {
|
|
|
|
|
NetworkTableInstance.getDefault().startClient(ntClientModeServer);
|
|
|
|
|
} else {
|
2019-10-15 21:00:18 +03:00
|
|
|
NetworkTableInstance.getDefault().startClientTeam(SettingsManager.GeneralSettings.teamNumber);
|
2019-10-01 02:19:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.printf("Starting Webserver at port %d\n", webserverPort);
|
|
|
|
|
Server.main(webserverPort);
|
|
|
|
|
} else {
|
|
|
|
|
System.err.println("No cameras connected!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-10 21:52:13 +03:00
|
|
|
}
|