Begin network rewrite

This commit is contained in:
Banks Troutman
2019-10-04 15:55:45 -04:00
parent 2d32fa21ce
commit 7699766091
20 changed files with 338 additions and 83 deletions

View File

@@ -1,9 +1,10 @@
package com.chameleonvision;
import com.chameleonvision.settings.Platform;
import com.chameleonvision.settings.SettingsManager;
import com.chameleonvision.settings.network.NetworkManager;
import com.chameleonvision.util.Utilities;
import com.chameleonvision.vision.camera.CameraManager;
import com.chameleonvision.vision.process.VisionProcess;
import com.chameleonvision.web.Server;
import edu.wpi.cscore.CameraServerCvJNI;
import edu.wpi.cscore.CameraServerJNI;
@@ -83,12 +84,13 @@ public class Main {
public static void main(String[] args) {
handleArgs(args);
// Attempt to load the JNI Libraries
try {
CameraServerJNI.forceLoad();
CameraServerCvJNI.forceLoad();
} catch (IOException e) {
var errorStr = SettingsManager.getCurrentPlatform().equals(SettingsManager.Platform.UNSUPPORTED) ? "Unsupported platform!" : "Failed to load JNI Libraries!";
var errorStr = Platform.getCurrentPlatform().equals(Platform.UNSUPPORTED) ? "Unsupported platform!" : "Failed to load JNI Libraries!";
throw new RuntimeException(errorStr);
}

View File

@@ -1,4 +1,4 @@
package com.chameleonvision.vision;
package com.chameleonvision.settings;
public class GeneralSettings {
public int team_number = 1577;

View File

@@ -1,9 +1,12 @@
package com.chameleonvision.settings;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import com.chameleonvision.util.Utilities;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
public class NetworkSettings {
@@ -51,20 +54,26 @@ public class NetworkSettings {
}
}
public static byte[] GetTeamNumberIPBytes(int teamNumber) {
return new byte[]{(byte) (teamNumber / 100), (byte) (teamNumber % 100)};
}
public static String getAdapter() {
try {//TODO fix windows get adapter
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
Enumeration<InetAddress> ee = netint.getInetAddresses();
for (InetAddress addr : Collections.list(ee))
if (addr instanceof Inet4Address)
if ((addr.getAddress()[0] & 0xFF) == 192 && (addr.getAddress()[1] & 0xFF) == 168) {
if (addr instanceof Inet4Address) {
var addrString = addr.toString();
if ((addr.getAddress()[0] & 0xFF) == 10 && (addr.getAddress()[1] & 0xFF) == 168) {
System.out.println("found robot network interface at " + netint.getName() + " ip: " + addr.getHostAddress());
return netint.getName();
}
}
}
} catch (SocketException e) {
System.err.println("Socket exception while trying to find current ip");
System.err.println("Socket exception while trying to find current IP");
}
return "";
}

View File

@@ -0,0 +1,52 @@
package com.chameleonvision.settings;
public enum Platform {
WINDOWS_64("Windows x64"),
LINUX_64("Linux x64"),
LINUX_RASPBIAN("Linux Raspbian"),
LINUX_AARCH64("Linux ARM 64bit"),
MACOS_64("Mac OS x64"),
UNSUPPORTED("Unsupported Platform");
public final String value;
Platform(String value) {
this.value = value;
}
public boolean isWindows() {
return this == WINDOWS_64;
}
public boolean isLinux() {
return this == LINUX_64 || this == LINUX_RASPBIAN || this == LINUX_AARCH64;
}
public boolean isMac() {
return this == MACOS_64;
}
public static Platform getCurrentPlatform() {
var osName = System.getProperty("os.name");
var osArch = System.getProperty("os.arch");
if (osName.contains("Windows")) {
if (osArch.equals("amd64")) return Platform.WINDOWS_64;
return Platform.UNSUPPORTED;
}
if (osName.contains("Linux")) {
if (osArch.equals("amd64")) return Platform.LINUX_64;
if (osArch.contains("rasp")) return Platform.LINUX_RASPBIAN;
if (osArch.contains("aarch")) return Platform.LINUX_64;
return Platform.UNSUPPORTED;
}
if (osName.contains("Mac")) {
if (osArch.equals("amd64")) return Platform.MACOS_64;
return Platform.UNSUPPORTED;
}
return Platform.UNSUPPORTED;
}
}

View File

@@ -1,7 +1,7 @@
package com.chameleonvision.settings;
import com.chameleonvision.FileHelper;
import com.chameleonvision.vision.GeneralSettings;
import com.chameleonvision.settings.network.NetworkManager;
import com.chameleonvision.util.FileHelper;
import com.chameleonvision.vision.camera.CameraManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -15,20 +15,21 @@ import java.nio.file.Paths;
public class SettingsManager {
public static final Path SettingsPath = Paths.get(System.getProperty("user.dir"), "settings");
public static com.chameleonvision.vision.GeneralSettings GeneralSettings;
public static com.chameleonvision.settings.GeneralSettings GeneralSettings;
private SettingsManager() {}
public static void initialize(boolean manageNetwork) {
initGeneralSettings();
if (manageNetwork) {
NetworkSettings netSettings = new NetworkSettings();
netSettings.hostname = GeneralSettings.hostname;
netSettings.gateway = GeneralSettings.gateway;
netSettings.netmask = GeneralSettings.netmask;
netSettings.connectionType = GeneralSettings.connection_type;
netSettings.ip = GeneralSettings.ip;
netSettings.run();
NetworkManager.init();
// NetworkSettings netSettings = new NetworkSettings();
// netSettings.hostname = GeneralSettings.hostname;
// netSettings.gateway = GeneralSettings.gateway;
// netSettings.netmask = GeneralSettings.netmask;
// netSettings.connectionType = GeneralSettings.connection_type;
// netSettings.ip = GeneralSettings.ip;
// netSettings.run();
}
var allCameras = CameraManager.getAllCamerasByName();
if (!allCameras.containsKey(GeneralSettings.curr_camera) && allCameras.size() > 0) {
@@ -38,49 +39,10 @@ public class SettingsManager {
}
}
public enum Platform {
WINDOWS_64("Windows x64"),
LINUX_64("Linux x64"),
LINUX_RASPBIAN("Linux Raspbian"),
LINUX_AARCH64("Linux ARM 64bit"),
MACOS_64("Mac OS x64"),
UNSUPPORTED("Unsupported Platform");
public final String value;
Platform(String value) {
this.value = value;
}
}
public static Platform getCurrentPlatform() {
var osName = System.getProperty("os.name");
var osArch = System.getProperty("os.arch");
if (osName.contains("Windows")) {
if (osArch.equals("amd64")) return Platform.WINDOWS_64;
return Platform.UNSUPPORTED;
}
if (osName.contains("Linux")) {
if (osArch.equals("amd64")) return Platform.LINUX_64;
if (osArch.contains("rasp")) return Platform.LINUX_RASPBIAN;
if (osArch.contains("aarch")) return Platform.LINUX_64;
return Platform.UNSUPPORTED;
}
if (osName.contains("Mac")) {
if (osArch.equals("amd64")) return Platform.MACOS_64;
return Platform.UNSUPPORTED;
}
return Platform.UNSUPPORTED;
}
private static void initGeneralSettings() {
FileHelper.CheckPath(SettingsPath);
try {
GeneralSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(), "settings.json").toString()), com.chameleonvision.vision.GeneralSettings.class);
GeneralSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(), "settings.json").toString()), com.chameleonvision.settings.GeneralSettings.class);
} catch (FileNotFoundException e) {
GeneralSettings = new GeneralSettings();
}

