Offline Update (.jar replace) (#340)

Allows users to upload a new JAR to a Pi. Also bumps the pi image to increase the heap size.
This commit is contained in:
Chris Gerth
2021-12-03 22:08:51 -06:00
committed by GitHub
parent dbd631da61
commit c944967476
10 changed files with 203 additions and 16 deletions

View File

@@ -22,8 +22,11 @@ import edu.wpi.first.math.geometry.Rotation2d;
import io.javalin.http.Context;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
@@ -37,6 +40,7 @@ import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.common.networking.NetworkManager;
import org.photonvision.common.util.ShellExec;
import org.photonvision.common.util.file.ProgramDirectoryUtilities;
import org.photonvision.vision.processes.VisionModuleManager;
import org.photonvision.vision.target.TargetModel;
@@ -103,6 +107,49 @@ public class RequestHandler {
}
}
public static void onOfflineUpdate(Context ctx) {
logger.info("Handling offline update .jar upload...");
var file = ctx.uploadedFile("jarData");
logger.info("New .jar uploaded successfully.");
if (file != null) {
if (Platform.isRaspberryPi()) {
try {
Path filePath =
Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "photonvision.jar");
File targetFile = new File(filePath.toString());
var stream = new FileOutputStream(targetFile);
logger.info(
"Streaming user-provided " + file.getFilename() + " into " + targetFile.toString());
file.getContent().transferTo(stream);
stream.close();
ctx.status(200);
logger.info("New .jar in place, going down for restart...");
restartProgram(ctx);
} catch (FileNotFoundException e) {
logger.error(
".jar of this program could not be found. How the heck this program started in the first place is a mystery.");
ctx.status(500);
} catch (IOException e) {
logger.error("Could not overwrite the .jar for this instance of photonvision.");
ctx.status(500);
}
} else {
logger.error("Hot .jar replace currently only supported on Raspberry pi. Ignoring.");
ctx.status(500);
}
} else {
logger.error("Couldn't read provided file for new .jar! Ignoring.");
ctx.status(500);
}
}
@SuppressWarnings("unchecked")
public static void onGeneralSettings(Context context) throws JsonProcessingException {
Map<String, Object> map =