|
20 | 20 | import edu.wpi.first.networktables.IntegerPublisher; |
21 | 21 | import edu.wpi.first.networktables.IntegerSubscriber; |
22 | 22 | import java.io.IOException; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.ArrayList; |
23 | 25 | import java.util.HashMap; |
24 | 26 | import java.util.Map; |
25 | 27 | import org.photonvision.common.configuration.ConfigManager; |
26 | 28 | import org.photonvision.common.configuration.HardwareConfig; |
27 | 29 | import org.photonvision.common.configuration.HardwareSettings; |
| 30 | +import org.photonvision.common.configuration.PathManager; |
28 | 31 | import org.photonvision.common.dataflow.networktables.NTDataChangeListener; |
| 32 | +import org.photonvision.common.dataflow.networktables.NTDriverStation; |
29 | 33 | import org.photonvision.common.dataflow.networktables.NetworkTablesManager; |
30 | 34 | import org.photonvision.common.hardware.GPIO.CustomGPIO; |
31 | 35 | import org.photonvision.common.hardware.GPIO.pi.PigpioSocket; |
@@ -228,4 +232,89 @@ private void statusLEDUpdate() { |
228 | 232 | public void publishMetrics() { |
229 | 233 | metricsManager.publishMetrics(); |
230 | 234 | } |
| 235 | + |
| 236 | + /** |
| 237 | + * Ensures there is enough space for new recordings by clearing stored recordings to free up disk |
| 238 | + * space. This method should delete the oldest recordings, prioritizing recordings from matches |
| 239 | + * over practice sessions regardless of age. |
| 240 | + * |
| 241 | + * @param requestedSpace The amount of space to free in MB |
| 242 | + * @return true if enough space was cleared, false otherwise |
| 243 | + */ |
| 244 | + public boolean reserveRecordingSpace(double requestedSpace) { |
| 245 | + if (metricsManager.getDiskSpaceAvailable() > requestedSpace * 1024) { |
| 246 | + return true; // Enough space already available |
| 247 | + } |
| 248 | + |
| 249 | + Path recordingsDir = PathManager.getInstance().getRootFolder().resolve("recordings"); |
| 250 | + |
| 251 | + // Create a list of all the recordings |
| 252 | + ArrayList<Path> recordings = new ArrayList<>(); |
| 253 | + try { |
| 254 | + if (!java.nio.file.Files.exists(recordingsDir) |
| 255 | + || !java.nio.file.Files.isDirectory(recordingsDir)) { |
| 256 | + logger.error("Recordings directory does not exist"); |
| 257 | + return false; // No recordings directory |
| 258 | + } |
| 259 | + |
| 260 | + try (var cameraStream = java.nio.file.Files.list(recordingsDir)) { |
| 261 | + cameraStream |
| 262 | + .filter(java.nio.file.Files::isDirectory) |
| 263 | + .forEach( |
| 264 | + cameraDir -> { |
| 265 | + try (var recStream = java.nio.file.Files.list(cameraDir)) { |
| 266 | + recStream.filter(java.nio.file.Files::isDirectory).forEach(recordings::add); |
| 267 | + } catch (Exception e) { |
| 268 | + // skip unreadable camera folder |
| 269 | + } |
| 270 | + }); |
| 271 | + } |
| 272 | + } catch (Exception e) { |
| 273 | + return false; // Unable to list recordings |
| 274 | + } |
| 275 | + |
| 276 | + if (recordings.isEmpty()) { |
| 277 | + return false; // No recordings to delete |
| 278 | + } |
| 279 | + |
| 280 | + // Create practice list and match list |
| 281 | + ArrayList<Path> matchRecordings = new ArrayList<>(); |
| 282 | + ArrayList<Path> practiceRecordings = new ArrayList<>(); |
| 283 | + |
| 284 | + for (Path rec : recordings) { |
| 285 | + String dirName = rec.getFileName().toString().toLowerCase(); |
| 286 | + if (dirName.startsWith("event")) { |
| 287 | + matchRecordings.add(rec); |
| 288 | + } else { |
| 289 | + practiceRecordings.add(rec); |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + // Sort both lists based on solely the filename |
| 294 | + matchRecordings.sort( |
| 295 | + (a, b) -> |
| 296 | + NTDriverStation.compareMatchData( |
| 297 | + a.getFileName().toString(), b.getFileName().toString())); |
| 298 | + practiceRecordings.sort( |
| 299 | + (a, b) -> a.getFileName().toString().compareTo(b.getFileName().toString())); |
| 300 | + |
| 301 | + while (requestedSpace * 1024 |
| 302 | + > HardwareManager.getInstance().metricsManager.getDiskSpaceAvailable()) { |
| 303 | + Path toDelete = null; |
| 304 | + if (!practiceRecordings.isEmpty()) { |
| 305 | + toDelete = practiceRecordings.remove(0); |
| 306 | + } else if (!matchRecordings.isEmpty()) { |
| 307 | + toDelete = matchRecordings.remove(0); |
| 308 | + } else { |
| 309 | + return false; // No recordings left to delete |
| 310 | + } |
| 311 | + try { |
| 312 | + java.nio.file.Files.delete(toDelete); |
| 313 | + } catch (Exception e) { |
| 314 | + // Ignore errors during deletion |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + return true; |
| 319 | + } |
231 | 320 | } |
0 commit comments