Big scary buttons (#1471)

This commit is contained in:
Matt
2024-10-24 20:48:02 -07:00
committed by GitHub
parent 9b61ed156c
commit 385059c233
14 changed files with 375 additions and 24 deletions

View File

@@ -54,6 +54,7 @@ public class ConfigManager {
// special case flag to disable flushing settings to disk at shutdown. Avoids the jvm shutdown
// hook overwriting the settings we just uploaded
private boolean flushOnShutdown = true;
private boolean allowWriteTask = false;
enum ConfigSaveStrategy {
SQL,
@@ -135,6 +136,10 @@ public class ConfigManager {
}
}
public static boolean nukeConfigDirectory() {
return FileUtils.deleteDirectory(getRootFolder());
}
public static boolean saveUploadedSettingsZip(File uploadPath) {
// Unpack to /tmp/something/photonvision
var folderPath = Path.of(System.getProperty("java.io.tmpdir"), "photonvision").toFile();
@@ -142,7 +147,9 @@ public class ConfigManager {
ZipUtil.unpack(uploadPath, folderPath);
// Nuke the current settings directory
FileUtils.deleteDirectory(getRootFolder());
if (!nukeConfigDirectory()) {
return false;
}
// If there's a cameras folder in the upload, we know we need to import from the
// old style
@@ -250,7 +257,14 @@ public class ConfigManager {
return imgFilePath.toPath();
}
public Path getCalibrationImageSavePath(Size frameSize, String uniqueCameraName) {
public Path getCalibrationImageSavePath(String uniqueCameraName) {
var imgFilePath =
Path.of(configDirectoryFile.toString(), "calibration", uniqueCameraName).toFile();
if (!imgFilePath.exists()) imgFilePath.mkdirs();
return imgFilePath.toPath();
}
public Path getCalibrationImageSavePathWithRes(Size frameSize, String uniqueCameraName) {
var imgFilePath =
Path.of(
configDirectoryFile.toString(),
@@ -301,7 +315,9 @@ public class ConfigManager {
private void saveAndWriteTask() {
// Only save if 1 second has past since the request was made
while (!Thread.currentThread().isInterrupted()) {
if (saveRequestTimestamp > 0 && (System.currentTimeMillis() - saveRequestTimestamp) > 1000L) {
if (saveRequestTimestamp > 0
&& (System.currentTimeMillis() - saveRequestTimestamp) > 1000L
&& allowWriteTask) {
saveRequestTimestamp = -1;
logger.debug("Saving to disk...");
saveToDisk();
@@ -330,6 +346,11 @@ public class ConfigManager {
this.flushOnShutdown = false;
}
/** Prevent pending automatic saves */
public void setWriteTaskEnabled(boolean enabled) {
this.allowWriteTask = enabled;
}
public void onJvmExit() {
if (flushOnShutdown) {
logger.info("Force-flushing settings...");

View File

@@ -114,6 +114,16 @@ public class PhotonConfiguration {
cameraConfigurations.put(name, config);
}
/**
* Delete a camera by its unique name
*
* @param name The camera name (usually unique name)
* @return True if the camera configuration was removed
*/
public boolean removeCameraConfig(String name) {
return cameraConfigurations.remove(name) != null;
}
public Map<String, Object> toHashMap() {
Map<String, Object> map = new HashMap<>();
var settingsSubmap = new HashMap<String, Object>();

View File

@@ -349,6 +349,19 @@ public class SqlConfigProvider extends ConfigProvider {
private void saveCameras(Connection conn) {
try {
// Delete all cameras we don't need anymore
String deleteExtraCamsString =
String.format(
"DELETE FROM %s WHERE %s not in (%s)",
Tables.CAMERAS,
Columns.CAM_UNIQUE_NAME,
config.getCameraConfigurations().keySet().stream()
.map(it -> "\"" + it + "\"")
.collect(Collectors.joining(", ")));
var stmt = conn.createStatement();
stmt.executeUpdate(deleteExtraCamsString);
// Replace this camera's row with the new settings
var sqlString =
String.format(
@@ -388,6 +401,7 @@ public class SqlConfigProvider extends ConfigProvider {
statement.executeUpdate();
}
} catch (SQLException | IOException e) {
logger.error("Err saving cameras", e);
try {

View File

@@ -123,6 +123,11 @@ public class Logger {
currentAppenders.add(new FileLogAppender(logFilePath));
}
public static void closeAllLoggers() {
currentAppenders.forEach(LogAppender::shutdown);
currentAppenders.clear();
}
public static void cleanLogs(Path folderToClean) {
File[] logs = folderToClean.toFile().listFiles();
if (logs == null) return;
@@ -284,6 +289,9 @@ public class Logger {
private interface LogAppender {
void log(String message, LogLevel level);
/** Release any file or other resources currently held by the Logger */
default void shutdown() {}
}
private static class ConsoleLogAppender implements LogAppender {
@@ -343,5 +351,16 @@ public class Logger {
// Nothing to do - no stream available for writing
}
}
@Override
public void shutdown() {
try {
out.close();
out = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

View File

@@ -40,7 +40,7 @@ public class FileUtils {
private static final Set<PosixFilePermission> allReadWriteExecutePerms =
new HashSet<>(Arrays.asList(PosixFilePermission.values()));
public static void deleteDirectory(Path path) {
public static boolean deleteDirectory(Path path) {
try {
var files = Files.walk(path);
@@ -53,8 +53,11 @@ public class FileUtils {
// close the stream
files.close();
return true;
} catch (IOException e) {
logger.error("Exception deleting files in " + path + "!", e);
return false;
}
}

View File

@@ -390,7 +390,7 @@ public class VisionModule {
var ret =
pipelineManager.calibration3dPipeline.tryCalibration(
ConfigManager.getInstance()
.getCalibrationImageSavePath(
.getCalibrationImageSavePathWithRes(
pipelineManager.calibration3dPipeline.getSettings().resolution,
visionSource.getCameraConfiguration().uniqueName));
pipelineManager.setCalibrationMode(false);

View File

@@ -255,7 +255,8 @@ public class Calibrate3dPipeTest {
var cal =
calibration3dPipeline.tryCalibration(
ConfigManager.getInstance().getCalibrationImageSavePath(imgRes, "Calibration_Test"));
ConfigManager.getInstance()
.getCalibrationImageSavePathWithRes(imgRes, "Calibration_Test"));
calibration3dPipeline.finishCalibration();
// visuallyDebugDistortion(directoryListing, imgRes, cal );