mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Start adding config deserialization to ConfigManager
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.chameleonvision.classabstraction;
|
||||
|
||||
import com.chameleonvision.classabstraction.config.ConfigManager;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.util.FileHelper;
|
||||
import com.google.gson.Gson;
|
||||
@@ -13,6 +14,7 @@ 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.LinkedHashMap;
|
||||
|
||||
public class VisionManager {
|
||||
@@ -45,43 +47,46 @@ public class VisionManager {
|
||||
|
||||
FileHelper.CheckPath(CamConfigPath);
|
||||
|
||||
UsbCameraInfosByCameraName.forEach((cameraName, cameraInfo) -> {
|
||||
Path cameraConfigFolder = Paths.get(CamConfigPath.toString(), String.format("%s\\", cameraName));
|
||||
Path cameraConfigPath = Paths.get(cameraConfigFolder.toString(), String.format("%s.json", cameraName));
|
||||
Path cameraPipelinesPath = Paths.get(cameraConfigFolder.toString(), "pipelines.json");
|
||||
Path cameraDrivermodePath = Paths.get(cameraConfigFolder.toString(), "drivermode.json");
|
||||
// load the config
|
||||
var loadedConfigs = ConfigManager.initializeCameraConfig(new ArrayList<>(UsbCameraInfosByCameraName.keySet()));
|
||||
|
||||
try {
|
||||
|
||||
boolean cameraFolderExists = Files.exists(cameraConfigFolder);
|
||||
|
||||
if (!cameraFolderExists) {
|
||||
Files.createDirectory(cameraConfigFolder);
|
||||
}
|
||||
boolean cameraConfigExists = cameraFolderExists && Files.exists(cameraConfigPath);
|
||||
|
||||
if (Files.exists(cameraConfigFolder)) {
|
||||
if (Files.exists(cameraConfigPath)) {
|
||||
File cameraConfigFile = new File(cameraConfigPath.toString());
|
||||
if (cameraConfigFile.length() != 0) {
|
||||
try {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Files.createFile(cameraConfigPath);
|
||||
}
|
||||
} else {
|
||||
Files.createDirectory(cameraConfigFolder);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
// UsbCameraInfosByCameraName.forEach((cameraName, cameraInfo) -> {
|
||||
// Path cameraConfigFolder = Paths.get(CamConfigPath.toString(), String.format("%s\\", cameraName));
|
||||
// Path cameraConfigPath = Paths.get(cameraConfigFolder.toString(), String.format("%s.json", cameraName));
|
||||
// Path cameraPipelinesPath = Paths.get(cameraConfigFolder.toString(), "pipelines.json");
|
||||
// Path cameraDrivermodePath = Paths.get(cameraConfigFolder.toString(), "drivermode.json");
|
||||
//
|
||||
// try {
|
||||
//
|
||||
// boolean cameraFolderExists = Files.exists(cameraConfigFolder);
|
||||
//
|
||||
// if (!cameraFolderExists) {
|
||||
// Files.createDirectory(cameraConfigFolder);
|
||||
// }
|
||||
// boolean cameraConfigExists = cameraFolderExists && Files.exists(cameraConfigPath);
|
||||
//
|
||||
// if (Files.exists(cameraConfigFolder)) {
|
||||
// if (Files.exists(cameraConfigPath)) {
|
||||
// File cameraConfigFile = new File(cameraConfigPath.toString());
|
||||
// if (cameraConfigFile.length() != 0) {
|
||||
// try {
|
||||
// Gson gson = new GsonBuilder().create();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// Files.createFile(cameraConfigPath);
|
||||
// }
|
||||
// } else {
|
||||
// Files.createDirectory(cameraConfigFolder);
|
||||
// }
|
||||
// } catch (IOException ex) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// });
|
||||
|
||||
// FileHelper.CheckPath(CamConfigPath);
|
||||
// UsbCameraInfosByCameraName.forEach((cameraName, cameraInfo) -> {
|
||||
|
||||
@@ -18,7 +18,7 @@ public class USBCameraProcess implements CameraProcess {
|
||||
public USBCameraProcess(UsbCamera camera, CameraConfig config) {
|
||||
baseCamera = camera;
|
||||
cvSink = CameraServer.getInstance().getVideo(baseCamera);
|
||||
properties = new CameraProperties(baseCamera, config.FOV);
|
||||
properties = new CameraProperties(baseCamera, config.fov);
|
||||
|
||||
setVideoMode(properties.videoModes.get(0));
|
||||
}
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
package com.chameleonvision.classabstraction.config;
|
||||
|
||||
public class CameraConfig {
|
||||
public double FOV;
|
||||
public String Path;
|
||||
public String Name;
|
||||
public String Nickname;
|
||||
public double fov;
|
||||
public String path;
|
||||
public String name;
|
||||
public String nickname;
|
||||
|
||||
public CameraConfig(double FOV,
|
||||
String path, String name,
|
||||
String nickname) {
|
||||
this.fov = FOV;
|
||||
this.path = path;
|
||||
this.name = name;
|
||||
this.nickname = nickname;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,20 +2,30 @@ package com.chameleonvision.classabstraction.config;
|
||||
|
||||
import com.chameleonvision.classabstraction.util.ProgramDirectoryUtilities;
|
||||
import com.chameleonvision.settings.GeneralSettings;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.util.FileHelper;
|
||||
import com.chameleonvision.vision.camera.CameraDeserializer;
|
||||
import com.chameleonvision.vision.camera.USBCamera;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigManager {
|
||||
private ConfigManager() {}
|
||||
|
||||
private static final Path SettingsPath = Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "settings");
|
||||
private static final Path CamConfigPath = Paths.get(SettingsPath.toString(), "cameras");
|
||||
private static final Path cameraConfigPath = Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "cameras");
|
||||
|
||||
public static GeneralSettings settings = new GeneralSettings();
|
||||
|
||||
@@ -30,6 +40,7 @@ public class ConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
// try to load the settings file and deserialize it
|
||||
Path settingsFilePath = Paths.get(SettingsPath.toString(), "settings.json");
|
||||
boolean settingsFileExists = Files.exists(settingsFilePath);
|
||||
|
||||
@@ -47,16 +58,23 @@ public class ConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
// try to load camera file and deserialize it
|
||||
// var cameraConfigs = initializeCameraConfig();
|
||||
|
||||
}
|
||||
|
||||
public static List<CameraConfig> initializeCameraConfig(List<String> cameraNames) {
|
||||
|
||||
var configList = new ArrayList<CameraConfig>();
|
||||
|
||||
// loop over all the camera names and try to create settings folders for it
|
||||
cameraNames.forEach((cameraName) -> {
|
||||
final Path cameraConfigFolder = Paths.get(CamConfigPath.toString(), String.format("%s\\", cameraName));
|
||||
final Path cameraConfigFolder = Paths.get(cameraConfigPath.toString(), String.format("%s\\", cameraName));
|
||||
final Path cameraConfigPath = Paths.get(cameraConfigFolder.toString(), String.format("%s.json", cameraName));
|
||||
final Path cameraPipelinesPath = Paths.get(cameraConfigFolder.toString(), "pipelines.json");
|
||||
final Path cameraDrivermodePath = Paths.get(cameraConfigFolder.toString(), "drivermode.json");
|
||||
|
||||
// check if the config folder exists, and if not, create it
|
||||
boolean cameraConfigFolderExists = Files.exists(cameraConfigFolder);
|
||||
|
||||
if (!cameraConfigFolderExists) {
|
||||
@@ -67,8 +85,30 @@ public class ConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
// try to deserialize the file
|
||||
var camJsonFile = new File(cameraConfigPath.toString());
|
||||
if(camJsonFile.exists() && camJsonFile.length() > 0) {
|
||||
try {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(USBCamera.class, new CameraDeserializer()).create();
|
||||
var camJsonFileReader = new FileReader(camJsonFile.toString());
|
||||
var gsonRead = gson.fromJson(camJsonFileReader, CameraConfig.class);
|
||||
configList.add(gsonRead);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
// create a default one
|
||||
configList.add(new CameraConfig(
|
||||
70.0,
|
||||
cameraConfigPath.toString(),
|
||||
cameraName,
|
||||
cameraName
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
return new ArrayList<>();
|
||||
return configList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public enum Platform {
|
||||
}
|
||||
}
|
||||
|
||||
private static Platform getCurrentPlatform() {
|
||||
public static Platform getCurrentPlatform() {
|
||||
if (OS_NAME.contains("Windows")) {
|
||||
if (OS_ARCH.equals("amd64")) return Platform.WINDOWS_64;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user