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

156 lines
5.3 KiB
Java
Raw Normal View History

package com.chameleonvision.config;
import com.chameleonvision.util.JacksonHelper;
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
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;
public class CameraConfig {
private static final Path camerasConfigFolderPath = Paths.get(ConfigManager.SettingsPath.toString(), "cameras");
private final String cameraConfigName;
private final CameraJsonConfig preliminaryConfig;
2019-11-27 22:23:02 -05:00
private final PipelineConfig pipelineConfig;
CameraConfig(CameraJsonConfig config) {
preliminaryConfig = config;
cameraConfigName = preliminaryConfig.name.replace(' ', '_');
2019-11-27 22:23:02 -05:00
pipelineConfig = new PipelineConfig(this);
}
2019-11-27 22:23:02 -05:00
public FullCameraConfiguration load() {
checkFolder();
checkConfig();
checkDriverMode();
2019-11-27 22:23:02 -05:00
pipelineConfig.check();
2019-11-27 22:23:02 -05:00
return new FullCameraConfiguration(loadConfig(), pipelineConfig.load(), loadDriverMode());
}
private CameraJsonConfig loadConfig() {
CameraJsonConfig config = preliminaryConfig;
try {
config = JacksonHelper.deserializer(getConfigPath(), CameraJsonConfig.class);
} catch (IOException e) {
System.err.printf("Failed to load camera config: %s - using default.\n", getConfigPath().toString());
}
return config;
}
List<CVPipelineSettings> loadPipelines() {
2019-11-27 19:51:49 -08:00
// 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() {
CVPipelineSettings driverMode = new CVPipelineSettings();
driverMode.nickname = "DRIVERMODE";
try {
driverMode = JacksonHelper.deserializer(getDriverModePath(), CVPipelineSettings.class);
} catch (IOException e) {
System.err.println("Failed to load camera drivermode: " + getDriverModePath().toString());
}
return driverMode;
}
void saveConfig(CameraJsonConfig config) {
try {
JacksonHelper.serializer(getConfigPath(), config);
} catch (IOException e) {
System.err.println("Failed to save camera config file: " + getConfigPath().toString());
}
}
void savePipelines(List<CVPipelineSettings> pipelines) {
2019-11-27 19:51:49 -08:00
// try {
// JacksonHelper.serializer(getPipelinesPath(), pipelines.toArray());
// } catch (IOException e) {
// System.err.println("Failed to save camera pipelines file: " + getConfigPath().toString());
// }
pipelineConfig.save(pipelines);
}
void saveDriverMode(CVPipelineSettings driverMode) {
try {
JacksonHelper.serializer(getDriverModePath(), driverMode);
} catch (IOException e) {
System.err.println("Failed to save camera drivermode file: " + getDriverModePath().toString());
}
}
2019-11-27 19:51:49 -08:00
void checkFolder() {
if (!getConfigFolderExists()) {
try {
2019-11-27 19:51:49 -08:00
if (!(new File(getConfigFolderPath().toUri()).mkdirs())) {
System.err.println("Failed to create camera config folder: " + getConfigFolderPath().toString());
}
} catch(Exception e) {
2019-11-27 19:51:49 -08:00
System.err.println("Failed to create camera config folder: " + getConfigFolderPath().toString());
}
}
}
private void checkConfig() {
if (!configExists()) {
try {
JacksonHelper.serializer(getConfigPath(), preliminaryConfig);
} catch (IOException e) {
System.err.println("Failed to create camera config file: " + getConfigPath().toString());
}
}
}
2019-11-27 22:23:02 -05:00
private void checkDriverMode() {
if (!driverModeExists()) {
try {
CVPipelineSettings newDriverModeSettings = new CVPipelineSettings();
newDriverModeSettings.nickname = "DRIVERMODE";
JacksonHelper.serializer(getDriverModePath(), newDriverModeSettings);
} catch (IOException e) {
System.err.println("Failed to create camera drivermode file: " + getDriverModePath().toString());
}
}
}
2019-11-27 19:51:49 -08:00
Path getConfigFolderPath() {
return Paths.get(camerasConfigFolderPath.toString(), cameraConfigName);
}
private Path getConfigPath() {
2019-11-27 19:51:49 -08:00
return Paths.get(getConfigFolderPath().toString(), "camera.json");
}
private Path getDriverModePath() {
2019-11-27 19:51:49 -08:00
return Paths.get(getConfigFolderPath().toString(), "drivermode.json");
}
2019-11-27 19:51:49 -08:00
boolean getConfigFolderExists() {
return Files.exists(getConfigFolderPath());
}
private boolean configExists() {
2019-11-27 19:51:49 -08:00
return getConfigFolderExists() && Files.exists(getConfigPath());
}
private boolean driverModeExists() {
2019-11-27 19:51:49 -08:00
return getConfigFolderExists() && Files.exists(getDriverModePath());
2019-11-27 22:23:02 -05:00
}
}