2019-09-21 16:04:58 +03:00
|
|
|
package com.chameleonvision.web;
|
|
|
|
|
|
2019-10-04 15:55:45 -04:00
|
|
|
import com.chameleonvision.vision.camera.CameraException;
|
2019-09-21 16:04:58 +03:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
|
|
|
|
import com.chameleonvision.vision.camera.CameraManager;
|
2019-10-11 02:25:25 +03:00
|
|
|
import io.javalin.websocket.*;
|
|
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2019-10-12 03:38:42 +03:00
|
|
|
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
|
|
|
|
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;
|
2019-10-13 00:44:17 +03:00
|
|
|
|
2019-09-21 16:04:58 +03:00
|
|
|
import java.lang.reflect.Field;
|
2019-10-13 00:44:17 +03:00
|
|
|
import java.lang.reflect.Method;
|
2019-10-11 02:25:25 +03:00
|
|
|
import java.util.*;
|
2019-09-21 16:04:58 +03:00
|
|
|
|
2019-10-12 03:38:42 +03:00
|
|
|
|
2019-09-21 16:04:58 +03:00
|
|
|
public class ServerHandler {
|
|
|
|
|
|
|
|
|
|
private static List<WsContext> users;
|
|
|
|
|
|
2019-09-21 13:05:00 -04:00
|
|
|
ServerHandler() {
|
|
|
|
|
users = new ArrayList<>();
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onClose(WsCloseContext context) {
|
|
|
|
|
users.remove(context);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 03:38:42 +03:00
|
|
|
void onBinaryMessage(WsBinaryMessageContext data) throws Exception {
|
|
|
|
|
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ArrayUtils.toPrimitive(data.data()));
|
|
|
|
|
int length = unpacker.unpackMapHeader();
|
|
|
|
|
for (int mapIndex = 0; mapIndex < length; mapIndex++) {
|
|
|
|
|
String key = unpacker.unpackString(); // key
|
|
|
|
|
Object value = get(unpacker.unpackValue());
|
2019-10-13 00:44:17 +03:00
|
|
|
runMethod(ApplyFields.class, "set" + key, value);
|
2019-10-12 03:38:42 +03:00
|
|
|
}
|
2019-10-13 00:44:17 +03:00
|
|
|
System.out.println(ReflectionToStringBuilder.toString(CameraManager.getCurrentPipeline()));
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-13 00:44:17 +03:00
|
|
|
private void runMethod(Class clazz, String funcName, Object param) {
|
2019-09-21 16:04:58 +03:00
|
|
|
try {
|
2019-10-13 00:44:17 +03:00
|
|
|
Method[] methods = clazz.getDeclaredMethods();
|
|
|
|
|
for (Method method : methods) {
|
|
|
|
|
if (funcName.equalsIgnoreCase(method.getName())) {
|
|
|
|
|
method.invoke(null, param);
|
|
|
|
|
return;
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-12 03:38:42 +03:00
|
|
|
} catch (Exception e) {
|
2019-10-13 00:44:17 +03:00
|
|
|
System.out.println("Error while trying to call a function");
|
2019-09-21 16:04:58 +03:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 00:44:17 +03:00
|
|
|
|
|
|
|
|
// 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<Value> valLst = ((ImmutableArrayValue) value).list();
|
|
|
|
|
// List<Object> 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<Object> getList(ImmutableArrayValue value) {
|
|
|
|
|
// List<Value> valLst = value.list();
|
|
|
|
|
// List<Object> lst = new ArrayList<>();
|
|
|
|
|
// for (Value val : valLst) {
|
|
|
|
|
// try {
|
|
|
|
|
// lst.add(get((ImmutableValue) val));
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return lst;
|
|
|
|
|
// }
|
|
|
|
|
|
2019-10-12 03:38:42 +03:00
|
|
|
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");
|
|
|
|
|
}
|
2019-09-21 16:04:58 +03:00
|
|
|
|
2019-10-13 00:44:17 +03:00
|
|
|
public List<Float> getFloatList(ImmutableArrayValue arrayValue) {
|
2019-10-12 03:38:42 +03:00
|
|
|
List<Float> output = new ArrayList<>();
|
|
|
|
|
List<Value> values = arrayValue.list();
|
2019-10-13 00:44:17 +03:00
|
|
|
for (Value v : values) {
|
2019-10-12 03:38:42 +03:00
|
|
|
if (v.isFloatValue())
|
|
|
|
|
output.add(v.asFloatValue().toFloat());
|
|
|
|
|
else
|
|
|
|
|
output.add((float) v.asIntegerValue().toInt());
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
2019-10-12 03:38:42 +03:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 00:44:17 +03:00
|
|
|
public List<Integer > getIntList(ImmutableArrayValue arrayValue) {
|
|
|
|
|
List<Integer> output = new ArrayList<>();
|
|
|
|
|
List<Value> values = arrayValue.list();
|
|
|
|
|
for (Value v : values) {
|
|
|
|
|
output.add((int) v.asIntegerValue().toInt());
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 03:38:42 +03:00
|
|
|
private static void broadcastMessage(Object obj, WsContext userToSkip) {
|
|
|
|
|
if (users != null)
|
|
|
|
|
for (var user : users) {
|
|
|
|
|
if (userToSkip != null && user.getSessionId().equals(userToSkip.getSessionId())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
user.send(obj);
|
|
|
|
|
}
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-11 02:25:25 +03:00
|
|
|
public static void broadcastMessage(Object obj) {//TODO fix sending for msgpack
|
|
|
|
|
broadcastMessage(obj, null);//Broadcasts the message to every user
|
2019-09-21 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-13 00:44:17 +03:00
|
|
|
// 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;
|
|
|
|
|
// }
|
2019-09-21 16:04:58 +03:00
|
|
|
|
2019-10-13 00:44:17 +03:00
|
|
|
public static Map<String, Object> allFieldsToMap(Object obj) {
|
2019-09-21 16:04:58 +03:00
|
|
|
Map map = new HashMap<String, Object>();
|
|
|
|
|
try {
|
|
|
|
|
Field[] fields = obj.getClass().getFields();
|
|
|
|
|
for (Field field : fields) {
|
|
|
|
|
map.put(field.getName(), field.get(obj));
|
|
|
|
|
}
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
System.err.println("Illegal Access error:" + e.getStackTrace());
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-22 02:49:30 -04:00
|
|
|
public static void sendFullSettings() {
|
2019-09-21 16:04:58 +03:00
|
|
|
//General settings
|
|
|
|
|
Map<String, Object> fullSettings = new HashMap<>(allFieldsToMap(SettingsManager.GeneralSettings));
|
|
|
|
|
fullSettings.put("cameraList", CameraManager.getAllCamerasByName().keySet());
|
|
|
|
|
try {
|
|
|
|
|
var currentCamera = CameraManager.getCurrentCamera();
|
|
|
|
|
fullSettings.putAll(allFieldsToMap(currentCamera.getCurrentPipeline()));
|
|
|
|
|
fullSettings.put("pipelineList", currentCamera.getPipelines().keySet());
|
|
|
|
|
fullSettings.put("resolutionList", CameraManager.getResolutionList());
|
|
|
|
|
fullSettings.put("resolution", currentCamera.getVideoModeIndex());
|
|
|
|
|
fullSettings.put("FOV", currentCamera.getFOV());
|
2019-09-22 02:49:30 -04:00
|
|
|
fullSettings.put("port", currentCamera.getStreamPort());
|
2019-09-21 16:04:58 +03:00
|
|
|
} catch (CameraException e) {
|
|
|
|
|
System.err.println("No camera found!");
|
|
|
|
|
//TODO: add message to ui to inform that there are no cameras
|
|
|
|
|
}
|
|
|
|
|
broadcastMessage(fullSettings);
|
|
|
|
|
}
|
|
|
|
|
}
|