Skip to content

Commit

Permalink
Fix format, add getType, inline getters
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Nov 1, 2024
1 parent bac8d0e commit 0375edc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
8 changes: 0 additions & 8 deletions wpilibc/src/main/native/cpp/Alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ void Alert::Set(bool active) {
m_active = active;
}

bool Alert::Get() const {
return m_active;
}

void Alert::SetText(std::string_view text) {
if (text == m_text) {
return;
Expand All @@ -88,10 +84,6 @@ void Alert::SetText(std::string_view text) {
}
}

std::string Alert::GetText() const {
return m_text;
}

Alert::SendableAlerts::SendableAlerts() {
m_alerts.fill({});
}
Expand Down
12 changes: 9 additions & 3 deletions wpilibc/src/main/native/include/frc/Alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Alert {
* Gets whether the alert is active.
* @return whether the alert is active.
*/
bool Get() const;
bool Get() const { return m_active; }

/**
* Updates current alert text. Use this method to dynamically change the
Expand All @@ -122,7 +122,13 @@ class Alert {
* Gets the current alert text.
* @return the current text.
*/
std::string GetText() const;
std::string GetText() const { return m_text; }

/**
* Get the type of this alert.
* @return the type
*/
AlertType GetType() const { return m_type; }

private:
class PublishedAlert {
Expand All @@ -141,7 +147,7 @@ class Alert {
void InitSendable(nt::NTSendableBuilder& builder) override;

/**
* Returns a reference to the set of active alerts for the given type
* Returns a reference to the set of active alerts for the given type.
* @param type the type
* @return reference to the set of active alerts for the type
*/
Expand Down
11 changes: 10 additions & 1 deletion wpilibj/src/main/java/edu/wpi/first/wpilibj/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ public String getText() {
return m_text;
}

/**
* Get the type of this alert.
*
* @return the type
*/
public AlertType getType() {
return m_type;
}

private record PublishedAlert(long timestamp, String text) implements Comparable<PublishedAlert> {
private static final Comparator<PublishedAlert> comparator =
Comparator.comparingLong((PublishedAlert alert) -> alert.timestamp())
Expand All @@ -175,7 +184,7 @@ private static final class SendableAlerts implements Sendable {
private final Map<AlertType, Set<PublishedAlert>> m_alerts = new HashMap<>();

/**
* Returns a reference to the set of active alerts for the given type
* Returns a reference to the set of active alerts for the given type.
*
* @param type the type
* @return reference to the set of active alerts for the type
Expand Down
14 changes: 7 additions & 7 deletions wpilibj/src/test/java/edu/wpi/first/wpilibj/AlertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

public class AlertTest {
class AlertTest {
String m_groupName;

@BeforeEach
Expand All @@ -34,7 +34,7 @@ void checkClean() {
assertEquals(0, getActiveAlerts(AlertType.kInfo).length);
}

String getSubtableName(Alert.AlertType type) {
private String getSubtableName(Alert.AlertType type) {
switch (type) {
case kError:
return "errors";
Expand All @@ -47,28 +47,28 @@ String getSubtableName(Alert.AlertType type) {
}
}

StringArraySubscriber getSubscriberForType(Alert.AlertType type) {
private StringArraySubscriber getSubscriberForType(Alert.AlertType type) {
return NetworkTableInstance.getDefault()
.getStringArrayTopic("/SmartDashboard/" + m_groupName + "/" + getSubtableName(type))
.subscribe(new String[] {});
}

String[] getActiveAlerts(AlertType type) {
private String[] getActiveAlerts(AlertType type) {
try (var sub = getSubscriberForType(type)) {
return sub.get();
}
}

void update() {
private void update() {
SmartDashboard.updateValues();
}

boolean isAlertActive(String text, Alert.AlertType type) {
private boolean isAlertActive(String text, Alert.AlertType type) {
update();
return Arrays.asList(getActiveAlerts(type)).contains(text);
}

Alert makeAlert(String text, Alert.AlertType type) {
private Alert makeAlert(String text, Alert.AlertType type) {
return new Alert(m_groupName, text, type);
}

Expand Down

0 comments on commit 0375edc

Please sign in to comment.