Skip to content

Commit

Permalink
still fails
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLi-0 committed Jan 7, 2025
1 parent a73dbe3 commit 8235fec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
32 changes: 28 additions & 4 deletions src/main/java/org/sciborgs1155/lib/FaultLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void set(Set<Alert> alerts) {

// DATA
private static final HashMap<BooleanSupplier, Alert> alertReporters = new HashMap<>(0);
private static final HashMap<String, Alert> allAlerts = new HashMap<>(0);
private static final Set<Alert> activeAlerts = new HashSet<>();
private static final Set<Alert> totalAlerts = new HashSet<>();

Expand All @@ -68,10 +69,9 @@ public void set(Set<Alert> 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);
}
});

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*/
Expand All @@ -366,4 +374,20 @@ private static String[] filteredStrings(Set<Alert> 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";
});
}
}
12 changes: 6 additions & 6 deletions src/test/java/org/sciborgs1155/lib/FaultLoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down

0 comments on commit 8235fec

Please sign in to comment.