mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-22 01:11:40 +00:00
Installupdate (#53)
Added auto service install and jar file upgrade from the ui
This commit is contained in:
@@ -1,13 +1,34 @@
|
||||
package com.chameleonvision.util;
|
||||
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import io.javalin.http.UploadedFile;
|
||||
import org.opencv.core.Scalar;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Helpers {
|
||||
private static final String kServicePath = "/etc/systemd/system/chameleonVision.service";
|
||||
private static final String kServiceString = "[Unit]\n" +
|
||||
"Description=chameleon vision\n" +
|
||||
"\n" +
|
||||
"[Service]\n" +
|
||||
"ExecStart=/usr/bin/java -jar %s \n" +
|
||||
"StandardOutput=file:/var/log/something.out.txt\n" +
|
||||
"StandardError=file:/var/log/something.err.txt\n" +
|
||||
"Type=simple\n" +
|
||||
"WorkingDirectory=/usr/local/bin\n" +
|
||||
"\n" +
|
||||
"[Install]\n" +
|
||||
"WantedBy=multi-user.target\n" +
|
||||
"\n";
|
||||
|
||||
private Helpers() {
|
||||
}
|
||||
|
||||
@@ -17,9 +38,19 @@ public class Helpers {
|
||||
|
||||
public static HashMap VideoModeToHashMap(VideoMode videoMode) {
|
||||
return new HashMap<String, Object>() {{
|
||||
put("width", videoMode.width);
|
||||
put("height", videoMode.height);
|
||||
put("fps", videoMode.fps);
|
||||
put("pixelFormat", videoMode.pixelFormat.toString());}};
|
||||
put("width", videoMode.width);
|
||||
put("height", videoMode.height);
|
||||
put("fps", videoMode.fps);
|
||||
put("pixelFormat", videoMode.pixelFormat.toString());
|
||||
}};
|
||||
}
|
||||
|
||||
public static void setService(Path filePath) throws IOException, InterruptedException {
|
||||
String newService = String.format(kServiceString, filePath.toString());
|
||||
Writer writer = new FileWriter(kServicePath, false);
|
||||
writer.write(newService);
|
||||
writer.close();
|
||||
Process p = Runtime.getRuntime().exec("systemctl enable chameleonVision.service");
|
||||
p.waitFor();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.chameleonvision.web;
|
||||
|
||||
import com.chameleonvision.Exceptions.DuplicatedKeyException;
|
||||
import com.chameleonvision.Main;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import com.chameleonvision.network.NetworkIPMode;
|
||||
import com.chameleonvision.networktables.NetworkTablesManager;
|
||||
import com.chameleonvision.util.Helpers;
|
||||
import com.chameleonvision.util.Platform;
|
||||
import com.chameleonvision.util.ProgramDirectoryUtilities;
|
||||
import com.chameleonvision.vision.VisionManager;
|
||||
import com.chameleonvision.vision.VisionProcess;
|
||||
import com.chameleonvision.vision.camera.USBCameraCapture;
|
||||
@@ -17,13 +21,19 @@ import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import edu.wpi.first.wpilibj.geometry.Rotation2d;
|
||||
import io.javalin.core.util.FileUtil;
|
||||
import io.javalin.http.Context;
|
||||
import io.javalin.http.Handler;
|
||||
import io.javalin.http.UploadedFile;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.math3.ml.neuralnet.Network;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Point3;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -209,4 +219,31 @@ public class RequestHandler {
|
||||
ctx.status(500);
|
||||
}
|
||||
}
|
||||
|
||||
public static void onInstallOrUpdate(Context ctx) {
|
||||
Platform p = Platform.getCurrentPlatform();
|
||||
try {
|
||||
if (p == Platform.LINUX_RASPBIAN || p == Platform.LINUX_64) {
|
||||
UploadedFile file = ctx.uploadedFile("file");
|
||||
Path filePath;
|
||||
if (file != null) {
|
||||
filePath = Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), file.getFilename());
|
||||
File target = new File(filePath.toString());
|
||||
OutputStream stream = new FileOutputStream(target);
|
||||
file.getContent().transferTo(stream);
|
||||
stream.close();
|
||||
} else {
|
||||
filePath = Paths.get(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath()); // quirk to get the current file directory
|
||||
}
|
||||
Helpers.setService(filePath);
|
||||
ctx.status(200);
|
||||
} else {
|
||||
ctx.result("Only Linux Platforms Support this feature");
|
||||
ctx.status(500);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ctx.result(e.toString());
|
||||
ctx.status(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ public class Server {
|
||||
app.post("/api/settings/snapshot", RequestHandler::onSnapshot);
|
||||
app.post("/api/settings/endCalibration", RequestHandler::onCalibrationEnding);
|
||||
app.post("/api/vision/pnpModel", RequestHandler::onPnpModel);
|
||||
app.post("/api/install", RequestHandler::onInstallOrUpdate);
|
||||
app.start(port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,8 +53,8 @@ public class SocketHandler {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
void onBinaryMessage(WsBinaryMessageContext context) throws Exception {
|
||||
Map<String, Object> deserialized = objectMapper.readValue(ArrayUtils.toPrimitive(context.data()), new TypeReference<>() {
|
||||
});
|
||||
Map<String, Object> deserialized = objectMapper.readValue((byte[]) ArrayUtils.toPrimitive(context.data()),
|
||||
new TypeReference<>(){});
|
||||
for (Map.Entry<String, Object> entry : deserialized.entrySet()) {
|
||||
try {
|
||||
VisionProcess currentProcess = VisionManager.getCurrentUIVisionProcess();
|
||||
@@ -266,11 +266,11 @@ public class SocketHandler {
|
||||
tmp.put("streamDivisor", currentVisionProcess.cameraStreamer.getDivisor().ordinal());
|
||||
tmp.put("resolution", currentVisionProcess.getCamera().getProperties().getCurrentVideoModeIndex());
|
||||
tmp.put("tilt", currentVisionProcess.getCamera().getProperties().getTilt().getDegrees());
|
||||
|
||||
|
||||
List<CameraCalibrationConfig.UICameraCalibrationConfig> calibrations = currentCamera.getAllCalibrationData().stream()
|
||||
.map(CameraCalibrationConfig.UICameraCalibrationConfig::new).collect(Collectors.toList());
|
||||
tmp.put("calibration", calibrations);
|
||||
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user