Cleaned up PipelineConfig

This commit is contained in:
Banks Troutman
2019-11-28 01:15:16 -05:00
parent febd7292cf
commit a534b6d5ce
3 changed files with 72 additions and 52 deletions

View File

@@ -8,7 +8,6 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class CameraConfig {
@@ -45,19 +44,7 @@ public class CameraConfig {
return config;
}
List<CVPipelineSettings> loadPipelines() {
// List<CVPipelineSettings> pipelines = new ArrayList<>();
// try {
// var pipelineArray = JacksonHelper.deserializer(getPipelinesPath(), CVPipelineSettings[].class);
//// pipelines = Arrays.asList(pipelineArray);
// } catch (Exception e) {
// System.err.println("Failed to load camera pipelines: " + getPipelinesPath().toString());
// }
// return pipelines;
return pipelineConfig.load();
}
CVPipelineSettings loadDriverMode() {
private CVPipelineSettings loadDriverMode() {
CVPipelineSettings driverMode = new CVPipelineSettings();
driverMode.nickname = "DRIVERMODE";
try {
@@ -77,11 +64,6 @@ public class CameraConfig {
}
void savePipelines(List<CVPipelineSettings> pipelines) {
// try {
// JacksonHelper.serializer(getPipelinesPath(), pipelines.toArray());
// } catch (IOException e) {
// System.err.println("Failed to save camera pipelines file: " + getConfigPath().toString());
// }
pipelineConfig.save(pipelines);
}
@@ -115,8 +97,6 @@ public class CameraConfig {
}
}
private void checkDriverMode() {
if (!driverModeExists()) {
try {
@@ -129,7 +109,7 @@ public class CameraConfig {
}
}
Path getConfigFolderPath() {
private Path getConfigFolderPath() {
return Paths.get(camerasConfigFolderPath.toString(), cameraConfigName);
}
@@ -141,7 +121,7 @@ public class CameraConfig {
return Paths.get(getConfigFolderPath().toString(), "drivermode.json");
}
boolean getConfigFolderExists() {
private boolean getConfigFolderExists() {
return Files.exists(getConfigFolderPath());
}

View File

@@ -9,8 +9,7 @@ public class FullCameraConfiguration {
public final List<CVPipelineSettings> pipelines;
public final CVPipelineSettings drivermode;
public FullCameraConfiguration(CameraJsonConfig cameraConfig, List<CVPipelineSettings> pipelines, CVPipelineSettings drivermode) {
FullCameraConfiguration(CameraJsonConfig cameraConfig, List<CVPipelineSettings> pipelines, CVPipelineSettings drivermode) {
this.cameraConfig = cameraConfig;
this.pipelines = pipelines;
this.drivermode = drivermode;

View File

@@ -5,16 +5,16 @@ import com.chameleonvision.vision.pipeline.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class PipelineConfig {
public static final String CVPipeline2DPrefix = "CV2D";
public static final String CVPipeline3DPrefix = "CV3D";
private static final String CVPipeline2DPrefix = "CV2D";
private static final String CVPipeline3DPrefix = "CV3D";
private final CameraConfig cameraConfig;
@@ -22,42 +22,61 @@ public class PipelineConfig {
* Construct a new PipelineConfig
* @param cameraConfig the CameraConfig (parent folder, kinda?)
*/
public PipelineConfig(CameraConfig cameraConfig) {
PipelineConfig(CameraConfig cameraConfig) {
this.cameraConfig = cameraConfig;
}
private void checkFolder() {
if ( !(new File(cameraConfig.getPipelineFolderPath().toUri()).mkdirs())) {
if (Files.notExists(cameraConfig.getPipelineFolderPath())) {
System.err.println("Failed to create pipelines folder.");
}
}
}
private File[] getPipelineFiles() {
return new File(cameraConfig.getPipelineFolderPath().toUri()).listFiles();
}
private boolean folderHasPipelines() {
File[] folderContents = getPipelineFiles();
if(folderContents == null) return false;
return folderContents.length > 0;
}
void check() {
cameraConfig.checkFolder();
checkFolder();
// Check if there's at least one pipe
if (!pipelinesExists()) {
if (!folderHasPipelines()) {
save(new CVPipeline2dSettings());
}
}
private boolean pipelinesExists() {
cameraConfig.checkFolder();
//noinspection ResultOfMethodCallIgnored
(new File(cameraConfig.getPipelineFolderPath().toUri())).mkdirs();
var folderContents = new File(cameraConfig.getPipelineFolderPath().toUri()).listFiles();
if(folderContents == null) return false;
return cameraConfig.getConfigFolderExists() && folderContents.length > 0;
private Path getPipelinePath(CVPipelineSettings setting) {
String pipelineName = setting.nickname.replace(' ', '_');
String prefix = ((setting instanceof CVPipeline2dSettings) ? CVPipeline2DPrefix : CVPipeline3DPrefix) + "-";
String fullFileName = prefix + pipelineName + ".json";
return Path.of(cameraConfig.getPipelineFolderPath().toString(), fullFileName);
}
private boolean pipelineExists(CVPipelineSettings setting) {
return Files.exists(getPipelinePath(setting));
}
private void save(CVPipelineSettings settings) {
var path = getPipelinePath(settings);
if (settings instanceof CVPipeline3dSettings) {
Path settingJsonPath = Paths.get(cameraConfig.getPipelineFolderPath().toString(),
CVPipeline3DPrefix + settings.nickname.replace(' ', '_') + ".json");
try {
JacksonHelper.serializer(settingJsonPath, settings);
JacksonHelper.serializer(path, settings);
} catch (IOException e) {
e.printStackTrace();
}
} else if (settings instanceof CVPipeline2dSettings) {
Path settingJsonPath = Paths.get(cameraConfig.getPipelineFolderPath().toString(),
CVPipeline2DPrefix + settings.nickname.replace(' ', '_') + ".json");
try {
JacksonHelper.serializer(settingJsonPath, settings);
JacksonHelper.serializer(path, settings);
} catch (IOException e) {
e.printStackTrace();
}
@@ -72,22 +91,44 @@ public class PipelineConfig {
}
}
public List<CVPipelineSettings> load() {
check();
var pipelineDir = new File(cameraConfig.getPipelineFolderPath().toUri());
public void delete(CVPipelineSettings setting) {
if(pipelineExists(setting)) {
try {
Files.delete(getPipelinePath(setting));
} catch (IOException e) {
System.err.println("Failed to delete pipeline!");
}
}
}
File[] files = pipelineDir.listFiles();
public CVPipelineSettings rename(CVPipelineSettings setting, String newName) {
if (pipelineExists(setting)) {
delete(setting);
setting.nickname = newName;
save(setting);
} else {
setting.nickname = newName;
save(setting);
}
return setting;
}
public List<CVPipelineSettings> load() {
check(); // TODO: this ensures there will be a default pipeline. is the check later necessary?
File[] pipelineFiles = getPipelineFiles();
List<CVPipelineSettings> deserializedList = new ArrayList<>();
if(files == null || files.length < 1) {
if(pipelineFiles == null || pipelineFiles.length < 1) {
// TODO handle no pipelines to load
System.err.println("no pipes to load! loading default");
} else {
for(File file : files) {
var name = file.getName();
for(File pipelineFile : pipelineFiles) {
var name = pipelineFile.getName();
if(name.startsWith(CVPipeline3DPrefix)) {
// try to load 3d pipe
try {
var pipe = JacksonHelper.deserializer(Paths.get(file.getPath()), CVPipeline3dSettings.class);
var pipe = JacksonHelper.deserializer(Paths.get(pipelineFile.getPath()), CVPipeline3dSettings.class);
deserializedList.add(pipe);
} catch (IOException e) {
System.err.println("couldn't load cvpipeline3d");
@@ -95,7 +136,7 @@ public class PipelineConfig {
} else if(name.startsWith(CVPipeline2DPrefix)) {
// try to load 2d pipe
try {
var pipe = JacksonHelper.deserializer(Paths.get(file.getPath()), CVPipeline2dSettings.class);
var pipe = JacksonHelper.deserializer(Paths.get(pipelineFile.getPath()), CVPipeline2dSettings.class);
deserializedList.add(pipe);
} catch (IOException e) {
System.err.println("couldn't load cvpipeline2d");