Update backend to provide more useful info to frontend (#866)

This commit is contained in:
Sriman Achanta
2023-06-25 21:07:27 -04:00
committed by GitHub
parent 7593c5ed05
commit 715ef62c85
17 changed files with 858 additions and 505 deletions

View File

@@ -128,7 +128,7 @@ public class ConfigManager {
}
}
public static void saveUploadedSettingsZip(File uploadPath) {
public static boolean saveUploadedSettingsZip(File uploadPath) {
// Unpack to /tmp/something/photonvision
var folderPath = Path.of(System.getProperty("java.io.tmpdir"), "photonvision").toFile();
folderPath.mkdirs();
@@ -147,14 +147,16 @@ public class ConfigManager {
var sql = new SqlConfigProvider(getRootFolder());
sql.setConfig(loadedConfig);
sql.saveToDisk();
return sql.saveToDisk();
} else {
// new structure -- just copy and save like we used to
try {
org.apache.commons.io.FileUtils.copyDirectory(folderPath, getRootFolder().toFile());
logger.info("Copied settings successfully!");
return true;
} catch (IOException e) {
logger.error("Exception copying uploaded settings!", e);
return false;
}
}
}
@@ -241,16 +243,16 @@ public class ConfigManager {
return imgFilePath.toPath();
}
public void saveUploadedHardwareConfig(Path uploadPath) {
m_provider.saveUploadedHardwareConfig(uploadPath);
public boolean saveUploadedHardwareConfig(Path uploadPath) {
return m_provider.saveUploadedHardwareConfig(uploadPath);
}
public void saveUploadedHardwareSettings(Path uploadPath) {
m_provider.saveUploadedHardwareSettings(uploadPath);
public boolean saveUploadedHardwareSettings(Path uploadPath) {
return m_provider.saveUploadedHardwareSettings(uploadPath);
}
public void saveUploadedNetworkConfig(Path uploadPath) {
m_provider.saveUploadedNetworkConfig(uploadPath);
public boolean saveUploadedNetworkConfig(Path uploadPath) {
return m_provider.saveUploadedNetworkConfig(uploadPath);
}
public void requestSave() {

View File

@@ -24,15 +24,15 @@ public abstract class ConfigProvider {
abstract void load();
abstract void saveToDisk();
abstract boolean saveToDisk();
PhotonConfiguration getConfig() {
return config;
}
public abstract void saveUploadedHardwareConfig(Path uploadPath);
public abstract boolean saveUploadedHardwareConfig(Path uploadPath);
public abstract void saveUploadedHardwareSettings(Path uploadPath);
public abstract boolean saveUploadedHardwareSettings(Path uploadPath);
public abstract void saveUploadedNetworkConfig(Path uploadPath);
public abstract boolean saveUploadedNetworkConfig(Path uploadPath);
}

View File

@@ -184,7 +184,7 @@ class LegacyConfigProvider extends ConfigProvider {
}
@Override
public void saveToDisk() {
public boolean saveToDisk() {
// Delete old configs
FileUtils.deleteDirectory(camerasFolder.toPath());
@@ -239,6 +239,7 @@ class LegacyConfigProvider extends ConfigProvider {
}
}
logger.info("Settings saved!");
return false; // TODO, deal with this. Do I need to?
}
private HashMap<String, CameraConfiguration> loadCameraConfigs() {
@@ -400,19 +401,19 @@ class LegacyConfigProvider extends ConfigProvider {
return this.networkConfigFile.toPath();
}
public void saveUploadedHardwareConfig(Path uploadPath) {
FileUtils.deleteFile(this.getHardwareConfigFile());
FileUtils.copyFile(uploadPath, this.getHardwareConfigFile());
@Override
public boolean saveUploadedHardwareConfig(Path uploadPath) {
return FileUtils.replaceFile(uploadPath, this.getHardwareConfigFile());
}
public void saveUploadedHardwareSettings(Path uploadPath) {
FileUtils.deleteFile(this.getHardwareSettingsFile());
FileUtils.copyFile(uploadPath, this.getHardwareSettingsFile());
@Override
public boolean saveUploadedHardwareSettings(Path uploadPath) {
return FileUtils.replaceFile(uploadPath, this.getHardwareSettingsFile());
}
public void saveUploadedNetworkConfig(Path uploadPath) {
FileUtils.deleteFile(this.getNetworkConfigFile());
FileUtils.copyFile(uploadPath, this.getNetworkConfigFile());
@Override
public boolean saveUploadedNetworkConfig(Path uploadPath) {
return FileUtils.replaceFile(uploadPath, this.getNetworkConfigFile());
}
public void requestSave() {

View File

@@ -79,15 +79,6 @@ public class NetworkConfig {
setShouldManage(shouldManage);
}
public static NetworkConfig fromHashMap(Map<String, Object> map) {
try {
return new ObjectMapper().convertValue(map, NetworkConfig.class);
} catch (Exception e) {
e.printStackTrace();
return new NetworkConfig();
}
}
public Map<String, Object> toHashMap() {
try {
return new ObjectMapper().convertValue(this, JacksonUtils.UIMap.class);

View File

@@ -168,14 +168,15 @@ public class SqlConfigProvider extends ConfigProvider {
}
@Override
public void saveToDisk() {
public boolean saveToDisk() {
logger.debug("Saving to disk");
var conn = createConn();
if (conn == null) return;
if (conn == null) return false;
synchronized (m_mutex) {
if (config == null) {
logger.error("Config null! Cannot save");
return;
return false;
}
saveCameras(conn);
@@ -185,11 +186,14 @@ public class SqlConfigProvider extends ConfigProvider {
try {
conn.close();
} catch (SQLException e) {
// TODO, does the file still save if the SQL connection isn't closed correctly? If so,
// return false here.
logger.error("SQL Err closing connection while saving to disk: ", e);
}
}
logger.info("Settings saved!");
return true;
}
@Override
@@ -366,14 +370,16 @@ public class SqlConfigProvider extends ConfigProvider {
}
}
private <T> void saveOneFile(String fname, Path path) {
private boolean saveOneFile(String fname, Path path) {
Connection conn = null;
PreparedStatement statement1 = null;
try {
conn = createConn();
if (conn == null) {
return;
return false;
}
// Replace this camera's row with the new settings
var sqlString = "REPLACE INTO global (filename, contents) VALUES " + "(?,?);";
@@ -382,36 +388,38 @@ public class SqlConfigProvider extends ConfigProvider {
statement1.executeUpdate();
conn.commit();
return true;
} catch (SQLException | IOException e) {
logger.error("Err saving global", e);
logger.error("Error while saving file to global: ", e);
try {
conn.rollback();
} catch (SQLException e1) {
logger.error("Err rolling back changes: ", e);
logger.error("Error rolling back changes: ", e);
}
return false;
} finally {
try {
if (statement1 != null) statement1.close();
conn.close();
} catch (SQLException e) {
logger.error("SQL Err saving file " + fname, e);
logger.error("SQL Error saving file " + fname, e);
}
}
}
@Override
public void saveUploadedHardwareConfig(Path uploadPath) {
saveOneFile(TableKeys.HARDWARE_CONFIG, uploadPath);
public boolean saveUploadedHardwareConfig(Path uploadPath) {
return saveOneFile(TableKeys.HARDWARE_CONFIG, uploadPath);
}
@Override
public void saveUploadedHardwareSettings(Path uploadPath) {
saveOneFile(TableKeys.HARDWARE_SETTINGS, uploadPath);
public boolean saveUploadedHardwareSettings(Path uploadPath) {
return saveOneFile(TableKeys.HARDWARE_SETTINGS, uploadPath);
}
@Override
public void saveUploadedNetworkConfig(Path uploadPath) {
saveOneFile(TableKeys.NETWORK_CONFIG, uploadPath);
public boolean saveUploadedNetworkConfig(Path uploadPath) {
return saveOneFile(TableKeys.NETWORK_CONFIG, uploadPath);
}
private HashMap<String, CameraConfiguration> loadCameraConfigs(Connection conn) {

View File

@@ -58,24 +58,55 @@ public class FileUtils {
}
}
public static void deleteFile(Path path) {
/**
* Delete the file at the path.
*
* @param path file path to delete.
* @return whether the operation was successful.
*/
public static boolean deleteFile(Path path) {
try {
Files.delete(path);
return true;
} catch (FileNotFoundException | NoSuchFileException fe) {
logger.warn("Tried to delete file \"" + path + "\" but it did not exist");
return false;
} catch (IOException e) {
logger.error("Exception deleting file \"" + path + "\"!", e);
return false;
}
}
public static void copyFile(Path src, Path dst) {
/**
* Copy a file from a source to a new destination.
*
* @param src the file path to copy.
* @param dst the file path to replace.
* @return whether the operation was successful.
*/
public static boolean copyFile(Path src, Path dst) {
try {
Files.copy(src, dst);
return true;
} catch (IOException e) {
logger.error("Exception copying file " + src + " to " + dst + "!", e);
return false;
}
}
/**
* Replace the destination file with a new source.
*
* @param src the file path to replace with.
* @param dst the file path to replace.
* @return whether the operation was successful.
*/
public static boolean replaceFile(Path src, Path dst) {
boolean fileDeleted = deleteFile(dst);
boolean fileCopied = copyFile(src, dst);
return fileDeleted && fileCopied;
}
public static void setFilePerms(Path path) throws IOException {
if (Platform.isLinux()) {
File thisFile = path.toFile();