mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-21 01:01:41 +00:00
general settings and camera settings as http requests
This commit is contained in:
@@ -2,14 +2,13 @@ package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.settings.Platform;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import com.chameleonvision.web.SocketHandler;
|
||||
import edu.wpi.cscore.*;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import edu.wpi.first.networktables.NetworkTable;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
@@ -157,7 +156,7 @@ public class Camera {
|
||||
var newHeight = camVideoMode.height / streamDivisor.value;
|
||||
cvSource = cs.putVideo(name, newWidth, newHeight);
|
||||
}
|
||||
ServerHandler.sendFullSettings();
|
||||
SocketHandler.sendFullSettings();
|
||||
}
|
||||
|
||||
public void addPipeline() {
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.vision.Orientation;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.vision.camera.Camera;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import com.chameleonvision.web.SocketHandler;
|
||||
import edu.wpi.cscore.VideoException;
|
||||
import edu.wpi.first.networktables.*;
|
||||
import org.opencv.core.*;
|
||||
@@ -76,8 +76,8 @@ public class VisionProcess implements Runnable {
|
||||
SettingsManager.GeneralSettings.currentPipeline = ntPipelineIndex;
|
||||
HashMap<String, Object> pipeChange = new HashMap<>();
|
||||
pipeChange.put("currentPipeline", ntPipelineIndex);
|
||||
ServerHandler.broadcastMessage(pipeChange);
|
||||
ServerHandler.sendFullSettings();
|
||||
SocketHandler.broadcastMessage(pipeChange);
|
||||
SocketHandler.sendFullSettings();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -245,7 +245,7 @@ public class VisionProcess implements Runnable {
|
||||
point.put("calculated", calculated);
|
||||
point.put("rawPoint", center);
|
||||
WebSend.put("point", point);
|
||||
ServerHandler.broadcastMessage(WebSend);
|
||||
SocketHandler.broadcastMessage(WebSend);
|
||||
}
|
||||
|
||||
cameraProcess.setOutputFrame(streamOutputMat);
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.chameleonvision.web;
|
||||
|
||||
import com.chameleonvision.network.NetworkIPMode;
|
||||
import com.chameleonvision.settings.GeneralSettings;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.vision.camera.CameraException;
|
||||
import com.chameleonvision.vision.camera.CameraManager;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Requesthandler {
|
||||
|
||||
public static void onGeneralSettings(Context ctx) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
Map map = objectMapper.readValue(ctx.body(), Map.class);
|
||||
SettingsManager.GeneralSettings.teamNumber = (int) map.get("teamNumber");
|
||||
SettingsManager.GeneralSettings.connectionType = NetworkIPMode.values()[(int) map.get("connectionType")];
|
||||
SettingsManager.GeneralSettings.ip = (String) map.get("ip");
|
||||
SettingsManager.GeneralSettings.netmask = (String) map.get("netmask");
|
||||
SettingsManager.GeneralSettings.gateway = (String) map.get("gateway");
|
||||
SettingsManager.GeneralSettings.hostname = (String) map.get("hostname");
|
||||
SettingsManager.saveSettings();
|
||||
SocketHandler.sendFullSettings();
|
||||
ctx.status(200);
|
||||
} catch (JsonProcessingException e) {
|
||||
ctx.status(500);
|
||||
}
|
||||
}
|
||||
|
||||
public static void onCameraSettings(Context ctx) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
Map camSettings = objectMapper.readValue(ctx.body(), Map.class);
|
||||
var curCam = CameraManager.getCurrentCamera();
|
||||
|
||||
Number newFOV = (Number) camSettings.get("fov");
|
||||
Integer newStreamDivisor = (Integer) camSettings.get("streamDivisor");
|
||||
Integer newResolution = (Integer) camSettings.get("resolution");
|
||||
|
||||
curCam.setFOV(newFOV);
|
||||
|
||||
var currentStreamDivisorOrdinal = curCam.getStreamDivisor().ordinal();
|
||||
if (currentStreamDivisorOrdinal != newStreamDivisor) {
|
||||
curCam.setStreamDivisor(newStreamDivisor, true);
|
||||
}
|
||||
|
||||
var currentResolutionIndex = curCam.getVideoModeIndex();
|
||||
if (currentResolutionIndex != newResolution) {
|
||||
curCam.setCamVideoMode(newResolution, true);
|
||||
}
|
||||
|
||||
CameraManager.saveCameras();
|
||||
SocketHandler.sendFullSettings();
|
||||
ctx.status(200);
|
||||
} catch (JsonProcessingException | CameraException e) {
|
||||
e.printStackTrace();
|
||||
ctx.status(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,30 +3,33 @@ package com.chameleonvision.web;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import io.javalin.Javalin;
|
||||
|
||||
|
||||
public class Server {
|
||||
public static ServerHandler handler;
|
||||
private static SocketHandler socketHandler;
|
||||
|
||||
public static void main(int port) {
|
||||
handler = new ServerHandler();
|
||||
socketHandler = new SocketHandler();
|
||||
|
||||
Javalin app = Javalin.create(javalinConfig -> javalinConfig.showJavalinBanner=false);
|
||||
app.config.addStaticFiles("web");
|
||||
Javalin app = Javalin.create(javalinConfig -> {
|
||||
javalinConfig.showJavalinBanner = false;
|
||||
javalinConfig.addStaticFiles("web");
|
||||
javalinConfig.enableCorsForAllOrigins();
|
||||
});
|
||||
app.ws("/websocket", ws -> {
|
||||
ws.onConnect(ctx -> {
|
||||
handler.onConnect(ctx);
|
||||
socketHandler.onConnect(ctx);
|
||||
System.out.println("Socket Connected");
|
||||
});
|
||||
ws.onClose(ctx -> {
|
||||
handler.onClose(ctx);
|
||||
socketHandler.onClose(ctx);
|
||||
System.out.println("Socket Disconnected");
|
||||
SettingsManager.saveSettings();
|
||||
});
|
||||
ws.onBinaryMessage(ctx -> {
|
||||
handler.onBinaryMessage(ctx);
|
||||
socketHandler.onBinaryMessage(ctx);
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/api/settings/general", Requesthandler::onGeneralSettings);
|
||||
app.post("/api/settings/camera", Requesthandler::onCameraSettings);
|
||||
app.start(port);
|
||||
}
|
||||
}
|
||||
@@ -22,12 +22,12 @@ import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class ServerHandler {
|
||||
public class SocketHandler {
|
||||
|
||||
private static List<WsContext> users;
|
||||
private static ObjectMapper objectMapper;
|
||||
|
||||
ServerHandler() {
|
||||
SocketHandler() {
|
||||
users = new ArrayList<>();
|
||||
objectMapper = new ObjectMapper(new MessagePackFactory());
|
||||
}
|
||||
@@ -47,14 +47,6 @@ public class ServerHandler {
|
||||
for (Map.Entry<String, Object> entry : deserialized.entrySet()) {
|
||||
try {
|
||||
switch (entry.getKey()) {
|
||||
case "generalSettings": {
|
||||
for (HashMap.Entry<String, Object> e : ((HashMap<String, Object>) entry.getValue()).entrySet()) {
|
||||
setField(SettingsManager.GeneralSettings, e.getKey(), e.getValue());
|
||||
}
|
||||
SettingsManager.saveSettings();
|
||||
sendFullSettings();
|
||||
break;
|
||||
}
|
||||
case "driverMode": {
|
||||
for (HashMap.Entry<String, Object> e : ((HashMap<String, Object>) entry.getValue()).entrySet()) {
|
||||
setField(CameraManager.getCurrentCamera(), e.getKey(), e.getValue());
|
||||
@@ -63,30 +55,6 @@ public class ServerHandler {
|
||||
CameraManager.saveCameras();
|
||||
break;
|
||||
}
|
||||
case "cameraSettings": {
|
||||
HashMap camSettings = (HashMap) entry.getValue();
|
||||
var curCam = CameraManager.getCurrentCamera();
|
||||
|
||||
Number newFOV = (Number) camSettings.get("fov");
|
||||
Integer newStreamDivisor = (Integer) camSettings.get("streamDivisor");
|
||||
Integer newResolution = (Integer) camSettings.get("resolution");
|
||||
|
||||
curCam.setFOV(newFOV);
|
||||
|
||||
var currentStreamDivisorOrdinal = curCam.getStreamDivisor().ordinal();
|
||||
if (currentStreamDivisorOrdinal != newStreamDivisor) {
|
||||
curCam.setStreamDivisor(newStreamDivisor, true);
|
||||
}
|
||||
|
||||
var currentResolutionIndex = curCam.getVideoModeIndex();
|
||||
if (currentResolutionIndex != newResolution) {
|
||||
curCam.setCamVideoMode(newResolution, true);
|
||||
}
|
||||
|
||||
CameraManager.saveCameras();
|
||||
sendFullSettings();
|
||||
break;
|
||||
}
|
||||
case "changeCameraName": {
|
||||
CameraManager.getCurrentCamera().setNickname((String) entry.getValue());
|
||||
sendFullSettings();
|
||||
@@ -183,7 +151,7 @@ public class ServerHandler {
|
||||
private void setField(Object obj, String fieldName, Object value) {
|
||||
try {
|
||||
if (obj instanceof Camera) {
|
||||
var cam = (Camera)obj;
|
||||
var cam = (Camera) obj;
|
||||
switch (fieldName) {
|
||||
case "driverBrightness":
|
||||
cam.setDriverBrightness((Integer) value);
|
||||
@@ -192,7 +160,7 @@ public class ServerHandler {
|
||||
cam.setDriverExposure((Integer) value);
|
||||
break;
|
||||
case "isDriver":
|
||||
cam.setDriverMode((boolean)value);
|
||||
cam.setDriverMode((boolean) value);
|
||||
break;
|
||||
default:
|
||||
Field field = obj.getClass().getField(fieldName);
|
||||
@@ -207,8 +175,7 @@ public class ServerHandler {
|
||||
Field field = obj.getClass().getField(fieldName);
|
||||
if (field.getType().isEnum()) {
|
||||
field.set(obj, field.getType().getEnumConstants()[(Integer) value]);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
field.set(obj, value);
|
||||
}
|
||||
}
|
||||
@@ -296,7 +263,7 @@ public class ServerHandler {
|
||||
fullSettings.put("cameraList", CameraManager.getAllCameraByNickname());
|
||||
fullSettings.put("pipeline", getOrdinalPipeline());
|
||||
var currentCamera = CameraManager.getCurrentCamera();
|
||||
fullSettings.put("driverMode",getOrdinalDriver());
|
||||
fullSettings.put("driverMode", getOrdinalDriver());
|
||||
fullSettings.put("pipelineList", currentCamera.getPipelinesNickname());
|
||||
fullSettings.put("resolutionList", currentCamera.getResolutionList());
|
||||
fullSettings.put("port", currentCamera.getStreamPort());
|
||||
Reference in New Issue
Block a user