Fix VisionSource device filtering (#60)

* Fix VisionSource filtering

* Apply spotless

* Change method ordering

* change one single character
This commit is contained in:
Banks T
2020-07-28 02:54:42 -04:00
committed by GitHub
parent 37a6d4e424
commit a6dc41f4da
2 changed files with 33 additions and 37 deletions

View File

@@ -21,77 +21,73 @@ import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.UsbCameraInfo;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.photonvision.common.configuration.CameraConfiguration;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.vision.camera.CameraType;
import org.photonvision.vision.camera.USBCameraSource;
import org.photonvision.vision.frame.provider.NetworkFrameProvider;
public class VisionSourceManager {
private static final Logger logger = new Logger(VisionSourceManager.class, LogGroup.Camera);
private static final List<String> deviceBlacklist = List.of("bcm2835-isp");
private static List<UsbCameraInfo> filterAllowedDevices(List<UsbCameraInfo> allDevices) {
List<UsbCameraInfo> filteredDevices = new ArrayList<>();
for (var device : allDevices) {
if (deviceBlacklist.contains(device.name)) {
logger.info(
"Skipping blacklisted device: \"" + device.name + "\" at \"" + device.path + "\"");
} else {
allDevices.add(device);
logger.info(
"Adding local video device - \"" + device.name + "\" at \"" + device.path + "\"");
}
}
return filteredDevices;
}
/**
* Load vision sources based on currently connected hardware.
*
* @param loadedConfigs The {@link CameraConfiguration}s loaded from disk.
*/
public static List<VisionSource> loadAllSources(Collection<CameraConfiguration> loadedConfigs) {
logger.trace(() -> "Loading all sources...");
List<UsbCameraInfo> usbCamInfos = Arrays.asList(UsbCamera.enumerateUsbCameras());
logger.trace(() -> "CSCore detected " + usbCamInfos.size() + " USB cameras!");
for (var usbCamInfo : usbCamInfos) {
logger.info(
"Adding local video device - \"" + usbCamInfo.name + "\" at \"" + usbCamInfo.path + "\"");
}
return LoadAllSources(loadedConfigs, usbCamInfos);
return loadAllSources(
loadedConfigs, filterAllowedDevices(Arrays.asList(UsbCamera.enumerateUsbCameras())));
}
/**
* Load vision sources based on currently connected hardware and preexisting configs.
* Load vision sources based on given cameras and configs.
*
* @param loadedConfigs The configs loaded from disk.
* @param detectedCamInfos The {@link UsbCameraInfo}s detected by {@link
* UsbCamera#enumerateUsbCameras()}.
* @param detectedCamInfos The cameras to attempt connection to.
*/
public static List<VisionSource> LoadAllSources(
public static List<VisionSource> loadAllSources(
Collection<CameraConfiguration> loadedConfigs, List<UsbCameraInfo> detectedCamInfos) {
var loadedUsbCamConfigs =
loadedConfigs.stream()
.filter(configuration -> configuration.cameraType == CameraType.UsbCamera)
.collect(Collectors.toList());
// var HttpCamerasConfiguration = camerasConfiguration.stream().filter(configuration ->
// configuration.cameraType == CameraType.HttpCamera);
var matchedCameras = matchUSBCameras(detectedCamInfos, loadedUsbCamConfigs);
// turn the matched cameras into VisionSources
return loadVisionSourcesFromCamConfigs(matchedCameras);
}
private static NetworkFrameProvider loadHTTPCamera(CameraConfiguration config) {
throw new NotImplementedException("");
private static List<UsbCameraInfo> filterAllowedDevices(List<UsbCameraInfo> allDevices) {
List<UsbCameraInfo> filteredDevices = new ArrayList<>();
for (var device : allDevices) {
if (deviceBlacklist.contains(device.name)) {
logger.info(
"Skipping blacklisted device - \""
+ device.name
+ "\" at \""
+ device.path
+ "\" with VID/PID: "
+ device.vendorId
+ ":"
+ device.productId);
} else {
filteredDevices.add(device);
logger.info(
"Adding local video device - \""
+ device.name
+ "\" at \""
+ device.path
+ "\" with VID/PID: "
+ device.vendorId
+ ":"
+ device.productId);
}
}
return filteredDevices;
}
/**