2019-09-10 23:47:06 +03:00
|
|
|
package Classes;
|
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
|
|
|
|
|
|
|
|
import Objects.VideoMode;
|
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 20:08:40 +03:00
|
|
|
import org.opencv.video.Video;
|
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-11 22:07:24 +03:00
|
|
|
public static DefaultGeneralSettings GeneralSettings;
|
|
|
|
|
public static HashMap CameraPort = new HashMap();
|
|
|
|
|
public static HashMap CamerasCurrentPipeline = new HashMap();
|
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
|
|
|
|
|
|
|
|
private void InitiateGeneralSettings(){
|
2019-09-13 20:08:40 +03:00
|
|
|
CheckPath(SettingsPath);
|
2019-09-12 23:23:36 +03:00
|
|
|
try {
|
|
|
|
|
GeneralSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(),"Settings.json").toString()),DefaultGeneralSettings.class);
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
GeneralSettings = new DefaultGeneralSettings();
|
|
|
|
|
}
|
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-10 23:47:06 +03:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
VideoMode CamVideoMode = new VideoMode();
|
|
|
|
|
CamVideoMode.fps = videomode.fps;
|
|
|
|
|
CamVideoMode.heigh = videomode.height;
|
|
|
|
|
CamVideoMode.width = videomode.width;
|
|
|
|
|
CamVideoMode.pixel_format = videomode.pixelFormat.name();
|
|
|
|
|
|
|
|
|
|
cam.pipelines = new HashMap<String, DefaultPipeline>();
|
|
|
|
|
cam.resolution = 0;
|
|
|
|
|
cam.FOV = 60.8;
|
|
|
|
|
Cameras.put(CameraName,cam);
|
|
|
|
|
CreateNewPipeline("",CameraName);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private void CreateNewPipeline(String PipeName, String CamName){
|
|
|
|
|
if (CamName.equals("")){
|
|
|
|
|
CamName = GeneralSettings.curr_camera;
|
|
|
|
|
}
|
|
|
|
|
if (PipeName.equals("")){
|
|
|
|
|
var suffix = 0;
|
|
|
|
|
PipeName = "pipeline" + suffix;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 23:47:06 +03:00
|
|
|
}
|