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;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.vision.GeneralSettings;
|
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-20 02:17:22 -04:00
|
|
|
import com.google.gson.GsonBuilder;
|
2019-09-16 04:10:26 +03:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
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
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
var allCameras = CameraManager.getAllCamerasByName();
|
|
|
|
|
if (!allCameras.containsKey(GeneralSettings.curr_camera) && allCameras.size() > 0) {
|
|
|
|
|
var cam = allCameras.entrySet().stream().findFirst().get().getValue();
|
|
|
|
|
GeneralSettings.curr_camera = cam.name;
|
|
|
|
|
GeneralSettings.curr_pipeline = cam.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-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> 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-14 13:27:16 +03:00
|
|
|
//Access Methods
|
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 SaveGeneralSettings() {
|
2019-09-14 12:45:00 +03:00
|
|
|
try {
|
2019-09-20 02:17:22 -04:00
|
|
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
|
FileWriter writer = new FileWriter(Paths.get(SettingsPath.toString(), "settings.json").toString());
|
|
|
|
|
gson.toJson(GeneralSettings, writer);
|
2019-09-14 12:45:00 +03:00
|
|
|
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
|
|
|
}
|