Files
PhotonVision/Main/src/main/java/com/chameleonvision/config/PipelineConfig.java

107 lines
3.7 KiB
Java
Raw Normal View History

2019-11-27 22:23:02 -05:00
package com.chameleonvision.config;
import com.chameleonvision.util.JacksonHelper;
2019-11-27 19:51:49 -08:00
import com.chameleonvision.vision.pipeline.*;
2019-11-27 22:23:02 -05:00
2019-11-27 19:51:49 -08:00
import java.io.File;
2019-11-27 22:23:02 -05:00
import java.io.IOException;
2019-11-27 19:51:49 -08:00
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
2019-11-27 22:23:02 -05:00
import java.util.List;
2019-11-27 19:51:49 -08:00
import java.util.Objects;
2019-11-27 22:23:02 -05:00
public class PipelineConfig {
public static final String CVPipeline2DPrefix = "CV2D";
public static final String CVPipeline3DPrefix = "CV3D";
2019-11-27 19:51:49 -08:00
private final Path pipelineFolderPath;
2019-11-27 22:23:02 -05:00
private final CameraConfig cameraConfig;
2019-11-27 19:51:49 -08:00
/**
* Construct a new PipelineConfig
* @param cameraConfig the CameraConfig (parent folder, kinda?)
*/
2019-11-27 22:23:02 -05:00
public PipelineConfig(CameraConfig cameraConfig) {
this.cameraConfig = cameraConfig;
2019-11-27 19:51:49 -08:00
pipelineFolderPath = Paths.get(cameraConfig.getConfigFolderPath().toString(), "pipelines");
2019-11-27 22:23:02 -05:00
}
void check() {
2019-11-27 19:51:49 -08:00
cameraConfig.checkFolder();
// Check if there's at least one pipe
if (!pipelinesExists()) {
2019-11-27 22:23:02 -05:00
save(new CVPipeline2dSettings());
}
}
2019-11-27 19:51:49 -08:00
private boolean pipelinesExists() {
cameraConfig.checkFolder();
return cameraConfig.getConfigFolderExists()
&& Objects.requireNonNull(new File(pipelineFolderPath.toUri()).listFiles()).length > 0;
}
2019-11-27 22:23:02 -05:00
2019-11-27 19:51:49 -08:00
private void save(CVPipelineSettings settings) {
2019-11-27 22:23:02 -05:00
2019-11-27 19:51:49 -08:00
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~");
2019-11-27 22:23:02 -05:00
}
}
public void save(List<CVPipelineSettings> settings) {
for(CVPipelineSettings setting : settings) {
save(setting);
}
}
public List<CVPipelineSettings> load() {
2019-11-27 19:51:49 -08:00
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;
2019-11-27 22:23:02 -05:00
}
}