2019-11-23 11:55:20 -05:00
|
|
|
package com.chameleonvision.config;
|
2019-11-19 12:43:38 -05:00
|
|
|
|
2019-11-23 11:55:20 -05:00
|
|
|
import com.chameleonvision.util.ProgramDirectoryUtilities;
|
2019-11-19 12:43:38 -05:00
|
|
|
import com.chameleonvision.util.FileHelper;
|
2019-11-23 19:39:24 +02:00
|
|
|
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
2019-11-19 12:06:37 -08:00
|
|
|
|
2019-11-19 12:43:38 -05:00
|
|
|
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 ConfigManager {
|
|
|
|
|
private ConfigManager() {}
|
|
|
|
|
|
|
|
|
|
private static final Path SettingsPath = Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "settings");
|
2019-11-21 05:32:19 -05:00
|
|
|
private static final Path cameraConfigPath = Paths.get(SettingsPath.toString(), "cameras");
|
2019-11-23 12:23:21 -05:00
|
|
|
private static final Path settingsFilePath = Paths.get(SettingsPath.toString(), "settings.json");
|
2019-11-19 12:43:38 -05:00
|
|
|
|
|
|
|
|
public static GeneralSettings settings = new GeneralSettings();
|
|
|
|
|
|
2019-11-23 12:23:21 -05:00
|
|
|
private static boolean settingsFolderExists() { return Files.exists(SettingsPath); }
|
|
|
|
|
private static boolean settingsFileExists() { return settingsFolderExists() && Files.exists(settingsFilePath); }
|
|
|
|
|
|
|
|
|
|
private static void checkSettingsFolder() {
|
|
|
|
|
if (!settingsFolderExists()) {
|
2019-11-19 12:43:38 -05:00
|
|
|
try {
|
|
|
|
|
Files.createDirectory(SettingsPath);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-23 12:23:21 -05:00
|
|
|
}
|
2019-11-19 12:43:38 -05:00
|
|
|
|
2019-11-23 12:23:21 -05:00
|
|
|
private static void checkSettingsFile() {
|
|
|
|
|
if (!settingsFileExists()) {
|
2019-11-19 12:43:38 -05:00
|
|
|
try {
|
2019-11-21 05:32:19 -05:00
|
|
|
FileHelper.Serializer(settingsFilePath, settings);
|
2019-11-19 12:43:38 -05:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
settings = FileHelper.DeSerializer(settingsFilePath, GeneralSettings.class);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Failed to load settings.json, using defaults.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 12:23:21 -05:00
|
|
|
public static void initializeSettings() {
|
|
|
|
|
checkSettingsFolder();
|
|
|
|
|
checkSettingsFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void saveSettingsFile() {
|
|
|
|
|
try {
|
|
|
|
|
FileHelper.Serializer(settingsFilePath, settings);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Failed to save settings.json!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveSettings() {
|
|
|
|
|
checkSettingsFolder();
|
|
|
|
|
saveSettingsFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Path getCameraSpecificFolderPath(String cameraName) {
|
|
|
|
|
return Paths.get(cameraConfigPath.toString(), cameraName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Path getCameraSpecificConfigPath(String cameraName) {
|
|
|
|
|
return Paths.get(getCameraSpecificFolderPath(cameraName).toString(), "camera.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Path getCameraSpecificPipelinesPath(String cameraName) {
|
|
|
|
|
return Paths.get(getCameraSpecificFolderPath(cameraName).toString(), "pipelines.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Path getCameraSpecificDriverModePath(String cameraName) {
|
|
|
|
|
return Paths.get(getCameraSpecificFolderPath(cameraName).toString(), "drivermode.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean cameraFolderExists(String cameraName) {
|
|
|
|
|
return Files.exists(getCameraSpecificFolderPath(cameraName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean cameraConfigExists(String cameraName) {
|
|
|
|
|
return cameraFolderExists(cameraName) && Files.exists(getCameraSpecificConfigPath(cameraName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean cameraPipelinesExists(String cameraName) {
|
|
|
|
|
return cameraFolderExists(cameraName) && Files.exists(getCameraSpecificPipelinesPath(cameraName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean cameraDriverModeExists(String cameraName) {
|
|
|
|
|
return cameraFolderExists(cameraName) && Files.exists(getCameraSpecificDriverModePath(cameraName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: (HIGH) cleanup!
|
2019-11-21 05:32:19 -05:00
|
|
|
public static List<CameraConfig> initializeCameraConfig(List<CameraConfig> preliminaryConfigs) {
|
2019-11-19 12:06:37 -08:00
|
|
|
var configList = new ArrayList<CameraConfig>();
|
|
|
|
|
|
|
|
|
|
// loop over all the camera names and try to create settings folders for it
|
2019-11-21 05:32:19 -05:00
|
|
|
preliminaryConfigs.forEach((preliminaryConfig) -> {
|
2019-11-19 12:43:38 -05:00
|
|
|
|
2019-11-21 05:32:19 -05:00
|
|
|
final Path cameraConfigFolderPath = Paths.get(cameraConfigPath.toString(), String.format("%s\\", preliminaryConfig.name));
|
|
|
|
|
final Path cameraConfigPath = Paths.get(cameraConfigFolderPath.toString(), "camera.json");
|
2019-11-19 12:43:38 -05:00
|
|
|
|
2019-11-21 05:32:19 -05:00
|
|
|
// check if the config folder exists, and if not, create it
|
|
|
|
|
if (Files.notExists(cameraConfigFolderPath)) {
|
2019-11-19 12:06:37 -08:00
|
|
|
try {
|
2019-11-21 05:32:19 -05:00
|
|
|
Files.createDirectory(cameraConfigFolderPath);
|
2019-11-19 21:49:56 -08:00
|
|
|
} catch (IOException e) {
|
2019-11-19 12:06:37 -08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-11-21 05:32:19 -05:00
|
|
|
CameraConfig config = preliminaryConfig;
|
|
|
|
|
if(!Files.exists(cameraConfigPath)) {
|
|
|
|
|
try {
|
|
|
|
|
FileHelper.Serializer(cameraConfigPath, preliminaryConfig);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
config = FileHelper.DeSerializer(cameraConfigPath, CameraConfig.class);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
configList.add(config);
|
2019-11-19 12:06:37 -08:00
|
|
|
}
|
2019-11-19 12:43:38 -05:00
|
|
|
});
|
|
|
|
|
|
2019-11-19 12:06:37 -08:00
|
|
|
return configList;
|
2019-11-19 12:43:38 -05:00
|
|
|
}
|
2019-11-23 19:39:24 +02:00
|
|
|
public static void saveCameraPipelines(String cameraName, List<CVPipelineSettings> pipelines) throws IOException {
|
|
|
|
|
Path cameraFolder = Paths.get(cameraConfigPath.toString(), cameraName);
|
|
|
|
|
Path filePath = Paths.get(cameraFolder.toString(), cameraName,"pipelines.json");
|
|
|
|
|
FileHelper.CheckPath(cameraFolder);
|
|
|
|
|
FileHelper.Serializer(filePath, pipelines);
|
|
|
|
|
}
|
|
|
|
|
public static void saveCameraDriverMode(String cameraName, CVPipelineSettings driverMode) throws IOException {
|
|
|
|
|
Path cameraFolder = Paths.get(cameraConfigPath.toString(), cameraName);
|
|
|
|
|
Path filePath = Paths.get(cameraFolder.toString(), cameraName,"driverMode.json");
|
|
|
|
|
FileHelper.CheckPath(cameraFolder);
|
|
|
|
|
FileHelper.Serializer(filePath, driverMode);
|
|
|
|
|
}
|
|
|
|
|
public static void saveCameraConfig(String cameraName, CameraConfig config) throws IOException {
|
|
|
|
|
Path cameraFolder = Paths.get(cameraConfigPath.toString(), cameraName);
|
|
|
|
|
Path filePath = Paths.get(cameraFolder.toString(), cameraName,"driverMode.json");
|
|
|
|
|
FileHelper.CheckPath(cameraFolder);
|
|
|
|
|
FileHelper.Serializer(filePath, config);
|
|
|
|
|
}
|
2019-11-19 12:43:38 -05:00
|
|
|
}
|