View File

@@ -0,0 +1,14 @@
package com.chameleonvision.settings.network;
import java.net.SocketException;
import java.util.List;
public interface INetworking {
String getHostname();
NetworkIPMode getIPMode();
boolean setDHCP();
boolean setHostname(String hostname);
boolean setStatic(String ipAddress, String netmask, String gateway);
List<NetworkInterface> getNetworkInterfaces() throws SocketException;
}

View File

@@ -0,0 +1,37 @@
package com.chameleonvision.settings.network;
import java.net.SocketException;
import java.util.List;
public class LinuxNetworking implements INetworking {
@Override
public String getHostname() {
return null;
}
@Override
public NetworkIPMode getIPMode() {
return null;
}
@Override
public boolean setDHCP() {
return false;
}
@Override
public boolean setHostname(String hostname) {
return false;
}
@Override
public boolean setStatic(String ipAddress, String netmask, String gateway) {
return false;
}
@Override
public List<NetworkInterface> getNetworkInterfaces() throws SocketException {
return null;
}
}

View File

@@ -0,0 +1,7 @@
package com.chameleonvision.settings.network;
public enum NetworkIPMode {
DHCP,
STATIC,
UNKNOWN
}

View File

@@ -0,0 +1,23 @@
package com.chameleonvision.settings.network;
import com.chameleonvision.settings.NetworkSettings;
import com.chameleonvision.settings.SettingsManager;
import java.net.InetAddress;
import java.util.Collections;
public class NetworkInterface {
public final String name;
public final String displayName;
// public NetworkIPMode IPMode;
// public String IPAddress;
// public String Netmask;
// public String Gateway;
public NetworkInterface(java.net.NetworkInterface inetface) {
name = inetface.getName();
displayName = inetface.getDisplayName();
}
}

