From 34eda17e974a795273d7a9260be53289411a9167 Mon Sep 17 00:00:00 2001 From: Omer Date: Sun, 13 Oct 2019 00:44:17 +0300 Subject: [PATCH] Simplfied serverhandler and moved alot of the logic to ApplyFields --- .../com/chameleonvision/web/ApplyFields.java | 174 ++++++++++++++++ .../chameleonvision/web/ServerHandler.java | 185 +++++++----------- 2 files changed, 242 insertions(+), 117 deletions(-) create mode 100644 Main/src/main/java/com/chameleonvision/web/ApplyFields.java diff --git a/Main/src/main/java/com/chameleonvision/web/ApplyFields.java b/Main/src/main/java/com/chameleonvision/web/ApplyFields.java new file mode 100644 index 000000000..e18463d2a --- /dev/null +++ b/Main/src/main/java/com/chameleonvision/web/ApplyFields.java @@ -0,0 +1,174 @@ +package com.chameleonvision.web; + +import com.chameleonvision.vision.Orientation; +import com.chameleonvision.vision.SortMode; +import com.chameleonvision.vision.TargetGroup; +import com.chameleonvision.vision.TargetIntersection; +import com.chameleonvision.vision.camera.CameraException; +import com.chameleonvision.vision.camera.CameraManager; +import edu.wpi.cscore.VideoException; +import org.msgpack.value.ImmutableArrayValue; + +import static com.chameleonvision.web.Server.handler; + +public class ApplyFields { + public static void setExposure(Object value) { + int newExposure = (int) value; + System.out.printf("Changing exposure to %d\n", newExposure); + try { + CameraManager.getCurrentCamera().setExposure(newExposure); + } catch (VideoException | CameraException e) { + System.out.println("Exposure changes is not supported on your webcam/webcam's driver"); + } + } + + public static void setBrightness(Object value) { + int newBrightness = (int) value; + System.out.printf("Changing brightness to %d\n", newBrightness); + try { + CameraManager.getCurrentCamera().setBrightness(newBrightness); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setOrientation(Object value) { + try { + CameraManager.getCurrentPipeline().orientation = Orientation.values()[(int) value]; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setHue(Object value) { + try { + CameraManager.getCurrentPipeline().hue = handler.getIntList((ImmutableArrayValue) value); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setSaturation(Object value) { + try { + CameraManager.getCurrentPipeline().saturation = handler.getIntList((ImmutableArrayValue) value); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setValue(Object value) { + try { + CameraManager.getCurrentPipeline().value = handler.getIntList((ImmutableArrayValue) value); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setErode(Object value) { + try { + CameraManager.getCurrentPipeline().erode = (boolean) value; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setDilate(Object value) { + try { + CameraManager.getCurrentPipeline().dilate = (boolean) value; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setArea(Object value) { + try { + CameraManager.getCurrentPipeline().area = handler.getFloatList((ImmutableArrayValue) value); + ; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setRatio(Object value) { + try { + CameraManager.getCurrentPipeline().ratio = handler.getFloatList((ImmutableArrayValue) value); + ; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setExtent(Object value) { + try { + CameraManager.getCurrentPipeline().extent = handler.getIntList((ImmutableArrayValue) value); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setTargetGroup(Object value) { + try { + CameraManager.getCurrentPipeline().targetGroup = TargetGroup.values()[(int) value]; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setTargetIntersection(Object value) { + try { + CameraManager.getCurrentPipeline().targetIntersection = TargetIntersection.values()[(int) value]; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setSortMode(Object value) { + try { + CameraManager.getCurrentPipeline().sortMode = SortMode.values()[(int) value]; + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setCurrCamera(Object value) { + String newCamera = (String) value; + System.out.printf("Changing camera to %s\n", newCamera); + try { + CameraManager.setCurrentCamera(newCamera); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setCurrPipeline(Object value) { + int newPipeline = (int) value; + System.out.printf("Changing pipeline to %s\n", newPipeline); + try { + CameraManager.setCurrentPipeline(newPipeline); + CameraManager.getCurrentCameraProcess().ntPipelineEntry.setNumber(newPipeline); + ServerHandler.broadcastMessage(ServerHandler.allFieldsToMap(CameraManager.getCurrentPipeline())); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setResolution(Object value) { + int newVideoMode = (int) value; + System.out.printf("Changing video mode to %d\n", newVideoMode); + try { + CameraManager.getCurrentCamera().setCamVideoMode(newVideoMode, true); + } catch (CameraException e) { + e.printStackTrace(); + } + } + + public static void setFOV(Object value) { + float newFov = ((Integer) value) * 1F;//TODO check this + System.out.printf("Changing FOV to %f\n", newFov); + try { + CameraManager.getCurrentCamera().setFOV(newFov); + } catch (CameraException e) { + e.printStackTrace(); + } + } +} diff --git a/Main/src/main/java/com/chameleonvision/web/ServerHandler.java b/Main/src/main/java/com/chameleonvision/web/ServerHandler.java index 550681f06..584ba50ea 100644 --- a/Main/src/main/java/com/chameleonvision/web/ServerHandler.java +++ b/Main/src/main/java/com/chameleonvision/web/ServerHandler.java @@ -1,24 +1,19 @@ package com.chameleonvision.web; -import com.chameleonvision.vision.Orientation; -import com.chameleonvision.vision.SortMode; -import com.chameleonvision.vision.TargetGroup; -import com.chameleonvision.vision.TargetIntersection; import com.chameleonvision.vision.camera.CameraException; import com.chameleonvision.settings.SettingsManager; import com.chameleonvision.vision.camera.CameraManager; -import edu.wpi.cscore.VideoException; import io.javalin.websocket.*; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; import org.msgpack.core.MessagePack; import org.msgpack.core.MessageUnpacker; import org.msgpack.value.ImmutableArrayValue; import org.msgpack.value.ImmutableValue; import org.msgpack.value.Value; -import org.springframework.beans.BeanUtils; + import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.*; @@ -45,116 +40,63 @@ public class ServerHandler { for (int mapIndex = 0; mapIndex < length; mapIndex++) { String key = unpacker.unpackString(); // key Object value = get(unpacker.unpackValue()); - try { - if (hasField(CameraManager.getCurrentPipeline(), key)) { - //Special cases for exposure and brightness and aspect ratio - switch (key) { - case "exposure": - int newExposure = (int) value; - System.out.printf("Changing exposure to %d\n", newExposure); - try { - ; - CameraManager.getCurrentCamera().setExposure(newExposure); - } catch (VideoException e) { - System.out.println("Exposure changes is not supported on your webcam/webcam's driver"); - } - break; - case "brightness": - int newBrightness = (int) value; - System.out.printf("Changing brightness to %d\n", newBrightness); - CameraManager.getCurrentCamera().setBrightness(newBrightness); - break; - case "ratio": - CameraManager.getCurrentPipeline().ratio=getFloatList((ImmutableArrayValue) value); - break; - case "area": - CameraManager.getCurrentPipeline().area=getFloatList((ImmutableArrayValue) value); - break; - //Enums - case "targetIntersection": - setField(CameraManager.getCurrentPipeline(), key, TargetIntersection.values()[(int) value]); - break; - case "targetGroup": - setField(CameraManager.getCurrentPipeline(), key, TargetGroup.values()[(int) value]); - break; - case "sortMode": - setField(CameraManager.getCurrentPipeline(), key, SortMode.values()[(int) value]); - break; - case "orientation": - setField(CameraManager.getCurrentPipeline(), key, Orientation.values()[(int) value]); - break; - default: - //Any other field in CameraManager that doesn't need anything special - setField(CameraManager.getCurrentPipeline(), key, value); - break; - } - } else { - switch (key) { - case "change_general_settings_values": - Map map = (Map) value; - map.forEach((s, o) -> setField(SettingsManager.GeneralSettings, s, o)); - SettingsManager.saveSettings(); - break; - case "curr_camera": - String newCamera = (String) value; - System.out.printf("Changing camera to %s\n", newCamera); - CameraManager.setCurrentCamera(newCamera); - HashMap portMap = new HashMap<>(); - portMap.put("port", CameraManager.getCurrentCamera().getStreamPort()); - sendFullSettings(); - break; - case "curr_pipeline": - int newPipeline = (int) value; - System.out.printf("Changing pipeline to %s\n", newPipeline); - CameraManager.setCurrentPipeline(newPipeline); - CameraManager.getCurrentCameraProcess().ntPipelineEntry.setNumber(newPipeline); - broadcastMessage(allFieldsToMap(CameraManager.getCurrentPipeline())); - break; - case "resolution": - int newVideoMode = (int) value; - System.out.printf("Changing video mode to %d\n", newVideoMode); - CameraManager.getCurrentCamera().setCamVideoMode(newVideoMode, true); - break; - case "FOV": - float newFov = ((Integer) value) * 1F;//TODO check this - System.out.printf("Changing FOV to %f\n", newFov); - CameraManager.getCurrentCamera().setFOV(newFov); - break; - default: - System.out.printf("Unexpected value from websocket: [%s, %s]\n", key, value); - break; - } - } - } catch (CameraException e) { - System.err.println("Camera error"); - e.printStackTrace(); - } + runMethod(ApplyFields.class, "set" + key, value); } + System.out.println(ReflectionToStringBuilder.toString(CameraManager.getCurrentPipeline())); } - private void setField(Object obj, String fieldName, Object value) { + private void runMethod(Class clazz, String funcName, Object param) { try { - Field[] fields = obj.getClass().getFields(); - for (Field f : fields) { - if (f.getName().equals(fieldName)) { - if (BeanUtils.isSimpleValueType(f.getType())) { - f.set(obj, value); - } else if (value instanceof ImmutableArrayValue) { - List valLst = ((ImmutableArrayValue) value).list(); - List lst = new ArrayList<>(); - for (Value val : valLst) { - lst.add(get((ImmutableValue) val)); - } - f.set(obj, lst); - } + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + if (funcName.equalsIgnoreCase(method.getName())) { + method.invoke(null, param); + return; } } } catch (Exception e) { - System.out.println("Exception setting field "); + System.out.println("Error while trying to call a function"); e.printStackTrace(); } } + +// private void setField(Object obj, String fieldName, Object value) { +// try { +// Field[] fields = obj.getClass().getFields(); +// for (Field f : fields) { +// if (f.getName().equals(fieldName)) { +// if (BeanUtils.isSimpleValueType(f.getType())) { +// f.set(obj, value); +// } else if (value instanceof ImmutableArrayValue) { +// List valLst = ((ImmutableArrayValue) value).list(); +// List lst = new ArrayList<>(); +// for (Value val : valLst) { +// lst.add(get((ImmutableValue) val)); +// } +// f.set(obj, lst); +// } +// } +// } +// } catch (Exception e) { +// System.out.println("Exception setting field "); +// e.printStackTrace(); +// } +// } +// +// public List getList(ImmutableArrayValue value) { +// List valLst = value.list(); +// List lst = new ArrayList<>(); +// for (Value val : valLst) { +// try { +// lst.add(get((ImmutableValue) val)); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// return lst; +// } + private Object get(ImmutableValue v) throws Exception { //TODO find a smarter way to write this if (v.isIntegerValue()) @@ -175,10 +117,10 @@ public class ServerHandler { throw new Exception("Value not recognized"); } - private List getFloatList(ImmutableArrayValue arrayValue) { + public List getFloatList(ImmutableArrayValue arrayValue) { List output = new ArrayList<>(); List values = arrayValue.list(); - for (Value v:values) { + for (Value v : values) { if (v.isFloatValue()) output.add(v.asFloatValue().toFloat()); else @@ -187,6 +129,15 @@ public class ServerHandler { return output; } + public List getIntList(ImmutableArrayValue arrayValue) { + List output = new ArrayList<>(); + List values = arrayValue.list(); + for (Value v : values) { + output.add((int) v.asIntegerValue().toInt()); + } + return output; + } + private static void broadcastMessage(Object obj, WsContext userToSkip) { if (users != null) for (var user : users) { @@ -202,16 +153,16 @@ public class ServerHandler { } - private boolean hasField(Object obj, String fieldName) { - Field[] fields = obj.getClass().getFields(); - for (Field field : fields) { - if (fieldName.equals(field.getName())) - return true; - } - return false; - } +// private boolean hasField(Object obj, String fieldName) { +// Field[] fields = obj.getClass().getFields(); +// for (Field field : fields) { +// if (fieldName.equals(field.getName())) +// return true; +// } +// return false; +// } - private static Map allFieldsToMap(Object obj) { + public static Map allFieldsToMap(Object obj) { Map map = new HashMap(); try { Field[] fields = obj.getClass().getFields();