Files
PhotonVision/Main/src/main/java/com/chameleonvision/web/SocketHandler.java

290 lines
14 KiB
Java
Raw Normal View History

package com.chameleonvision.web;
import com.chameleonvision.config.ConfigManager;
import com.chameleonvision.vision.VisionManager;
import com.chameleonvision.vision.VisionProcess;
import com.chameleonvision.vision.camera.CameraCapture;
import com.chameleonvision.vision.enums.ImageRotationMode;
import com.chameleonvision.vision.pipeline.CVPipeline;
2019-11-28 13:28:36 +02:00
import com.chameleonvision.vision.pipeline.CVPipeline2dSettings;
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
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-11-28 13:28:36 +02:00
import org.apache.commons.lang3.SerializationUtils;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SocketHandler {
private static List<WsContext> users;
private static ObjectMapper objectMapper;
SocketHandler() {
users = new ArrayList<>();
objectMapper = new ObjectMapper(new MessagePackFactory());
}
void onConnect(WsConnectContext context) {
users.add(context);
sendFullSettings();
}
void onClose(WsCloseContext context) {
users.remove(context);
}
@SuppressWarnings("unchecked")
2019-10-14 22:24:37 +03:00
void onBinaryMessage(WsBinaryMessageContext context) throws Exception {
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()) {
try {
2019-11-23 04:05:37 -05:00
VisionProcess currentProcess = VisionManager.getCurrentUIVisionProcess();
CameraCapture currentCamera = currentProcess.getCamera();
2019-11-23 04:05:37 -05:00
CVPipeline currentPipeline = currentProcess.getCurrentPipeline();
switch (entry.getKey()) {
2019-11-01 17:01:10 +02:00
case "driverMode": {
HashMap<String, Object> data = (HashMap<String, Object>) entry.getValue();
currentProcess.getDriverModeSettings().exposure = (Integer) data.get("exposure");
currentProcess.getDriverModeSettings().brightness = (Integer) data.get("brightness");
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-10-25 14:02:42 +03:00
sendFullSettings();
2019-11-26 23:03:07 -05:00
VisionManager.saveCurrentCameraSettings();
break;
2019-10-19 17:45:40 +03:00
}
2019-10-20 10:13:07 +03:00
case "changePipelineName": {
2019-11-23 04:05:37 -05:00
currentPipeline.settings.nickname = ((String) entry.getValue());
sendFullSettings();
2019-11-26 23:03:07 -05:00
VisionManager.saveCurrentCameraPipelines();
break;
2019-10-19 17:45:40 +03:00
}
2019-11-28 13:28:36 +02:00
case "duplicatePipeline": {
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();
CVPipelineSettings origPipeline = currentProcess.getPipelineByIndex(pipelineIndex).settings;
String val = mapper.writeValueAsString(origPipeline);
CVPipelineSettings newPipeline = mapper.readValue(val, origPipeline.getClass());
newPipeline.nickname += "(Copy)";
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) {
newProcess.addPipeline(newPipeline);
2019-11-23 04:05:37 -05:00
}
} else {
2019-11-28 13:28:36 +02:00
currentProcess.addPipeline(newPipeline);
}
2019-11-26 23:03:07 -05:00
VisionManager.saveCurrentCameraPipelines();
2019-11-28 13:28:36 +02:00
sendFullSettings();
break;
2019-10-19 17:45:40 +03:00
}
case "command": {
2019-10-20 10:13:07 +03:00
switch ((String) entry.getValue()) {
case "addNewPipeline":
currentProcess.addBlankPipeline();
2019-10-25 14:05:00 +03:00
sendFullSettings();
2019-11-26 23:03:07 -05:00
VisionManager.saveCurrentCameraPipelines();
break;
case "deleteCurrentPipeline":
2019-11-28 13:42:53 +02:00
int currentIndex = currentProcess.getCurrentPipelineIndex();
if (currentIndex == currentProcess.getPipelines().size() - 1) {
currentProcess.setPipeline(currentIndex -1, false);
}
currentProcess.deletePipeline(currentIndex);
sendFullSettings();
VisionManager.saveCurrentCameraPipelines();
// TODO remove pipeline file after deleting
break;
case "save":
2019-11-26 23:03:07 -05:00
ConfigManager.saveGeneralSettings();
VisionManager.saveAllCameras();
System.out.println("Saved Settings");
break;
}
// used to define all incoming commands
break;
}
case "currentCamera": {
2019-11-23 04:05:37 -05:00
VisionManager.setCurrentProcessByIndex((Integer) entry.getValue());
sendFullSettings();
break;
}
case "currentPipeline": {
currentProcess.setPipeline((Integer) entry.getValue(), true);
2019-10-26 23:41:45 +03:00
sendFullSettings();
break;
}
2019-11-28 09:13:16 -08:00
case "generalSettings": {
var array = (HashMap<String, Object>) entry.getValue();
array.forEach((key, value) -> {
setField(ConfigManager.settings, key, value);
});
sendFullSettings();
ConfigManager.saveGeneralSettings();
break;
}
default: {
// TODO fix this hack
String key;
Object value;
if(entry.getKey().equals("orientation")) { // FIXME the field is now called rotationMode
value = entry.getValue();
var int_ = (Integer) value;
if (int_ == 1) {
currentPipeline.settings.rotationMode = ImageRotationMode.DEG_180;
} else {
currentPipeline.settings.rotationMode = ImageRotationMode.DEG_0;
}
} else {
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-10-20 10:13:07 +03:00
case "brightness": {
2019-11-23 04:05:37 -05:00
currentCamera.setBrightness((Integer) entry.getValue());
}
}
VisionManager.saveCurrentCameraPipelines();
break;
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
2019-10-20 10:13:07 +03:00
broadcastMessage(deserialized, context);
}
}
2019-10-20 10:13:07 +03:00
private void setField(Object obj, String fieldName, Object value) {
try {
Field field = obj.getClass().getField(fieldName);
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);
} catch (NoSuchFieldException | IllegalAccessException ex) {
ex.printStackTrace();
}
}
private static void broadcastMessage(Object obj, WsContext userToSkip) {
if (users != null)
2019-11-23 04:05:37 -05:00
for (WsContext user : users) {
if (userToSkip != null && user.getSessionId().equals(userToSkip.getSessionId())) {
continue;
}
2019-10-20 10:13:07 +03:00
try {
ByteBuffer b = ByteBuffer.wrap(objectMapper.writeValueAsBytes(obj));
user.send(b);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
public static void broadcastMessage(Object obj) {
2019-10-11 02:25:25 +03:00
broadcastMessage(obj, null);//Broadcasts the message to every user
}
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
try {
if (!field.getType().isEnum()) { // if the field is not an enum, get it based on the current pipeline
tmp.put(field.getName(), field.get(VisionManager.getCurrentUIVisionProcess().getCurrentPipeline().settings));
} else {
var ordinal = (Enum) field.get(VisionManager.getCurrentUIVisionProcess().getCurrentPipeline().settings);
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);
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();
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());
// TODO: (HIGH) get videomode index!
2019-11-27 21:11:05 -08:00
// tmp.put("resolution", currentCamera.getVideoModeIndex());
return tmp;
}
2019-10-20 10:13:07 +03:00
2019-11-01 17:01:10 +02:00
private static HashMap<String, Object> getOrdinalDriver() {
HashMap<String, Object> tmp = new HashMap<>();
2019-11-23 04:05:37 -05:00
VisionProcess currentProcess = VisionManager.getCurrentUIVisionProcess();
CVPipelineSettings driverModeSettings = currentProcess.getDriverModeSettings();
tmp.put("isDriver", currentProcess.getDriverMode());
tmp.put("driverBrightness", driverModeSettings.brightness);
tmp.put("driverExposure", driverModeSettings.exposure);
2019-11-01 17:01:10 +02:00
return tmp;
}
2019-09-22 02:49:30 -04:00
public static void sendFullSettings() {
//General settings
Map<String, Object> fullSettings = new HashMap<>();
VisionProcess currentProcess = VisionManager.getCurrentUIVisionProcess();
CameraCapture currentCamera = currentProcess.getCamera();
CVPipeline currentPipeline = currentProcess.getCurrentPipeline();
try {
// fullSettings.putAll(settingsToMap(ConfigManager.settings));
// fullSettings.putAll(pipelineToMap(currentPipeline.settings));
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());
fullSettings.put("pipeline", getOrdinalPipeline(currentPipeline.settings.getClass()));
fullSettings.put("driverMode", getOrdinalDriver());
fullSettings.put("pipelineList", VisionManager.getCurrentCameraPipelineNicknames());
fullSettings.put("resolutionList", VisionManager.getCurrentCameraResolutionList());
fullSettings.put("port", currentProcess.cameraStreamer.getStreamPort());
2019-11-23 04:05:37 -05:00
fullSettings.put("currentPipelineIndex", VisionManager.getCurrentUIVisionProcess().getCurrentPipelineIndex());
fullSettings.put("currentCameraIndex", VisionManager.getCurrentUIVisionProcessIndex());
} catch (IllegalAccessException e) {
System.err.println("No camera found!");
}
broadcastMessage(fullSettings);
}
}