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";
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2019-11-27 20:20:00 -08:00
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
|
|
(new File(cameraConfig.getPipelineFolderPath().toUri())).mkdirs();
|
|
|
|
|
var folderContents = new File(cameraConfig.getPipelineFolderPath().toUri()).listFiles();
|
2019-11-27 20:06:49 -08:00
|
|
|
if(folderContents == null) return false;
|
|
|
|
|
return cameraConfig.getConfigFolderExists() && folderContents.length > 0;
|
2019-11-27 19:51:49 -08:00
|
|
|
}
|
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) {
|
2019-11-27 20:20:00 -08:00
|
|
|
Path settingJsonPath = Paths.get(cameraConfig.getPipelineFolderPath().toString(),
|
2019-11-27 20:06:49 -08:00
|
|
|
CVPipeline3DPrefix + settings.nickname.replace(' ', '_') + ".json");
|
2019-11-27 19:51:49 -08:00
|
|
|
try {
|
|
|
|
|
JacksonHelper.serializer(settingJsonPath, settings);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else if (settings instanceof CVPipeline2dSettings) {
|
2019-11-27 20:20:00 -08:00
|
|
|
Path settingJsonPath = Paths.get(cameraConfig.getPipelineFolderPath().toString(),
|
2019-11-27 20:06:49 -08:00
|
|
|
CVPipeline2DPrefix + settings.nickname.replace(' ', '_') + ".json");
|
2019-11-27 19:51:49 -08:00
|
|
|
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();
|
2019-11-27 20:20:00 -08:00
|
|
|
var pipelineDir = new File(cameraConfig.getPipelineFolderPath().toUri());
|
2019-11-27 19:51:49 -08:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|