mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Buffer setting save (#51)
This makes it so that settings are written to disk 1 second after settings have stopped being changed. This means that while dragging sliders in the UI, settings won't be written repeatedly. This reduces wear on system storage and improves performance, as saving is expensive.
This commit is contained in:
@@ -46,8 +46,7 @@ public class Main {
|
||||
public static final int DEFAULT_WEBPORT = 5800;
|
||||
|
||||
private static final Logger logger = new Logger(Main.class, LogGroup.General);
|
||||
private static final boolean isRelease =
|
||||
!PhotonVersion.isRelease; // Hack!!!! Until PhotonVersion script fixed
|
||||
private static final boolean isRelease = PhotonVersion.isRelease;
|
||||
|
||||
private static boolean isTestMode;
|
||||
private static boolean printDebugLogs;
|
||||
@@ -146,6 +145,7 @@ public class Main {
|
||||
logger.error("Failed to parse command-line options!", e);
|
||||
}
|
||||
|
||||
System.out.println("Running in " + (isRelease ? "release" : "development") + " mode!");
|
||||
var logLevel = (isRelease || printDebugLogs) ? LogLevel.INFO : LogLevel.DEBUG;
|
||||
Logger.setLevel(LogGroup.Camera, logLevel);
|
||||
Logger.setLevel(LogGroup.WebServer, logLevel);
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import org.photonvision.common.logging.LogGroup;
|
||||
import org.photonvision.common.logging.Logger;
|
||||
import org.photonvision.common.util.TimedTaskManager;
|
||||
import org.photonvision.common.util.file.FileUtils;
|
||||
import org.photonvision.common.util.file.JacksonUtils;
|
||||
import org.photonvision.vision.pipeline.CVPipelineSettings;
|
||||
@@ -46,6 +47,8 @@ public class ConfigManager {
|
||||
|
||||
final File configDirectoryFile;
|
||||
|
||||
private long saveRequestTimestamp = -1;
|
||||
|
||||
public static ConfigManager getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new ConfigManager(getRootFolder());
|
||||
@@ -83,6 +86,8 @@ public class ConfigManager {
|
||||
new File(Path.of(configDirectoryFile.toString(), "networkSettings.json").toUri());
|
||||
this.camerasFolder = new File(Path.of(configDirectoryFile.toString(), "cameras").toUri());
|
||||
|
||||
TimedTaskManager.getInstance().addTask("ConfigManager", this::checkSaveAndWrite, 1000);
|
||||
|
||||
load();
|
||||
}
|
||||
|
||||
@@ -155,7 +160,7 @@ public class ConfigManager {
|
||||
this.config = new PhotonConfiguration(hardwareConfig, networkConfig, cameraConfigurations);
|
||||
}
|
||||
|
||||
public void save() {
|
||||
public void saveToDisk() {
|
||||
logger.info("Saving settings...");
|
||||
|
||||
try {
|
||||
@@ -305,12 +310,12 @@ public class ConfigManager {
|
||||
.map(it -> it.getSettables().getConfiguration())
|
||||
.collect(Collectors.toList());
|
||||
getConfig().addCameraConfigs(list);
|
||||
save();
|
||||
requestSave();
|
||||
}
|
||||
|
||||
public void saveModule(CameraConfiguration config, String uniqueName) {
|
||||
getConfig().addCameraConfig(uniqueName, config);
|
||||
save();
|
||||
requestSave();
|
||||
}
|
||||
|
||||
public File getSettingsFolderAsZip() {
|
||||
@@ -325,7 +330,7 @@ public class ConfigManager {
|
||||
|
||||
public void setNetworkSettings(NetworkConfig networkConfig) {
|
||||
getConfig().setNetworkConfig(networkConfig);
|
||||
save();
|
||||
requestSave();
|
||||
}
|
||||
|
||||
public Path getLogPath() {
|
||||
@@ -336,4 +341,18 @@ public class ConfigManager {
|
||||
if (!logFile.getParentFile().exists()) logFile.getParentFile().mkdirs();
|
||||
return logFile.toPath();
|
||||
}
|
||||
|
||||
public void requestSave() {
|
||||
logger.debug("Requesting save...");
|
||||
saveRequestTimestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private void checkSaveAndWrite() {
|
||||
// Only save if 1 second has past since the request was made
|
||||
if (saveRequestTimestamp > 0 && (System.currentTimeMillis() - saveRequestTimestamp) > 1000L) {
|
||||
saveRequestTimestamp = -1;
|
||||
logger.debug("Saving to disk...");
|
||||
saveToDisk();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ public class Logger {
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
},
|
||||
30000L);
|
||||
3000L);
|
||||
} catch (FileNotFoundException e) {
|
||||
out = null;
|
||||
System.err.println("Unable to log to file " + logFilePath.toString());
|
||||
|
||||
@@ -75,7 +75,7 @@ public class RequestHandler {
|
||||
|
||||
var networkConfig = NetworkConfig.fromHashMap(networking);
|
||||
ConfigManager.getInstance().setNetworkSettings(networkConfig);
|
||||
ConfigManager.getInstance().save();
|
||||
ConfigManager.getInstance().requestSave();
|
||||
NetworkManager.getInstance().reinitialize();
|
||||
NetworkTablesManager.setClientMode(null); // TODO
|
||||
|
||||
|
||||
@@ -318,12 +318,6 @@ public class VisionModule {
|
||||
config.driveModeSettings = pipelineManager.driverModePipeline.getSettings();
|
||||
config.currentPipelineIndex = Math.max(pipelineManager.getCurrentPipelineIndex(), -1);
|
||||
|
||||
logger.info(
|
||||
"Saving state with "
|
||||
+ config.calibrations.size()
|
||||
+ " calibrated resolutions and index "
|
||||
+ config.currentPipelineIndex);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user