mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-24 01:31:44 +00:00
Move Java backend to properly named folder
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CVPipelineSettingsList extends List<CVPipelineSettings> {
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
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.List;
|
||||
|
||||
public class CameraConfig {
|
||||
|
||||
private static final Path camerasConfigFolderPath = Paths.get(ConfigManager.SettingsPath.toString(), "cameras");
|
||||
|
||||
private final String cameraConfigName;
|
||||
private final CameraJsonConfig preliminaryConfig;
|
||||
|
||||
public final PipelineConfig pipelineConfig;
|
||||
|
||||
CameraConfig(CameraJsonConfig config) {
|
||||
preliminaryConfig = config;
|
||||
cameraConfigName = preliminaryConfig.name.replace(' ', '_');
|
||||
pipelineConfig = new PipelineConfig(this);
|
||||
}
|
||||
|
||||
public FullCameraConfiguration load() {
|
||||
checkFolder();
|
||||
checkConfig();
|
||||
checkDriverMode();
|
||||
pipelineConfig.check();
|
||||
|
||||
return new FullCameraConfiguration(loadConfig(), pipelineConfig.load(), loadDriverMode(), this);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private 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) {
|
||||
pipelineConfig.save(pipelines);
|
||||
}
|
||||
|
||||
public void saveDriverMode(CVPipelineSettings driverMode) {
|
||||
try {
|
||||
JacksonHelper.serializer(getDriverModePath(), driverMode);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Failed to save camera drivermode file: " + getDriverModePath().toString());
|
||||
}
|
||||
}
|
||||
|
||||
void checkFolder() {
|
||||
if (!getConfigFolderExists()) {
|
||||
try {
|
||||
if (!(new File(getConfigFolderPath().toUri()).mkdirs())) {
|
||||
System.err.println("Failed to create camera config folder: " + getConfigFolderPath().toString());
|
||||
}
|
||||
} catch(Exception e) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Path getConfigFolderPath() {
|
||||
return Paths.get(camerasConfigFolderPath.toString(), cameraConfigName);
|
||||
}
|
||||
|
||||
private Path getConfigPath() {
|
||||
return Paths.get(getConfigFolderPath().toString(), "camera.json");
|
||||
}
|
||||
|
||||
private Path getDriverModePath() {
|
||||
return Paths.get(getConfigFolderPath().toString(), "drivermode.json");
|
||||
}
|
||||
|
||||
private boolean getConfigFolderExists() {
|
||||
return Files.exists(getConfigFolderPath());
|
||||
}
|
||||
|
||||
Path getPipelineFolderPath() {
|
||||
return Paths.get(getConfigFolderPath().toString(), "pipelines");
|
||||
}
|
||||
|
||||
private boolean configExists() {
|
||||
return getConfigFolderExists() && Files.exists(getConfigPath());
|
||||
}
|
||||
|
||||
private boolean driverModeExists() {
|
||||
return getConfigFolderExists() && Files.exists(getDriverModePath());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.vision.VisionProcess;
|
||||
import com.chameleonvision.vision.camera.USBCameraProperties;
|
||||
import com.chameleonvision.vision.enums.StreamDivisor;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class CameraJsonConfig {
|
||||
public final double fov;
|
||||
public final String path;
|
||||
public final String name;
|
||||
public final String nickname;
|
||||
public final int videomode;
|
||||
public final StreamDivisor streamDivisor;
|
||||
|
||||
@JsonCreator
|
||||
public CameraJsonConfig(
|
||||
@JsonProperty("fov") double fov,
|
||||
@JsonProperty("path") String path,
|
||||
@JsonProperty("name") String name,
|
||||
@JsonProperty("nickname") String nickname,
|
||||
@JsonProperty("videomode") int videomode,
|
||||
@JsonProperty("streamDivisor") StreamDivisor streamDivisor) {
|
||||
this.fov = fov;
|
||||
this.path = path;
|
||||
this.name = name;
|
||||
this.nickname = nickname;
|
||||
this.videomode = videomode;
|
||||
this.streamDivisor = streamDivisor;
|
||||
}
|
||||
|
||||
public CameraJsonConfig(String path, String name) {
|
||||
this.fov = USBCameraProperties.DEFAULT_FOV;
|
||||
this.path = path;
|
||||
this.name = name;
|
||||
this.nickname = name;
|
||||
this.videomode = 0;
|
||||
this.streamDivisor = StreamDivisor.NONE;
|
||||
}
|
||||
|
||||
public static CameraJsonConfig fromVisionProcess(VisionProcess process) {
|
||||
USBCameraProperties camProps = process.getCamera().getProperties();
|
||||
int videomode = camProps.getCurrentVideoModeIndex();
|
||||
StreamDivisor streamDivisor = process.cameraStreamer.getDivisor();
|
||||
return new CameraJsonConfig(camProps.getFOV(), camProps.path, camProps.name, camProps.getNickname(), videomode, streamDivisor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.util.ProgramDirectoryUtilities;
|
||||
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.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigManager {
|
||||
private ConfigManager() {}
|
||||
|
||||
static final Path SettingsPath = Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "settings");
|
||||
private static final Path settingsFilePath = Paths.get(SettingsPath.toString(), "settings.json");
|
||||
|
||||
private static final LinkedHashMap<String, CameraConfig> cameraConfigs = new LinkedHashMap<>();
|
||||
|
||||
public static GeneralSettings settings = new GeneralSettings();
|
||||
|
||||
private static boolean settingsFolderExists() { return Files.exists(SettingsPath); }
|
||||
private static boolean settingsFileExists() { return settingsFolderExists() && Files.exists(settingsFilePath); }
|
||||
|
||||
private static void checkSettingsFolder() {
|
||||
if (!settingsFolderExists()) {
|
||||
try {
|
||||
if( !(new File(SettingsPath.toUri()).mkdirs()) ) {
|
||||
System.err.println("Failed to create settings folder: " + SettingsPath.toString());
|
||||
}
|
||||
Files.createDirectory(SettingsPath);
|
||||
} catch (IOException e) {
|
||||
if(!(e instanceof java.nio.file.FileAlreadyExistsException))
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkSettingsFile() {
|
||||
boolean settingsFileEmpty = settingsFileExists() && new File(settingsFilePath.toString()).length() == 0;
|
||||
if (settingsFileEmpty || !settingsFileExists()) {
|
||||
try {
|
||||
JacksonHelper.serializer(settingsFilePath, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
settings = JacksonHelper.deserializer(settingsFilePath, GeneralSettings.class);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Failed to load settings.json, using defaults.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void initializeSettings() {
|
||||
System.out.println("Settings folder: " + SettingsPath.toString());
|
||||
checkSettingsFolder();
|
||||
checkSettingsFile();
|
||||
}
|
||||
|
||||
private static void saveSettingsFile() {
|
||||
try {
|
||||
JacksonHelper.serializer(settingsFilePath, settings);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Failed to save settings.json!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveGeneralSettings() {
|
||||
checkSettingsFolder();
|
||||
saveSettingsFile();
|
||||
}
|
||||
|
||||
public static List<FullCameraConfiguration> initializeCameras(List<CameraJsonConfig> preliminaryConfigs) {
|
||||
List<FullCameraConfiguration> configList = new ArrayList<>();
|
||||
|
||||
checkSettingsFolder();
|
||||
|
||||
// loop over all the camera names and try to create settings folders for it
|
||||
for (CameraJsonConfig preliminaryConfig : preliminaryConfigs) {
|
||||
CameraConfig cameraConfiguration = new CameraConfig(preliminaryConfig);
|
||||
cameraConfigs.put(preliminaryConfig.name, cameraConfiguration);
|
||||
|
||||
FullCameraConfiguration camJsonConfig = cameraConfiguration.load();
|
||||
|
||||
configList.add(camJsonConfig);
|
||||
}
|
||||
|
||||
return configList;
|
||||
}
|
||||
|
||||
public static void saveCameraConfig(String cameraName, CameraJsonConfig config) {
|
||||
var camConf = cameraConfigs.get(cameraName);
|
||||
camConf.saveConfig(config);
|
||||
}
|
||||
|
||||
public static void saveCameraPipelines(String cameraName, List<CVPipelineSettings> pipelines) {
|
||||
var camConf = cameraConfigs.get(cameraName);
|
||||
camConf.savePipelines(pipelines);
|
||||
}
|
||||
|
||||
public static void saveCameraDriverMode(String cameraName, CVPipelineSettings driverMode) {
|
||||
var camConf = cameraConfigs.get(cameraName);
|
||||
camConf.saveDriverMode(driverMode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FullCameraConfiguration {
|
||||
public final CameraJsonConfig cameraConfig;
|
||||
public final List<CVPipelineSettings> pipelines;
|
||||
public final CVPipelineSettings drivermode;
|
||||
public final CameraConfig fileConfig;
|
||||
|
||||
FullCameraConfiguration(CameraJsonConfig cameraConfig, List<CVPipelineSettings> pipelines, CVPipelineSettings drivermode, CameraConfig fileConfig) {
|
||||
this.cameraConfig = cameraConfig;
|
||||
this.pipelines = pipelines;
|
||||
this.drivermode = drivermode;
|
||||
this.fileConfig = fileConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.network.NetworkIPMode;
|
||||
|
||||
public class GeneralSettings {
|
||||
public int teamNumber = 1577;
|
||||
public NetworkIPMode connectionType = NetworkIPMode.DHCP;
|
||||
public String ip = "";
|
||||
public String gateway = "";
|
||||
public String netmask = "";
|
||||
public String hostname = "Chameleon-vision";
|
||||
public String currentCamera = "";
|
||||
public Integer currentPipeline = null;
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.util.JacksonHelper;
|
||||
import com.chameleonvision.vision.pipeline.*;
|
||||
|
||||
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 PipelineConfig {
|
||||
|
||||
private static final String CVPipeline2DPrefix = "CV2D";
|
||||
private static final String CVPipeline3DPrefix = "CV3D";
|
||||
|
||||
private final CameraConfig cameraConfig;
|
||||
|
||||
/**
|
||||
* Construct a new PipelineConfig
|
||||
* @param cameraConfig the CameraConfig (parent folder, kinda?)
|
||||
*/
|
||||
PipelineConfig(CameraConfig cameraConfig) {
|
||||
this.cameraConfig = cameraConfig;
|
||||
}
|
||||
|
||||
private void checkFolder() {
|
||||
if ( !(new File(cameraConfig.getPipelineFolderPath().toUri()).mkdirs())) {
|
||||
if (Files.notExists(cameraConfig.getPipelineFolderPath())) {
|
||||
System.err.println("Failed to create pipelines folder.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private File[] getPipelineFiles() {
|
||||
return new File(cameraConfig.getPipelineFolderPath().toUri()).listFiles();
|
||||
}
|
||||
|
||||
private boolean folderHasPipelines() {
|
||||
File[] folderContents = getPipelineFiles();
|
||||
if(folderContents == null) return false;
|
||||
return folderContents.length > 0;
|
||||
}
|
||||
|
||||
void check() {
|
||||
cameraConfig.checkFolder();
|
||||
checkFolder();
|
||||
// Check if there's at least one pipe
|
||||
if (!folderHasPipelines()) {
|
||||
save(new CVPipeline2dSettings());
|
||||
}
|
||||
}
|
||||
|
||||
private Path getPipelinePath(CVPipelineSettings setting) {
|
||||
String pipelineName = setting.nickname.replace(' ', '_');
|
||||
String prefix = ((setting instanceof CVPipeline2dSettings) ? CVPipeline2DPrefix : CVPipeline3DPrefix) + "-";
|
||||
String fullFileName = prefix + pipelineName + ".json";
|
||||
return Path.of(cameraConfig.getPipelineFolderPath().toString(), fullFileName);
|
||||
}
|
||||
|
||||
private boolean pipelineExists(CVPipelineSettings setting) {
|
||||
return Files.exists(getPipelinePath(setting));
|
||||
}
|
||||
|
||||
public void save(CVPipelineSettings settings) {
|
||||
|
||||
var path = getPipelinePath(settings);
|
||||
|
||||
if (settings instanceof CVPipeline3dSettings) {
|
||||
try {
|
||||
JacksonHelper.serializer(path, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (settings instanceof CVPipeline2dSettings) {
|
||||
try {
|
||||
JacksonHelper.serializer(path, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("saving non-2d and non-3d pipelines not implemented~");
|
||||
}
|
||||
}
|
||||
|
||||
public void save(List<CVPipelineSettings> settings) {
|
||||
for(CVPipelineSettings setting : settings) {
|
||||
save(setting);
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(CVPipelineSettings setting) {
|
||||
if(pipelineExists(setting)) {
|
||||
try {
|
||||
Files.delete(getPipelinePath(setting));
|
||||
} catch (IOException e) {
|
||||
System.err.println("Failed to delete pipeline!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CVPipelineSettings rename(CVPipelineSettings setting, String newName) {
|
||||
if (pipelineExists(setting)) {
|
||||
delete(setting);
|
||||
setting.nickname = newName;
|
||||
save(setting);
|
||||
} else {
|
||||
setting.nickname = newName;
|
||||
save(setting);
|
||||
}
|
||||
return setting;
|
||||
}
|
||||
|
||||
public List<CVPipelineSettings> load() {
|
||||
check(); // TODO: this ensures there will be a default pipeline. is the check later necessary?
|
||||
|
||||
File[] pipelineFiles = getPipelineFiles();
|
||||
List<CVPipelineSettings> deserializedList = new ArrayList<>();
|
||||
|
||||
if(pipelineFiles == null || pipelineFiles.length < 1) {
|
||||
// TODO handle no pipelines to load
|
||||
System.err.println("no pipes to load! loading default");
|
||||
} else {
|
||||
for(File pipelineFile : pipelineFiles) {
|
||||
var name = pipelineFile.getName();
|
||||
if(name.startsWith(CVPipeline3DPrefix)) {
|
||||
// try to load 3d pipe
|
||||
try {
|
||||
var pipe = JacksonHelper.deserializer(Paths.get(pipelineFile.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(pipelineFile.getPath()), CVPipeline2dSettings.class);
|
||||
deserializedList.add(pipe);
|
||||
} catch (IOException e) {
|
||||
System.err.println("couldn't load cvpipeline2d");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deserializedList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user