Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow robot created notifications #48

Merged
merged 14 commits into from
Jun 25, 2024
Merged

Allow robot created notifications #48

merged 14 commits into from
Jun 25, 2024

Conversation

EmeraldWither
Copy link
Contributor

2024-06-18.13-15-08.mp4

Robot code usage:

 @Override
  public void autonomousInit() {
    Elastic.sendAlert(AlertLevel.INFO, "Autonomous!", "The robot is now in auto.");

There are 3 levels of notifications:

INFO:
image

WARNING:
image

ERROR:
image

The idea is that the driver can get a notification when something happens (eg. an arm extends too far and gets disabled).

Users can copy the Elastic class into their robot project, and use those methods to push notifications to the dashboard.

Copy link
Owner

@Gold872 Gold872 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea, however I'm wondering where it would be better to have users use the network alerts vs the notifications

There should also be some way of unit testing this, the PR for restructuring the NT dependency makes testing it much easier, so it might make more sense to add unit tests for notifications after the rework is complete

Comment on lines 213 to 266
var notifications = ntConnection.subscribe("notifications");

//dont display a notification when we first connect
bool notificationFirstRun = true;
notifications.listen((p0, p1) {
List<String> data = p0.toString().replaceAll("[", "").replaceAll("]", "").split(",");
if(notificationFirstRun) {
notificationFirstRun = false;
return;
}
Icon icon;
if(data[0] == "INFO") {
icon = const Icon(Icons.info);
}
else if(data[0] == "WARNING") {
icon = const Icon(Icons.warning_amber, color: Colors.orange,);
}
else if(data[0] == "ERROR") {
icon = const Icon(Icons.error, color: Colors.red,);
}
else {
icon = const Icon(Icons.question_mark);
}

String title = data[1];
String description = data[2];
setState(() {
ColorScheme colorScheme = Theme.of(context).colorScheme;
TextTheme textTheme = Theme.of(context).textTheme;
ElegantNotification notification = ElegantNotification(
autoDismiss: true,
showProgressIndicator: true,
background: colorScheme.surface,
width: 350,
position: Alignment.bottomRight,
title: Text(
title,
style: textTheme.bodyMedium!.copyWith(
fontWeight: FontWeight.bold,
),
),
icon: icon,
description: Text(description),
stackedOptions: StackedOptions(
key: 'notification',
type: StackedType.above,
itemOffset: const Offset(0, 5),
),
);
if (mounted) {
notification.show(context);
}
});
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the listening logic would belong better in a separate class that you pass a function into which is called when a new notification is published. The main reason is because it could eventually cause the method to become a bit cluttered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a robot alert service similar be a good way of going about this?

import edu.wpi.first.networktables.StringArrayTopic;

public final class Elastic {
private static final StringArrayTopic topic = NetworkTableInstance.getDefault().getStringArrayTopic("notifications");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change the topic to something like "elastic/notifications" in case if there's ever some library that also publishes to a topic called notifications?

Copy link
Contributor Author

@EmeraldWither EmeraldWither Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking of renaming it to "elastic/alerts" since I think that would make more sense.

Robot alerts vs Elastic Notifications. Maybe an icon on the notification can differentiate the two? Also renaming everywhere to alerts would probably make more sense.

Comment on lines 14 to 16
public static void sendAlert(AlertLevel level, String title, String description) {
publisher.set(new String[] {level.toString(), title, description});
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be easier to maintain long term if it used a different format that's easier to expand on. Maybe there could be a way of publishing some sort of json map that can have different properties if we want to add more customization later on

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a json map would be the way to go here.

@EmeraldWither
Copy link
Contributor Author

Hey thanks for looking at this.

There should also be some way of unit testing this, the PR for restructuring the NT dependency makes testing it much easier, so it might make more sense to add unit tests for notifications after the rework is complete

Unit testing is something that I haven't done before (😓), but my summer break starts today, so I will take a look at how to do this and will put in later.

@EmeraldWither EmeraldWither marked this pull request as draft June 21, 2024 01:57
Copy link
Owner

@Gold872 Gold872 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far this is looking really good, I think this would be a great addition to the dashboard. Some things that would have to be added (such as unit tests) aren't very easy to do in the main branch, so some parts would have to be added in the NT connection rework branch after this is merged

elasticlib/Elastic.java Outdated Show resolved Hide resolved
elasticlib/Elastic.java Outdated Show resolved Hide resolved
lib/services/robot_alerts.dart Outdated Show resolved Hide resolved
elasticlib/Elastic.java Show resolved Hide resolved
lib/pages/dashboard_page.dart Outdated Show resolved Hide resolved
lib/services/nt4_client.dart Outdated Show resolved Hide resolved
addresses name conflicts with wpilibs stuff in 2025
Copy link

codecov bot commented Jun 22, 2024

Codecov Report

Attention: Patch coverage is 23.07692% with 20 lines in your changes missing coverage. Please review.

Project coverage is 45.45%. Comparing base (f1d29a6) to head (4666061).

Current head 4666061 differs from pull request most recent head cce8f91

Please upload reports for the commit cce8f91 to get more accurate results.

Files Patch % Lines
lib/services/robot_notifications_listener.dart 13.63% 19 Missing ⚠️
lib/pages/dashboard_page.dart 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #48      +/-   ##
==========================================
- Coverage   45.53%   45.45%   -0.08%     
==========================================
  Files          73       74       +1     
  Lines        7465     7489      +24     
==========================================
+ Hits         3399     3404       +5     
- Misses       4066     4085      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EmeraldWither EmeraldWither marked this pull request as ready for review June 25, 2024 00:54
@EmeraldWither EmeraldWither requested a review from Gold872 June 25, 2024 00:55
Copy link
Owner

@Gold872 Gold872 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, I'll have to merge these changes into the rework branch and there might be some slight modifications to work better with the new NT api, but this is ready to put into main

color: Colors.red,
);
} else {
icon = const Icon(Icons.question_mark);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this

@Gold872 Gold872 merged commit 79d80b6 into Gold872:main Jun 25, 2024
5 checks passed
@Gold872
Copy link
Owner

Gold872 commented Jun 25, 2024

@EmeraldWither quick question, what's the reason for having it send all if it ignores the notification on initial connection?

Also, I merged all of these changes into the nt-dependency-rework branch, so you can add unit tests for this much easier. Whenever you're ready, just make a PR to that branch. Let me know if you need any help with the tests

This is definitely a great addition, thanks for making this!

@EmeraldWither
Copy link
Contributor Author

On the initial connection, when I start listening, it will always call the method with whatever is currently inside the topic, even if it's old/stale data (eg, rebooting elastic will cause the notification so show again even though we haven't actually posted anything).

By ignoring the first time that this happens, this allows us to only receive the new data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants