mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Start work on pipelineconfig
This commit is contained in:
@@ -1,35 +1,65 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.util.JacksonHelper;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline2dSettings;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline3dSettings;
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
import com.chameleonvision.vision.pipeline.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
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 final Path pipelineFolderPath;
|
||||
|
||||
private final CameraConfig cameraConfig;
|
||||
|
||||
/**
|
||||
* Construct a new PipelineConfig
|
||||
* @param cameraConfig the CameraConfig (parent folder, kinda?)
|
||||
*/
|
||||
public PipelineConfig(CameraConfig cameraConfig) {
|
||||
this.cameraConfig = cameraConfig;
|
||||
pipelineFolderPath = Paths.get(cameraConfig.getConfigFolderPath().toString(), "pipelines");
|
||||
}
|
||||
|
||||
void check() {
|
||||
if (!cameraConfig.pipelinesExists()) {
|
||||
cameraConfig.checkFolder();
|
||||
// Check if there's at least one pipe
|
||||
if (!pipelinesExists()) {
|
||||
save(new CVPipeline2dSettings());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean pipelinesExists() {
|
||||
cameraConfig.checkFolder();
|
||||
return cameraConfig.getConfigFolderExists()
|
||||
&& Objects.requireNonNull(new File(pipelineFolderPath.toUri()).listFiles()).length > 0;
|
||||
}
|
||||
|
||||
private void save(CVPipelineSettings settings) {
|
||||
if (settings instanceof CVPipeline2dSettings) {
|
||||
|
||||
} else if (settings instanceof CVPipeline3dSettings) {
|
||||
|
||||
if (settings instanceof CVPipeline3dSettings) {
|
||||
Path settingJsonPath = Paths.get(pipelineFolderPath.toString(), CVPipeline3DPrefix + settings.nickname);
|
||||
try {
|
||||
JacksonHelper.serializer(settingJsonPath, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (settings instanceof CVPipeline2dSettings) {
|
||||
Path settingJsonPath = Paths.get(pipelineFolderPath.toString(), CVPipeline2DPrefix + settings.nickname);
|
||||
try {
|
||||
JacksonHelper.serializer(settingJsonPath, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("saving non-2d and non-3d pipelines not implemented~");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +70,37 @@ public class PipelineConfig {
|
||||
}
|
||||
|
||||
public List<CVPipelineSettings> load() {
|
||||
return null;
|
||||
check();
|
||||
var pipelineDir = new File(pipelineFolderPath.toUri());
|
||||
|
||||
File[] files = pipelineDir.listFiles();
|
||||
List<CVPipelineSettings> deserializedList = new ArrayList<>();
|
||||
if(files == null || files.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();
|
||||
if(name.startsWith(CVPipeline3DPrefix)) {
|
||||
// try to load 3d pipe
|
||||
try {
|
||||
var pipe = JacksonHelper.deserializer(Paths.get(file.getPath()), CVPipeline3dSettings.class);
|
||||
deserializedList.add(pipe);
|
||||
} catch (IOException e) {
|
||||
System.err.println("couldn't load cvpipeline3d");
|
||||
}
|
||||
} else if(name.startsWith(CVPipeline2DPrefix)) {
|
||||
// try to load 2d pipe
|
||||
try {
|
||||
var pipe = JacksonHelper.deserializer(Paths.get(file.getPath()), CVPipeline2dSettings.class);
|
||||
deserializedList.add(pipe);
|
||||
} catch (IOException e) {
|
||||
System.err.println("couldn't load cvpipeline2d");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deserializedList;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user