2019-09-15 15:03:44 -04:00
|
|
|
package com.chameleonvision.settings;
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.FileHelper;
|
|
|
|
|
import com.chameleonvision.CameraException;
|
2019-09-15 15:03:44 -04:00
|
|
|
|
2019-09-12 23:23:36 +03:00
|
|
|
import java.io.*;
|
|
|
|
|
import java.nio.file.*;
|
2019-09-13 20:08:40 +03:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.vision.camera.Camera;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.vision.GeneralSettings;
|
|
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.vision.camera.CameraManager;
|
2019-09-12 23:23:36 +03:00
|
|
|
import com.google.gson.Gson;
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-13 18:20:40 +03:00
|
|
|
import java.util.ArrayList;
|
2019-09-11 22:07:24 +03:00
|
|
|
import java.util.HashMap;
|
2019-09-12 23:23:36 +03:00
|
|
|
import java.util.List;
|
2019-09-13 20:08:40 +03:00
|
|
|
import java.util.Map;
|
2019-09-12 23:23:36 +03:00
|
|
|
|
|
|
|
|
import edu.wpi.cscore.*;
|
2019-09-11 22:07:24 +03:00
|
|
|
|
2019-09-16 04:10:26 +03:00
|
|
|
public class SettingsManager {
|
2019-09-12 01:05:27 +03:00
|
|
|
private static SettingsManager instance;
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-12 01:05:27 +03:00
|
|
|
private SettingsManager() {
|
2019-09-18 10:36:29 +03:00
|
|
|
InitiateGeneralSettings();
|
2019-09-16 04:10:26 +03:00
|
|
|
|
|
|
|
|
if (!Cameras.containsKey(GeneralSettings.curr_camera) && Cameras.size() > 0) {
|
|
|
|
|
String camName = Cameras.keySet().stream().findFirst().get();
|
|
|
|
|
GeneralSettings.curr_camera = camName;
|
2019-09-19 14:07:42 -04:00
|
|
|
GeneralSettings.curr_pipeline = CameraManager.getCamera(camName).getCurrentPipelineIndex();
|
2019-09-16 04:10:26 +03:00
|
|
|
}
|
2019-09-12 01:05:27 +03:00
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
|
|
|
|
public static synchronized SettingsManager getInstance() {
|
|
|
|
|
if (instance == null) {
|
2019-09-12 01:05:27 +03:00
|
|
|
synchronized (SettingsManager.class) {
|
2019-09-16 04:10:26 +03:00
|
|
|
if (instance == null) {
|
2019-09-12 01:05:27 +03:00
|
|
|
instance = new SettingsManager();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
public static HashMap<String, Camera> Cameras = new HashMap<>();
|
|
|
|
|
public static HashMap<String, UsbCamera> UsbCameras = new HashMap<>();
|
2019-09-15 15:03:44 -04:00
|
|
|
public static com.chameleonvision.vision.GeneralSettings GeneralSettings;
|
2019-09-19 14:07:42 -04:00
|
|
|
public static HashMap<String, String> CamerasCurrentPipeline = new HashMap<>();
|
|
|
|
|
// public static HashMap<String, String> CameraPorts = new HashMap<>();//TODO Implement ports
|
|
|
|
|
public static final Path SettingsPath = Paths.get(System.getProperty("user.dir"), "Settings");
|
2019-09-11 22:07:24 +03:00
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-18 10:36:29 +03:00
|
|
|
private void InitiateGeneralSettings() {
|
2019-09-19 14:07:42 -04:00
|
|
|
FileHelper.CheckPath(SettingsPath);
|
2019-09-12 23:23:36 +03:00
|
|
|
try {
|
2019-09-16 04:10:26 +03:00
|
|
|
GeneralSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(), "Settings.json").toString()), com.chameleonvision.vision.GeneralSettings.class);
|
2019-09-12 23:23:36 +03:00
|
|
|
} catch (FileNotFoundException e) {
|
2019-09-14 13:27:16 +03:00
|
|
|
GeneralSettings = new GeneralSettings();
|
2019-09-12 23:23:36 +03:00
|
|
|
}
|
2019-09-11 22:07:24 +03:00
|
|
|
}
|
2019-09-12 01:05:27 +03:00
|
|
|
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-14 13:27:16 +03:00
|
|
|
//Access Methods
|
2019-09-19 14:07:42 -04:00
|
|
|
public List<String> GetResolutionList() throws CameraException {
|
2019-09-17 02:12:53 -04:00
|
|
|
if (!GeneralSettings.curr_camera.equals("")) {
|
2019-09-19 14:07:42 -04:00
|
|
|
List<String> list = new ArrayList<>();
|
|
|
|
|
var cam = CameraManager.getCamera(GeneralSettings.curr_camera).UsbCam;
|
2019-09-16 04:10:26 +03:00
|
|
|
for (var res : cam.enumerateVideoModes()) {
|
2019-09-14 13:27:16 +03:00
|
|
|
list.add(String.format("%s X %s at %s fps", res.width, res.height, res.fps));
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
2019-09-14 13:27:16 +03:00
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
public void updateCameraSetting(String cameraName, int pipelineNumber) {
|
|
|
|
|
GeneralSettings.curr_camera = cameraName;
|
|
|
|
|
GeneralSettings.curr_pipeline = pipelineNumber;
|
2019-09-14 13:27:16 +03:00
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
public void updatePipelineSetting(int pipelineNumber){
|
|
|
|
|
GeneralSettings.curr_pipeline = pipelineNumber;
|
2019-09-14 12:45:00 +03:00
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
|
|
|
|
|
2019-09-14 12:45:00 +03:00
|
|
|
//Savers
|
2019-09-16 04:10:26 +03:00
|
|
|
public void SaveSettings() {
|
2019-09-19 14:07:42 -04:00
|
|
|
CameraManager.saveCameras();
|
2019-09-14 12:45:00 +03:00
|
|
|
SaveGeneralSettings();
|
|
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
|
|
|
|
private void SaveCameras() {
|
|
|
|
|
for (Map.Entry<String, Camera> entry : Cameras.entrySet()) {
|
2019-09-14 12:45:00 +03:00
|
|
|
try {
|
|
|
|
|
Gson gson = new Gson();
|
2019-09-19 14:07:42 -04:00
|
|
|
FileWriter writer = new FileWriter(Paths.get(CameraManager.CamConfigPath.toString(), String.format("%s.json", entry.getKey())).toString());
|
2019-09-16 04:10:26 +03:00
|
|
|
gson.toJson(entry.getValue(), writer);
|
2019-09-14 12:45:00 +03:00
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-16 04:10:26 +03:00
|
|
|
|
|
|
|
|
private void SaveGeneralSettings() {
|
2019-09-14 12:45:00 +03:00
|
|
|
try {
|
2019-09-16 04:10:26 +03:00
|
|
|
FileWriter writer = new FileWriter(Paths.get(SettingsPath.toString(), "Settings.json").toString());
|
2019-09-14 12:45:00 +03:00
|
|
|
new Gson().toJson(GeneralSettings, writer);
|
|
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
2019-09-13 20:08:40 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-10 23:47:06 +03:00
|
|
|
}
|