From 1aa7e57aec718bce20724d0757484be23c15a125 Mon Sep 17 00:00:00 2001 From: Sriman Achanta <68172138+srimanachanta@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:35:44 -0400 Subject: [PATCH] Add HTTP request for ATFL upload --- .../photonvision/server/RequestHandler.java | 42 +++++++++++++++++++ .../java/org/photonvision/server/Server.java | 1 + 2 files changed, 43 insertions(+) diff --git a/photon-server/src/main/java/org/photonvision/server/RequestHandler.java b/photon-server/src/main/java/org/photonvision/server/RequestHandler.java index 14bebe2887..3f6038dfd4 100644 --- a/photon-server/src/main/java/org/photonvision/server/RequestHandler.java +++ b/photon-server/src/main/java/org/photonvision/server/RequestHandler.java @@ -246,6 +246,48 @@ public static void onNetworkConfigRequest(Context ctx) { } } + public static void onAprilTagFieldLayoutRequest(Context ctx) { + var file = ctx.uploadedFile("data"); + + if (file == null) { + ctx.status(400); + ctx.result( + "No File was sent with the request. Make sure that the field layout json is sent at the key 'data'"); + logger.error( + "No File was sent with the request. Make sure that the field layout json is sent at the key 'data'"); + return; + } + + if (!file.getExtension().contains("json")) { + ctx.status(400); + ctx.result( + "The uploaded file was not of type 'json'. The uploaded file should be a .json file."); + logger.error( + "The uploaded file was not of type 'json'. The uploaded file should be a .json file."); + return; + } + + // Create a temp file + var tempFilePath = handleTempFileCreation(file); + + if (tempFilePath.isEmpty()) { + ctx.status(500); + ctx.result("There was an error while creating a temporary copy of the file"); + logger.error("There was an error while creating a temporary copy of the file"); + return; + } + + if (ConfigManager.getInstance().saveUploadedAprilTagFieldLayout(tempFilePath.get().toPath())) { + ctx.status(200); + ctx.result("Successfully saved the uploaded AprilTagFieldLayout"); + logger.info("Successfully saved the uploaded AprilTagFieldLayout"); + } else { + ctx.status(500); + ctx.result("There was an error while saving the uploaded AprilTagFieldLayout"); + logger.error("There was an error while saving the uploaded AprilTagFieldLayout"); + } + } + public static void onOfflineUpdateRequest(Context ctx) { var file = ctx.uploadedFile("jarData"); diff --git a/photon-server/src/main/java/org/photonvision/server/Server.java b/photon-server/src/main/java/org/photonvision/server/Server.java index 7a025d2b6e..18010e6146 100644 --- a/photon-server/src/main/java/org/photonvision/server/Server.java +++ b/photon-server/src/main/java/org/photonvision/server/Server.java @@ -96,6 +96,7 @@ public static void start(int port) { app.post("/api/settings/hardwareConfig", RequestHandler::onHardwareConfigRequest); app.post("/api/settings/hardwareSettings", RequestHandler::onHardwareSettingsRequest); app.post("/api/settings/networkConfig", RequestHandler::onNetworkConfigRequest); + app.post("/api/settings/aprilTagFieldLayout", RequestHandler::onAprilTagFieldLayoutRequest); app.post("/api/settings/general", RequestHandler::onGeneralSettingsRequest); app.post("/api/settings/camera", RequestHandler::onCameraSettingsRequest); app.post("/api/settings/camera/setNickname", RequestHandler::onCameraNicknameChangeRequest);