diff --git a/src/main/java/org/sciborgs1155/lib/FaultLogger.java b/src/main/java/org/sciborgs1155/lib/FaultLogger.java index 6e06a0d..d4154ec 100644 --- a/src/main/java/org/sciborgs1155/lib/FaultLogger.java +++ b/src/main/java/org/sciborgs1155/lib/FaultLogger.java @@ -56,6 +56,7 @@ public void set(Set alerts) { // DATA private static final HashMap alertReporters = new HashMap<>(0); + private static final HashMap allAlerts = new HashMap<>(0); private static final Set activeAlerts = new HashSet<>(); private static final Set totalAlerts = new HashSet<>(); @@ -68,10 +69,9 @@ public void set(Set alerts) { public static void update() { alertReporters.forEach( (r, a) -> { + a.set(r.getAsBoolean()); if (r.getAsBoolean()) { - a.set(true); - } else { - a.set(false); + report(a); } }); @@ -135,7 +135,14 @@ public static void report(Alert alert) { * @param type The type of the alert. */ public static void report(String name, String description, AlertType type) { - report(new Alert(name, description, type)); + allAlerts.clear(); + alertReporters.forEach((r, a) -> allAlerts.put(alertToString(a), a)); + + Alert alert = new Alert(name, description, type); + if (allAlerts.containsKey(alertToString(alert))) { + alert = allAlerts.get(alertToString(alert)); + } + report(alert); } /** @@ -357,6 +364,7 @@ public static boolean check(SparkBase spark, REVLibError error) { /** * Returns an array of descriptions of all alerts that match the specified type. * + * @param alerts Alerts to search through. * @param type The type to filter for. * @return An array of description strings. */ @@ -366,4 +374,20 @@ private static String[] filteredStrings(Set alerts, AlertType type) { .map(Alert::getText) .toArray(String[]::new); } + + /** + * Converts a WPILIB Alert to a String, which can be used to identify if Alerts are identical. + * + * @param alert + * @return + */ + private static String alertToString(Alert alert) { + return String.valueOf(alert.get()) + + alert.getText() + + (switch (alert.getType()) { + case kError -> "error"; + case kWarning -> "warning"; + case kInfo -> "info"; + }); + } } diff --git a/src/test/java/org/sciborgs1155/lib/FaultLoggerTest.java b/src/test/java/org/sciborgs1155/lib/FaultLoggerTest.java index b5c044a..ede49de 100644 --- a/src/test/java/org/sciborgs1155/lib/FaultLoggerTest.java +++ b/src/test/java/org/sciborgs1155/lib/FaultLoggerTest.java @@ -26,11 +26,11 @@ public void setup() { @Test void report() { - NetworkTable base = NetworkTableInstance.getDefault().getTable("Faults"); + NetworkTable base = NetworkTableInstance.getDefault().getTable("Alerts"); var activeInfos = - base.getSubTable("Active Faults").getStringArrayTopic("infos").subscribe(new String[10]); + base.getSubTable("Active Alerts").getStringArrayTopic("infos").subscribe(new String[10]); var totalErrors = - base.getSubTable("Total Faults").getStringArrayTopic("errors").subscribe(new String[10]); + base.getSubTable("Total Alerts").getStringArrayTopic("errors").subscribe(new String[10]); FaultLogger.update(); FaultLogger.report("Test", "Example", AlertType.kInfo); assertEquals(1, FaultLogger.activeAlerts().size()); @@ -57,11 +57,11 @@ void report() { @Test void register() { - NetworkTable base = NetworkTableInstance.getDefault().getTable("Faults"); + NetworkTable base = NetworkTableInstance.getDefault().getTable("Alerts"); var activeErrors = - base.getSubTable("Active Faults").getStringArrayTopic("errors").subscribe(new String[10]); + base.getSubTable("Active Alerts").getStringArrayTopic("errors").subscribe(new String[10]); var totalErrors = - base.getSubTable("Total Faults").getStringArrayTopic("errors").subscribe(new String[10]); + base.getSubTable("Total Alerts").getStringArrayTopic("errors").subscribe(new String[10]); FaultLogger.update(); FaultLogger.register(() -> true, "Recurring Test", "Idk", AlertType.kError);