View File

@@ -0,0 +1,40 @@
package com.chameleonvision.settings.network;
import com.chameleonvision.settings.Platform;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class NetworkManager {
private NetworkManager() {}
private static INetworking networking;
public static void init() {
Platform platform = Platform.getCurrentPlatform();
if (platform.isLinux()) {
networking = new LinuxNetworking();
} else if (platform.isWindows()) {
networking = new WindowsNetworking();
}
List<NetworkInterface> interfaces = new ArrayList<>();
try {
interfaces = networking.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
if (interfaces != null) {
for (var inetface : interfaces) {
if (inetface.displayName.toLowerCase().contains("asus")) {
// networking.setHostname("BIGRIG");
}
}
}
}
}

View File

@@ -0,0 +1,83 @@
package com.chameleonvision.settings.network;
import com.chameleonvision.settings.NetworkSettings;
import com.chameleonvision.settings.SettingsManager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class WindowsNetworking implements INetworking {
@Override
public String getHostname() {
try {
InetAddress localhost = InetAddress.getLocalHost();
return localhost.getHostName().split("/")[0];
} catch (UnknownHostException e) {
return null;
}
}
@Override
public NetworkIPMode getIPMode() {
return NetworkIPMode.UNKNOWN;
}
@Override
public boolean setDHCP() {
return false;
}
@Override
public boolean setHostname(String newHostname) {
var currentHostname = getHostname();
if (getHostname() == null) {
return false;
}
String command = String.format("wmic computersystem where name=\"%s\" call rename name=\"%s\"", currentHostname, newHostname);
try {
var process = Runtime.getRuntime().exec(command);
var returnCode = process.waitFor();
return returnCode == 0;
} catch(Exception e) {
return false;
}
}
@Override
public boolean setStatic(String ipAddress, String netmask, String gateway) {
return false;
}
@Override
public List<NetworkInterface> getNetworkInterfaces() throws SocketException {
var netInterfaces = Collections.list(java.net.NetworkInterface.getNetworkInterfaces());
List<NetworkInterface> goodInterfaces = new ArrayList<>();
var teamBytes = NetworkSettings.GetTeamNumberIPBytes(SettingsManager.GeneralSettings.team_number);
for (var inetface : netInterfaces) {
if (inetface.getDisplayName().toLowerCase().contains("bluetooth")) continue;
if (inetface.getDisplayName().toLowerCase().contains("virtual")) continue;
if (inetface.getDisplayName().toLowerCase().contains("loopback")) continue;
if (!inetface.isUp()) continue;
for (var inetAddr : Collections.list(inetface.getInetAddresses())) {
var rawAddr = inetAddr.getAddress();
if (rawAddr[1] == teamBytes[0] && rawAddr[2] == teamBytes[1]) {
goodInterfaces.add(new NetworkInterface(inetface));
}
}
}
return goodInterfaces;
}
}

View File

@@ -1,4 +1,4 @@
package com.chameleonvision;
package com.chameleonvision.util;
import java.io.IOException;
import java.nio.file.Files;

View File

@@ -1,4 +1,4 @@
package com.chameleonvision;
package com.chameleonvision.util;
public class MemoryManager {

View File

@@ -1,5 +1,8 @@
package com.chameleonvision.util;
import java.util.ArrayList;
import java.util.List;
public class Utilities {
private Utilities() {}
@@ -8,4 +11,30 @@ public class Utilities {
return ip.matches(PATTERN);
}
public static List<Byte> getDigitBytes(int num) {
List<Byte> digits = new ArrayList<>();
collectDigitBytes(num, digits);
return digits;
}
private static void collectDigitBytes(int num, List<Byte> digits) {
if (num / 10 > 0) {
collectDigitBytes( num / 10, digits);
}
digits.add((byte) (num % 10));
}
public static List<Integer> getDigits(int num) {
List<Integer> digits = new ArrayList<Integer>();
collectDigits(num, digits);
return digits;
}
private static void collectDigits(int num, List<Integer> digits) {
if(num / 10 > 0) {
collectDigits(num / 10, digits);
}
digits.add(num % 10);
}
}

View File

@@ -1,13 +1,11 @@
package com.chameleonvision.vision.camera;
import com.chameleonvision.CameraException;
import com.chameleonvision.settings.SettingsManager;
import com.chameleonvision.settings.Platform;
import com.chameleonvision.vision.Pipeline;
import com.chameleonvision.web.ServerHandler;
import edu.wpi.cscore.*;
import edu.wpi.first.cameraserver.CameraServer;
import org.opencv.core.Mat;
import org.springframework.core.env.Environment;
import java.util.Arrays;
import java.util.HashMap;
@@ -67,12 +65,11 @@ public class Camera {
this.pipelines = pipelines;
// set up video modes according to minimums
if (SettingsManager.getCurrentPlatform() == SettingsManager.Platform.WINDOWS_64 && !UsbCam.isConnected()) {
if (Platform.getCurrentPlatform() == Platform.WINDOWS_64 && !UsbCam.isConnected()) {
System.out.print("Waiting on camera... ");
long initTimeout = System.nanoTime();
while(!UsbCam.isConnected())
{
//TODO add a time sleep, can wait only so long before giving up
if (((System.nanoTime() - initTimeout) / 1e6 ) >= MAX_INIT_MS) {
break;
}

View File

@@ -1,4 +1,4 @@
package com.chameleonvision;
package com.chameleonvision.vision.camera;
public class CameraException extends Exception {
public enum CameraExceptionType {
@@ -19,7 +19,7 @@ public class CameraException extends Exception {
}
}
public CameraException(CameraExceptionType camExceptionType) {
CameraException(CameraExceptionType camExceptionType) {
super(camExceptionType.toString());
}
}

View File

@@ -1,9 +1,7 @@
package com.chameleonvision.vision.camera;
import com.chameleonvision.CameraException;
import com.chameleonvision.FileHelper;
import com.chameleonvision.util.FileHelper;
import com.chameleonvision.settings.SettingsManager;
import com.chameleonvision.vision.GeneralSettings;
import com.chameleonvision.vision.Pipeline;
import com.chameleonvision.vision.process.VisionProcess;
import com.google.gson.Gson;
@@ -15,7 +13,6 @@ import org.opencv.videoio.VideoCapture;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

View File

@@ -110,14 +110,20 @@ public class VisionProcess implements Runnable {
}
private void updateNetworkTables(PipelineResult pipelineResult) {
ntValidEntry.setBoolean(pipelineResult.IsValid);
if (pipelineResult.IsValid) {
ntValidEntry.setBoolean(true);
ntYawEntry.setNumber(pipelineResult.Yaw);
ntPitchEntry.setNumber(pipelineResult.Pitch);
ntDistanceEntry.setNumber(pipelineResult.Area);
ntTimeStampEntry.setNumber(TimeStamp);
NetworkTableInstance.getDefault().flush();
} else {
ntYawEntry.setNumber(0.0);
ntPitchEntry.setNumber(0.0);
ntDistanceEntry.setNumber(0.0);
ntTimeStampEntry.setNumber(TimeStamp);
ntValidEntry.setBoolean(false);
}
ntTimeStampEntry.setNumber(TimeStamp);
}
private PipelineResult runVisionProcess(Mat inputImage, Mat outputImage) {

View File

@@ -1,11 +1,8 @@
package com.chameleonvision.web;
import com.chameleonvision.CameraException;
import com.chameleonvision.vision.camera.CameraException;
import com.chameleonvision.settings.SettingsManager;
import com.chameleonvision.vision.Pipeline;
import com.chameleonvision.vision.camera.Camera;
import com.chameleonvision.vision.camera.CameraManager;
import com.google.gson.JsonArray;
import edu.wpi.cscore.VideoException;
import io.javalin.websocket.WsCloseContext;
import io.javalin.websocket.WsConnectContext;