mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Simplified ServerHandler, move ApplyFields to set value function
This commit is contained in:
@@ -11,6 +11,7 @@ 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.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import org.msgpack.core.MessagePack;
|
||||
import org.msgpack.core.MessageUnpacker;
|
||||
@@ -18,6 +19,7 @@ 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.util.*;
|
||||
|
||||
@@ -44,147 +46,100 @@ public class ServerHandler {
|
||||
int length = unpacker.unpackMapHeader();
|
||||
for (int mapIndex = 0; mapIndex < length; mapIndex++) {
|
||||
String key = unpacker.unpackString(); // key
|
||||
Object value = get(unpacker.unpackValue());
|
||||
// Object value = get(unpacker.unpackValue());
|
||||
ImmutableValue value = 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);
|
||||
switch (key) {
|
||||
case "generalSettings": {
|
||||
//change general settings using a general settings object
|
||||
break;
|
||||
}
|
||||
case "cameraSettings": {
|
||||
//change camera settings using a camera settings object
|
||||
break;
|
||||
}
|
||||
case "command": {
|
||||
// used to define all incoming commands
|
||||
break;
|
||||
}
|
||||
case "currentCamera": {
|
||||
//camera name by string
|
||||
break;
|
||||
}
|
||||
case "currentPipeline": {
|
||||
// camera pipeline by index
|
||||
break;
|
||||
}
|
||||
default: {//Change pipeline values
|
||||
//Two special cases for exposure and brightness changes
|
||||
if (key.equals("exposure"))
|
||||
try {
|
||||
;
|
||||
CameraManager.getCurrentCamera().setExposure(newExposure);
|
||||
CameraManager.getCurrentCamera().setExposure(value.asIntegerValue().toInt());
|
||||
} 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<String, Object> map = (Map<String, Object>) 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<String, Integer> 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;
|
||||
else if (key.equals("brightness")) try {
|
||||
CameraManager.getCurrentCamera().setBrightness(value.asIntegerValue().toInt());
|
||||
} catch (VideoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
else
|
||||
setValue(CameraManager.getCurrentPipeline(), key, value);//All of the other assignments fields
|
||||
|
||||
System.out.println(ToStringBuilder.reflectionToString(CameraManager.getCurrentPipeline()));//Print all pipeline data for debugging
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (CameraException e) {
|
||||
System.err.println("Camera error");
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
unexpectedData(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void setField(Object obj, String fieldName, Object value) {
|
||||
private void setValue(Object obj, String fieldName, ImmutableValue value) {
|
||||
try {
|
||||
boolean found = false;
|
||||
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) {
|
||||
found = true;
|
||||
if (f.getType().isEnum())//Field is enum like Orientation
|
||||
f.set(obj, f.getType().getEnumConstants()[value.asIntegerValue().toInt()]);
|
||||
else if (value.isBooleanValue()) {//Field is boolean like erode
|
||||
f.set(obj, value.asBooleanValue().getBoolean());
|
||||
} else if (value.isIntegerValue()) {//Field is int like M and B
|
||||
f.set(obj, value.asIntegerValue().toInt());
|
||||
} else if (f.get(obj) instanceof List<?>) {
|
||||
List<Value> valLst = ((ImmutableArrayValue) value).list();
|
||||
List<Object> lst = new ArrayList<>();
|
||||
for (Value val : valLst) {
|
||||
lst.add(get((ImmutableValue) val));
|
||||
if (((List) f.get(obj)).get(0).getClass().equals(Float.class)) {//Field is List of Floats like area in pipeline
|
||||
List<Float> lst = new ArrayList<>();
|
||||
for (Value v : valLst) {
|
||||
lst.add(v.isFloatValue() ? v.asFloatValue().toFloat() : (float) v.asIntegerValue().toInt());//Adds float if value is float, casts value to float from int otherwise
|
||||
}
|
||||
f.set(obj, lst);
|
||||
} else if (((List) f.get(obj)).get(0).getClass().equals(Integer.class)) {//Fields is List of Integers like hue in pipeline
|
||||
List<Integer> lst = new ArrayList<>();
|
||||
for (Value v : valLst) {
|
||||
lst.add(v.asIntegerValue().toInt());
|
||||
}
|
||||
f.set(obj, lst);
|
||||
}
|
||||
f.set(obj, lst);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
unexpectedData(fieldName, value);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception setting field ");
|
||||
System.out.println("Exception setting field");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private Object get(ImmutableValue v) throws Exception {
|
||||
//TODO find a smarter way to write this
|
||||
if (v.isIntegerValue())
|
||||
return v.asIntegerValue().toInt();
|
||||
if (v.isFloatValue())
|
||||
return v.asFloatValue().toFloat();
|
||||
if (v.isArrayValue()) {
|
||||
return v.asArrayValue();
|
||||
}
|
||||
if (v.isBinaryValue())
|
||||
return v.asBinaryValue().asByteArray();
|
||||
if (v.isBooleanValue())
|
||||
return v.asBooleanValue().getBoolean();
|
||||
if (v.isMapValue())
|
||||
return v.asMapValue().map();
|
||||
if (v.isStringValue())
|
||||
return v.asStringValue().asString();
|
||||
throw new Exception("Value not recognized");
|
||||
}
|
||||
|
||||
private List<Float> getFloatList(ImmutableArrayValue arrayValue) {
|
||||
List<Float> output = new ArrayList<>();
|
||||
List<Value> values = arrayValue.list();
|
||||
for (Value v:values) {
|
||||
if (v.isFloatValue())
|
||||
output.add(v.asFloatValue().toFloat());
|
||||
else
|
||||
output.add((float) v.asIntegerValue().toInt());
|
||||
}
|
||||
return output;
|
||||
public void unexpectedData(String key, ImmutableValue v) {
|
||||
System.err.println("Unexpected key or value, key=" + key + " Value=" + v.toString());
|
||||
//TODO send a error message to the user
|
||||
//TODO in the very far future send a bug report?
|
||||
}
|
||||
|
||||
private static void broadcastMessage(Object obj, WsContext userToSkip) {
|
||||
@@ -201,16 +156,6 @@ public class ServerHandler {
|
||||
broadcastMessage(obj, null);//Broadcasts the message to every user
|
||||
}
|
||||
|
||||
|
||||
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<String, Object> allFieldsToMap(Object obj) {
|
||||
Map map = new HashMap<String, Object>();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user