2019-09-10 23:47:06 +03:00
|
|
|
package Classes;
|
2019-09-14 13:27:16 +03:00
|
|
|
import Exceptions.NoCameraException;
|
2019-09-11 22:07:24 +03:00
|
|
|
import Objects.*;
|
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-14 12:45:00 +03:00
|
|
|
import Objects.CamVideoMode;
|
2019-09-12 23:23:36 +03:00
|
|
|
import com.google.gson.Gson;
|
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-13 18:20:40 +03:00
|
|
|
import org.opencv.videoio.VideoCapture;
|
2019-09-11 22:07:24 +03:00
|
|
|
|
2019-09-12 01:05:27 +03:00
|
|
|
public class SettingsManager {
|
|
|
|
|
private static SettingsManager instance;
|
|
|
|
|
private SettingsManager() {
|
|
|
|
|
InitiateGeneralSettings();
|
|
|
|
|
InitiateCamerasInfo();
|
|
|
|
|
InitiateUsbCameras();
|
2019-09-13 20:08:40 +03:00
|
|
|
InitiateCameras();
|
2019-09-12 01:05:27 +03:00
|
|
|
InitiateUsbCamerasSettings();
|
|
|
|
|
}
|
|
|
|
|
public static synchronized SettingsManager getInstance(){
|
|
|
|
|
if(instance == null){
|
|
|
|
|
synchronized (SettingsManager.class) {
|
|
|
|
|
if(instance == null){
|
|
|
|
|
instance = new SettingsManager();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2019-09-13 20:08:40 +03:00
|
|
|
public static HashMap<String,Camera> Cameras = new HashMap<String, Camera>();
|
|
|
|
|
public static HashMap<String,UsbCamera> UsbCameras = new HashMap<String, UsbCamera>();
|
2019-09-13 18:20:40 +03:00
|
|
|
public static HashMap<String,UsbCameraInfo> USBCamerasInfo = new HashMap<String,UsbCameraInfo>();
|
2019-09-14 13:27:16 +03:00
|
|
|
public static Objects.GeneralSettings GeneralSettings;
|
2019-09-14 17:14:49 +03:00
|
|
|
public static HashMap<String, String> CamerasCurrentPipeline = new HashMap<String, String>();
|
|
|
|
|
public static HashMap<String,String> CameraPort = new HashMap<String, String>();
|
2019-09-12 23:23:36 +03:00
|
|
|
private Path SettingsPath = Paths.get(System.getProperty("user.dir"),"Settings");
|
|
|
|
|
private Path CamsPath = Paths.get(SettingsPath.toString(),"Cams");
|
|
|
|
|
|
2019-09-11 22:07:24 +03:00
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-11 22:07:24 +03:00
|
|
|
private void InitiateGeneralSettings(){
|
2019-09-13 20:08:40 +03:00
|
|
|
CheckPath(SettingsPath);
|
2019-09-12 23:23:36 +03:00
|
|
|
try {
|
2019-09-14 13:27:16 +03:00
|
|
|
GeneralSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(),"Settings.json").toString()), Objects.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-11 22:07:24 +03:00
|
|
|
private void InitiateCamerasInfo(){
|
2019-09-13 18:20:40 +03:00
|
|
|
List<Integer> TrueCameras = new ArrayList<Integer>();
|
2019-09-12 23:23:36 +03:00
|
|
|
UsbCameraInfo[] UsbDevices = UsbCamera.enumerateUsbCameras();
|
2019-09-13 18:20:40 +03:00
|
|
|
for (var i=0; i < UsbDevices.length; i++){
|
|
|
|
|
var cap = new VideoCapture(UsbDevices[i].dev);
|
|
|
|
|
if (cap.isOpened()){
|
|
|
|
|
TrueCameras.add(i);
|
|
|
|
|
cap.release();
|
|
|
|
|
}
|
2019-09-13 02:45:11 +03:00
|
|
|
|
2019-09-13 18:20:40 +03:00
|
|
|
}
|
|
|
|
|
for (var i: TrueCameras){
|
|
|
|
|
var DeviceName = UsbDevices[i].name;
|
|
|
|
|
var suffix = 0;
|
|
|
|
|
while (USBCamerasInfo.containsKey(DeviceName)){
|
|
|
|
|
suffix++;
|
|
|
|
|
DeviceName = String.format("%s(%s)",UsbDevices[i].name,suffix);
|
|
|
|
|
}
|
|
|
|
|
USBCamerasInfo.put(DeviceName,UsbDevices[i]);
|
|
|
|
|
}
|
2019-09-11 22:07:24 +03:00
|
|
|
}
|
|
|
|
|
private void InitiateUsbCameras(){
|
2019-09-13 20:08:40 +03:00
|
|
|
for(Map.Entry<String,UsbCameraInfo> entry : USBCamerasInfo.entrySet()){
|
|
|
|
|
var device = entry.getValue();
|
|
|
|
|
var camera = new UsbCamera(device.name, device.dev);
|
|
|
|
|
UsbCameras.put(device.name,camera);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void InitiateCameras(){
|
|
|
|
|
CheckPath(CamsPath);
|
|
|
|
|
for(Map.Entry<String,UsbCameraInfo> entry: USBCamerasInfo.entrySet()){
|
|
|
|
|
var camPath = Paths.get(CamsPath.toString(),String.format("%s.json",entry.getKey()));
|
|
|
|
|
if(Files.exists(camPath)){
|
|
|
|
|
try {
|
|
|
|
|
Camera cam = new Gson().fromJson(new FileReader(camPath.toString()),Camera.class);
|
|
|
|
|
Cameras.put(entry.getKey(),cam);
|
2019-09-11 22:07:24 +03:00
|
|
|
|
2019-09-13 20:08:40 +03:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else{
|
|
|
|
|
CreateNewCam(entry.getKey());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-11 22:07:24 +03:00
|
|
|
}
|
|
|
|
|
private void InitiateUsbCamerasSettings(){
|
2019-09-14 12:45:00 +03:00
|
|
|
for(Map.Entry<String,UsbCamera> entry: UsbCameras.entrySet()){
|
|
|
|
|
var cam = entry.getValue();
|
|
|
|
|
var camName = entry.getKey();
|
|
|
|
|
var camInfo = Cameras.get(camName);
|
|
|
|
|
cam.setPixelFormat(VideoMode.PixelFormat.valueOf(camInfo.camVideoMode.pixel_format));
|
|
|
|
|
cam.setFPS(camInfo.camVideoMode.fps);
|
|
|
|
|
cam.setResolution(camInfo.camVideoMode.width, camInfo.camVideoMode.heigh);
|
|
|
|
|
}
|
2019-09-11 22:07:24 +03:00
|
|
|
}
|
2019-09-13 20:08:40 +03:00
|
|
|
private void CheckPath(Path path){
|
|
|
|
|
if (!Files.exists(path)){
|
|
|
|
|
try {
|
|
|
|
|
Files.createDirectories(path);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-14 12:45:00 +03:00
|
|
|
// Creators
|
2019-09-13 20:08:40 +03:00
|
|
|
private void CreateNewCam(String CameraName){
|
|
|
|
|
Camera cam = new Camera();
|
|
|
|
|
var caminfo =USBCamerasInfo.get(CameraName);
|
|
|
|
|
cam.path = caminfo.path;
|
|
|
|
|
var videomode = UsbCameras.get(CameraName).enumerateVideoModes()[0];
|
2019-09-14 12:45:00 +03:00
|
|
|
CamVideoMode CamVideoMode = new CamVideoMode();
|
2019-09-13 20:08:40 +03:00
|
|
|
CamVideoMode.fps = videomode.fps;
|
|
|
|
|
CamVideoMode.heigh = videomode.height;
|
|
|
|
|
CamVideoMode.width = videomode.width;
|
|
|
|
|
CamVideoMode.pixel_format = videomode.pixelFormat.name();
|
2019-09-14 12:45:00 +03:00
|
|
|
cam.camVideoMode = CamVideoMode;
|
2019-09-14 13:27:16 +03:00
|
|
|
cam.pipelines = new HashMap<String, Pipeline>();
|
2019-09-13 20:08:40 +03:00
|
|
|
cam.resolution = 0;
|
|
|
|
|
cam.FOV = 60.8;
|
|
|
|
|
Cameras.put(CameraName,cam);
|
2019-09-14 12:45:00 +03:00
|
|
|
|
|
|
|
|
CreateNewPipeline(null,CameraName);
|
2019-09-13 20:08:40 +03:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private void CreateNewPipeline(String PipeName, String CamName){
|
2019-09-14 12:45:00 +03:00
|
|
|
if (CamName == null){
|
2019-09-13 20:08:40 +03:00
|
|
|
CamName = GeneralSettings.curr_camera;
|
|
|
|
|
}
|
2019-09-14 12:45:00 +03:00
|
|
|
var cam = Cameras.get(CamName);
|
|
|
|
|
if (PipeName == null){
|
2019-09-13 20:08:40 +03:00
|
|
|
var suffix = 0;
|
|
|
|
|
PipeName = "pipeline" + suffix;
|
2019-09-14 12:45:00 +03:00
|
|
|
while (cam.pipelines.containsKey(PipeName)){
|
|
|
|
|
suffix ++;
|
|
|
|
|
PipeName = "pipeline"+suffix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (cam.pipelines.containsKey(PipeName)){
|
|
|
|
|
System.err.println("Pipeline Already Exists");
|
|
|
|
|
}
|
2019-09-14 13:27:16 +03:00
|
|
|
cam.pipelines.put(PipeName,new Pipeline());
|
|
|
|
|
}
|
|
|
|
|
//Access Methods
|
|
|
|
|
public Pipeline GetCurrentPipeline() throws Exception {
|
|
|
|
|
if (!GeneralSettings.curr_pipeline.equals("")){
|
|
|
|
|
return Cameras.get(GeneralSettings.curr_camera).pipelines.get(GeneralSettings.curr_pipeline);
|
|
|
|
|
}
|
|
|
|
|
throw new NoCameraException();
|
|
|
|
|
}
|
|
|
|
|
public Camera GetCurrentCamera() throws Exception {
|
|
|
|
|
if (!GeneralSettings.curr_camera.equals("")){
|
|
|
|
|
return Cameras.get(GeneralSettings.curr_camera);
|
|
|
|
|
}
|
|
|
|
|
throw new NoCameraException();
|
|
|
|
|
}
|
|
|
|
|
public List<String> GetResolutionList() throws NoCameraException {
|
|
|
|
|
if (!GeneralSettings.curr_camera.equals("")){
|
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
|
var cam = UsbCameras.get(GeneralSettings.curr_camera);
|
|
|
|
|
for (var res : cam.enumerateVideoModes()){
|
|
|
|
|
list.add(String.format("%s X %s at %s fps", res.width, res.height, res.fps));
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
throw new NoCameraException();
|
|
|
|
|
}
|
|
|
|
|
public void SetCurrentCamera(String CamName) throws Exception {
|
|
|
|
|
if (Cameras.containsKey(CamName)){
|
|
|
|
|
GeneralSettings.curr_camera = CamName;
|
|
|
|
|
GeneralSettings.curr_pipeline = GetCurrentCamera().pipelines.keySet().stream().findFirst().toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void SetCurrentPipeline(String PipelineName) throws Exception {
|
|
|
|
|
if (GetCurrentCamera().pipelines.containsKey(PipelineName)){
|
|
|
|
|
GeneralSettings.curr_pipeline = PipelineName;
|
|
|
|
|
}
|
2019-09-14 12:45:00 +03:00
|
|
|
}
|
|
|
|
|
//Savers
|
|
|
|
|
public void SaveSettings(){
|
|
|
|
|
SaveCameras();
|
|
|
|
|
SaveGeneralSettings();
|
|
|
|
|
}
|
|
|
|
|
private void SaveCameras(){
|
|
|
|
|
for(Map.Entry<String,Camera> entry: Cameras.entrySet()){
|
|
|
|
|
try {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
FileWriter writer = new FileWriter(Paths.get(CamsPath.toString(),String.format("%s.json",entry.getKey())).toString());
|
|
|
|
|
gson.toJson(entry.getValue(),writer);
|
|
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void SaveGeneralSettings(){
|
|
|
|
|
try {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
FileWriter writer = new FileWriter(Paths.get(SettingsPath.toString(),"Settings.json").toString());
|
|
|
|
|
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
|
|
|
}
|