mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-21 01:01:41 +00:00
Cleaned up PipelineConfig
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user