2019-09-21 16:04:58 +03:00
|
|
|
package com.chameleonvision.web;
|
|
|
|
|
|
2019-11-27 19:39:08 -05:00
|
|
|
import com.chameleonvision.config.ConfigManager;
|
2019-11-23 11:55:20 -05:00
|
|
|
import com.chameleonvision.vision.VisionManager;
|
|
|
|
|
import com.chameleonvision.vision.VisionProcess;
|
2019-11-25 05:34:04 -05:00
|
|
|
import com.chameleonvision.vision.camera.CameraCapture;
|
2019-11-30 19:24:03 +02:00
|
|
|
import com.chameleonvision.vision.enums.StreamDivisor;
|
2019-11-23 11:55:20 -05:00
|
|
|
import com.chameleonvision.vision.pipeline.CVPipeline;
|
|
|
|
|
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
2019-10-14 11:21:20 +03:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-11-30 11:02:10 -08:00
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
2019-11-27 19:39:08 -05:00
|
|
|
import io.javalin.websocket.WsBinaryMessageContext;
|
|
|
|
|
import io.javalin.websocket.WsCloseContext;
|
|
|
|
|
import io.javalin.websocket.WsConnectContext;
|
|
|
|
|
import io.javalin.websocket.WsContext;
|
2019-10-11 02:25:25 +03:00
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2019-10-14 11:21:20 +03:00
|
|
|
import org.msgpack.jackson.dataformat.MessagePackFactory;
|
2019-10-13 22:59:36 +03:00
|
|
|
|
2019-09-21 16:04:58 +03:00
|
|
|
import java.lang.reflect.Field;
|
2019-10-14 11:21:20 +03:00
|
|
|
import java.nio.ByteBuffer;
|
2019-11-27 19:39:08 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2019-09-21 16:04:58 +03:00
|
|
|
|
2019-10-12 03:38:42 +03:00
|
|
|
|
2019-11-09 19:52:33 +02:00
|
|
|
public class SocketHandler {
|
2019-09-21 16:04:58 +03:00
|
|
|
|
|
|
|
|
private static List<WsContext> users;
|
2019-10-14 11:21:20 +03:00
|
|
|
private static ObjectMapper objectMapper;
|
2019-09-21 16:04:58 +03:00
|
|
|
|
2019-11-09 19:52:33 +02:00
|
|
|
SocketHandler() {
|
2019-09-21 13:05:00 -04:00
|
|
|
users = new ArrayList<>();
|
2019-10-14 11:21:20 +03:00
|
|
|
objectMapper = new ObjectMapper(new MessagePackFactory());
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-21 13:05:00 -04:00
|
|
|
void onConnect(WsConnectContext context) {
|
2019-09-21 16:04:58 +03:00
|
|
|
users.add(context);
|
|
|
|
|
sendFullSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 05:34:04 -05:00
|
|
|
void onClose(WsCloseContext context) {
|
2019-09-21 16:04:58 +03:00
|
|
|
users.remove(context);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 05:34:04 -05:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-10-14 22:24:37 +03:00
|
|
|
void onBinaryMessage(WsBinaryMessageContext context) throws Exception {
|
2019-11-25 05:34:04 -05:00
|
|
|
Map<String, Object> deserialized = objectMapper.readValue(ArrayUtils.toPrimitive(context.data()), new TypeReference<>() {
|
2019-10-20 10:13:07 +03:00
|
|
|
});
|
|
|
|
|
for (Map.Entry<String, Object> entry : deserialized.entrySet()) {
|
2019-10-13 21:58:34 +03:00
|
|
|
try {
|
2019-11-23 04:05:37 -05:00
|
|
|
VisionProcess currentProcess = VisionManager.getCurrentUIVisionProcess();
|
2019-11-25 05:34:04 -05:00
|
|
|
CameraCapture currentCamera = currentProcess.getCamera();
|
2019-11-29 23:13:57 -05:00
|
|
|
CVPipeline currentPipeline = currentProcess.pipelineManager.getCurrentPipeline();
|
2019-11-23 04:05:37 -05:00
|
|
|
|
2019-10-14 11:21:20 +03:00
|
|
|
switch (entry.getKey()) {
|
2019-11-01 17:01:10 +02:00
|
|
|
case "driverMode": {
|
2019-11-25 05:34:04 -05:00
|
|
|
HashMap<String, Object> data = (HashMap<String, Object>) entry.getValue();
|
2019-11-29 21:18:07 -08:00
|
|
|
currentProcess.getDriverModeSettings().exposure = (Integer) data.get("driverExposure");
|
|
|
|
|
currentProcess.getDriverModeSettings().brightness = (Integer) data.get("driverBrightness");
|
2019-11-23 20:02:21 -05:00
|
|
|
currentProcess.setDriverMode((Boolean) data.get("isDriver"));
|
2019-11-23 04:05:37 -05:00
|
|
|
|
2019-11-26 23:03:07 -05:00
|
|
|
VisionManager.saveCurrentCameraDriverMode();
|
2019-11-01 17:01:10 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
case "changeCameraName": {
|
2019-11-23 04:05:37 -05:00
|
|
|
currentCamera.getProperties().setNickname((String) entry.getValue());
|
2019-11-30 11:10:57 -08:00
|
|
|
currentProcess.setCameraName((String) entry.getValue());
|
2019-10-25 14:02:42 +03:00
|
|
|
sendFullSettings();
|
2019-11-26 23:03:07 -05:00
|
|
|
VisionManager.saveCurrentCameraSettings();
|
2019-10-24 15:20:15 -04:00
|
|
|
break;
|
2019-10-19 17:45:40 +03:00
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
case "changePipelineName": {
|
2019-11-30 11:10:57 -08:00
|
|
|
currentProcess.pipelineManager.renameCurrentPipeline((String) entry.getValue());
|
2019-10-25 15:57:46 +03:00
|
|
|
sendFullSettings();
|
2019-11-26 23:03:07 -05:00
|
|
|
VisionManager.saveCurrentCameraPipelines();
|
2019-10-24 15:20:15 -04:00
|
|
|
break;
|
2019-10-19 17:45:40 +03:00
|
|
|
}
|
2019-11-28 13:28:36 +02:00
|
|
|
case "duplicatePipeline": {
|
2019-10-24 15:20:15 -04:00
|
|
|
HashMap pipelineVals = (HashMap) entry.getValue();
|
|
|
|
|
int pipelineIndex = (int) pipelineVals.get("pipeline");
|
|
|
|
|
int cameraIndex = (int) pipelineVals.get("camera");
|
2019-11-28 13:28:36 +02:00
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
2019-11-29 23:13:57 -05:00
|
|
|
CVPipelineSettings origPipeline = currentProcess.pipelineManager.getPipeline(pipelineIndex).settings;
|
2019-11-28 13:28:36 +02:00
|
|
|
String val = mapper.writeValueAsString(origPipeline);
|
|
|
|
|
CVPipelineSettings newPipeline = mapper.readValue(val, origPipeline.getClass());
|
2019-11-29 00:30:29 -05:00
|
|
|
|
2019-11-29 23:13:57 -05:00
|
|
|
// TODO: move to PipelineManager
|
2019-11-28 13:28:36 +02:00
|
|
|
newPipeline.nickname += "(Copy)";
|
2019-11-29 00:30:29 -05:00
|
|
|
|
2019-10-24 15:20:15 -04:00
|
|
|
if (cameraIndex != -1) {
|
2019-11-23 04:05:37 -05:00
|
|
|
VisionProcess newProcess = VisionManager.getVisionProcessByIndex(cameraIndex);
|
2019-11-28 13:28:36 +02:00
|
|
|
if (newProcess != null) {
|
2019-11-29 23:13:57 -05:00
|
|
|
currentProcess.pipelineManager.duplicatePipeline(newPipeline, newProcess);
|
|
|
|
|
} else {
|
|
|
|
|
System.err.println("Failed to get new camera for pipeline duplication!");
|
2019-11-23 04:05:37 -05:00
|
|
|
}
|
2019-10-24 15:20:15 -04:00
|
|
|
} else {
|
2019-11-29 23:13:57 -05:00
|
|
|
currentProcess.pipelineManager.duplicatePipeline(newPipeline);
|
2019-10-24 15:20:15 -04:00
|
|
|
}
|
2019-11-29 00:30:29 -05:00
|
|
|
|
2019-11-26 23:03:07 -05:00
|
|
|
VisionManager.saveCurrentCameraPipelines();
|
2019-11-28 13:28:36 +02:00
|
|
|
sendFullSettings();
|
2019-10-24 15:20:15 -04:00
|
|
|
break;
|
2019-10-19 17:45:40 +03:00
|
|
|
}
|
2019-10-13 22:59:36 +03:00
|
|
|
case "command": {
|
2019-10-20 10:13:07 +03:00
|
|
|
switch ((String) entry.getValue()) {
|
2019-10-19 16:58:10 +03:00
|
|
|
case "addNewPipeline":
|
2019-11-29 23:13:57 -05:00
|
|
|
// TODO: add to UI selection for new 2d/3d
|
|
|
|
|
boolean is3d = false;
|
|
|
|
|
currentProcess.pipelineManager.addNewPipeline(is3d);
|
2019-10-25 14:05:00 +03:00
|
|
|
sendFullSettings();
|
2019-11-26 23:03:07 -05:00
|
|
|
VisionManager.saveCurrentCameraPipelines();
|
2019-10-19 16:58:10 +03:00
|
|
|
break;
|
|
|
|
|
case "deleteCurrentPipeline":
|
2019-11-29 23:13:57 -05:00
|
|
|
currentProcess.pipelineManager.deleteCurrentPipeline();
|
2019-11-28 13:42:53 +02:00
|
|
|
sendFullSettings();
|
|
|
|
|
VisionManager.saveCurrentCameraPipelines();
|
2019-10-29 21:26:13 +02:00
|
|
|
break;
|
|
|
|
|
case "save":
|
2019-11-26 23:03:07 -05:00
|
|
|
ConfigManager.saveGeneralSettings();
|
2019-11-27 14:59:36 -05:00
|
|
|
VisionManager.saveAllCameras();
|
|
|
|
|
System.out.println("Saved Settings");
|
2019-10-19 16:58:10 +03:00
|
|
|
break;
|
|
|
|
|
}
|
2019-10-13 22:59:36 +03:00
|
|
|
// used to define all incoming commands
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "currentCamera": {
|
2019-11-23 04:05:37 -05:00
|
|
|
VisionManager.setCurrentProcessByIndex((Integer) entry.getValue());
|
2019-10-25 15:57:46 +03:00
|
|
|
sendFullSettings();
|
2019-10-13 22:59:36 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "currentPipeline": {
|
2019-11-29 23:13:57 -05:00
|
|
|
currentProcess.pipelineManager.setCurrentPipeline((Integer) entry.getValue());
|
2019-10-26 23:41:45 +03:00
|
|
|
sendFullSettings();
|
2019-10-13 22:59:36 +03:00
|
|
|
break;
|
|
|
|
|
}
|
2019-10-14 21:22:43 +03:00
|
|
|
default: {
|
2019-11-29 21:21:40 -08:00
|
|
|
|
2019-11-29 21:46:39 -08:00
|
|
|
// only change settings when we aren't in driver mode
|
2019-11-29 21:41:12 -08:00
|
|
|
if(currentProcess.pipelineManager.getDriverMode()) break;
|
2019-11-29 21:21:40 -08:00
|
|
|
|
2019-11-29 13:58:37 +02:00
|
|
|
setField(currentPipeline.settings, entry.getKey(), entry.getValue());
|
2019-10-20 10:13:07 +03:00
|
|
|
switch (entry.getKey()) {
|
|
|
|
|
case "exposure": {
|
2019-11-23 04:05:37 -05:00
|
|
|
currentCamera.setExposure((Integer) entry.getValue());
|
2019-11-30 10:39:53 -08:00
|
|
|
break;
|
2019-10-14 23:35:29 +03:00
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
case "brightness": {
|
2019-11-23 04:05:37 -05:00
|
|
|
currentCamera.setBrightness((Integer) entry.getValue());
|
2019-11-30 10:39:53 -08:00
|
|
|
break;
|
2019-10-14 23:35:29 +03:00
|
|
|
}
|
2019-11-30 22:03:28 +02:00
|
|
|
case "videoModeIndex":{
|
2019-11-30 19:24:03 +02:00
|
|
|
currentCamera.setVideoMode((Integer) entry.getValue());
|
2019-11-30 10:39:53 -08:00
|
|
|
break;
|
2019-11-30 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
case "streamDivisor":{
|
|
|
|
|
VisionProcess currentVisionProcess = VisionManager.getCurrentUIVisionProcess();
|
|
|
|
|
currentVisionProcess.cameraStreamer.setDivisor(StreamDivisor.values()[(Integer) entry.getValue()], true);
|
2019-11-30 10:39:53 -08:00
|
|
|
break;
|
2019-11-30 19:24:03 +02:00
|
|
|
}
|
2019-10-14 23:35:29 +03:00
|
|
|
}
|
2019-11-27 19:39:08 -05:00
|
|
|
|
|
|
|
|
VisionManager.saveCurrentCameraPipelines();
|
2019-10-13 22:59:36 +03:00
|
|
|
break;
|
2019-10-13 21:58:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-13 22:59:36 +03:00
|
|
|
} catch (Exception e) {
|
2019-10-19 16:58:10 +03:00
|
|
|
System.err.println(e.getMessage());
|
2019-10-13 21:58:34 +03:00
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
broadcastMessage(deserialized, context);
|
2019-10-12 03:38:42 +03:00
|
|
|
}
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
|
2019-10-14 21:22:43 +03:00
|
|
|
private void setField(Object obj, String fieldName, Object value) {
|
2019-09-21 16:04:58 +03:00
|
|
|
try {
|
2019-10-14 21:22:43 +03:00
|
|
|
Field field = obj.getClass().getField(fieldName);
|
2019-10-30 14:32:57 +02:00
|
|
|
if (field.getType().isEnum())
|
|
|
|
|
field.set(obj, field.getType().getEnumConstants()[(Integer) value]);
|
|
|
|
|
else
|
2019-10-20 10:13:07 +03:00
|
|
|
field.set(obj, value);
|
2019-10-14 21:22:43 +03:00
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ex) {
|
|
|
|
|
ex.printStackTrace();
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 03:38:42 +03:00
|
|
|
private static void broadcastMessage(Object obj, WsContext userToSkip) {
|
|
|
|
|
if (users != null)
|
2019-11-23 04:05:37 -05:00
|
|
|
for (WsContext user : users) {
|
2019-10-12 03:38:42 +03:00
|
|
|
if (userToSkip != null && user.getSessionId().equals(userToSkip.getSessionId())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
try {
|
2019-10-14 11:21:20 +03:00
|
|
|
ByteBuffer b = ByteBuffer.wrap(objectMapper.writeValueAsBytes(obj));
|
|
|
|
|
user.send(b);
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2019-10-12 03:38:42 +03:00
|
|
|
}
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-16 14:13:12 +03:00
|
|
|
public static void broadcastMessage(Object obj) {
|
2019-10-11 02:25:25 +03:00
|
|
|
broadcastMessage(obj, null);//Broadcasts the message to every user
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-27 21:46:07 +02:00
|
|
|
private static HashMap<String, Object> getOrdinalPipeline(Class cvClass) throws IllegalAccessException {
|
2019-10-20 10:13:07 +03:00
|
|
|
HashMap<String, Object> tmp = new HashMap<>();
|
2019-11-28 13:28:36 +02:00
|
|
|
for (Field field : cvClass.getFields()) { // iterate over every field in CVPipelineSettings
|
2019-11-23 18:34:10 -08:00
|
|
|
try {
|
|
|
|
|
if (!field.getType().isEnum()) { // if the field is not an enum, get it based on the current pipeline
|
2019-11-29 23:13:57 -05:00
|
|
|
tmp.put(field.getName(), field.get(VisionManager.getCurrentUIVisionProcess().pipelineManager.getCurrentPipeline().settings));
|
2019-11-23 18:34:10 -08:00
|
|
|
} else {
|
2019-11-29 23:13:57 -05:00
|
|
|
var ordinal = (Enum) field.get(VisionManager.getCurrentUIVisionProcess().pipelineManager.getCurrentPipeline().settings);
|
2019-11-23 18:34:10 -08:00
|
|
|
tmp.put(field.getName(), ordinal.ordinal());
|
|
|
|
|
}
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
e.printStackTrace();
|
2019-10-14 22:36:56 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
|
|
|
|
|
private static HashMap<String, Object> getOrdinalSettings() {
|
|
|
|
|
HashMap<String, Object> tmp = new HashMap<>();
|
2019-11-22 14:34:21 -08:00
|
|
|
tmp.put("teamNumber", ConfigManager.settings.teamNumber);
|
|
|
|
|
tmp.put("connectionType", ConfigManager.settings.connectionType.ordinal());
|
|
|
|
|
tmp.put("ip", ConfigManager.settings.ip);
|
|
|
|
|
tmp.put("gateway", ConfigManager.settings.gateway);
|
|
|
|
|
tmp.put("netmask", ConfigManager.settings.netmask);
|
|
|
|
|
tmp.put("hostname", ConfigManager.settings.hostname);
|
2019-10-15 21:00:18 +03:00
|
|
|
return tmp;
|
|
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
|
|
|
|
|
private static HashMap<String, Object> getOrdinalCameraSettings() {
|
|
|
|
|
HashMap<String, Object> tmp = new HashMap<>();
|
2019-11-23 04:05:37 -05:00
|
|
|
VisionProcess currentVisionProcess = VisionManager.getCurrentUIVisionProcess();
|
2019-11-25 05:34:04 -05:00
|
|
|
CameraCapture currentCamera = VisionManager.getCurrentUIVisionProcess().getCamera();
|
2019-11-27 18:09:27 -05:00
|
|
|
tmp.put("fov", currentCamera.getProperties().getFOV());
|
2019-11-23 04:05:37 -05:00
|
|
|
tmp.put("streamDivisor", currentVisionProcess.cameraStreamer.getDivisor().ordinal());
|
2019-11-29 14:24:34 -05:00
|
|
|
tmp.put("resolution", currentVisionProcess.getCamera().getProperties().getCurrentVideoModeIndex());
|
2019-10-16 14:13:12 +03:00
|
|
|
return tmp;
|
|
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
|
2019-09-22 02:49:30 -04:00
|
|
|
public static void sendFullSettings() {
|
2019-09-21 16:04:58 +03:00
|
|
|
//General settings
|
2019-10-15 21:00:18 +03:00
|
|
|
Map<String, Object> fullSettings = new HashMap<>();
|
2019-11-23 20:02:21 -05:00
|
|
|
|
|
|
|
|
VisionProcess currentProcess = VisionManager.getCurrentUIVisionProcess();
|
2019-11-29 23:13:57 -05:00
|
|
|
CVPipeline currentPipeline = currentProcess.pipelineManager.getCurrentPipeline();
|
2019-11-23 20:02:21 -05:00
|
|
|
|
2019-09-21 16:04:58 +03:00
|
|
|
try {
|
2019-10-15 21:00:18 +03:00
|
|
|
fullSettings.put("settings", getOrdinalSettings());
|
2019-10-20 10:13:07 +03:00
|
|
|
fullSettings.put("cameraSettings", getOrdinalCameraSettings());
|
2019-11-23 04:05:37 -05:00
|
|
|
fullSettings.put("cameraList", VisionManager.getAllCameraNicknames());
|
2019-11-27 21:46:07 +02:00
|
|
|
fullSettings.put("pipeline", getOrdinalPipeline(currentPipeline.settings.getClass()));
|
2019-11-23 20:02:21 -05:00
|
|
|
fullSettings.put("pipelineList", VisionManager.getCurrentCameraPipelineNicknames());
|
|
|
|
|
fullSettings.put("resolutionList", VisionManager.getCurrentCameraResolutionList());
|
|
|
|
|
fullSettings.put("port", currentProcess.cameraStreamer.getStreamPort());
|
2019-11-29 23:13:57 -05:00
|
|
|
fullSettings.put("currentPipelineIndex", VisionManager.getCurrentUIVisionProcess().pipelineManager.getCurrentPipelineIndex());
|
2019-11-23 20:02:21 -05:00
|
|
|
fullSettings.put("currentCameraIndex", VisionManager.getCurrentUIVisionProcessIndex());
|
|
|
|
|
} catch (IllegalAccessException e) {
|
2019-09-21 16:04:58 +03:00
|
|
|
System.err.println("No camera found!");
|
|
|
|
|
}
|
2019-10-14 21:22:43 +03:00
|
|
|
broadcastMessage(fullSettings);
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
}
|