From 86b4f4cfe337ca59ff75ba5b14bf6adfd5f85723 Mon Sep 17 00:00:00 2001 From: Gold87 <91761103+Gold872@users.noreply.github.com> Date: Mon, 24 Jun 2024 21:53:55 -0400 Subject: [PATCH] Increase notification time (#50) Also rename the notification table from "elastic" to "Elastic", and add more safety checks when parsing notification data --- elasticlib/Elastic.java | 2 +- lib/pages/dashboard_page.dart | 3 ++- .../robot_notifications_listener.dart | 27 ++++++++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/elasticlib/Elastic.java b/elasticlib/Elastic.java index 0a7f8694..1c88070b 100644 --- a/elasticlib/Elastic.java +++ b/elasticlib/Elastic.java @@ -11,7 +11,7 @@ public final class Elastic { private static final StringTopic topic = NetworkTableInstance.getDefault() - .getStringTopic("elastic/robotnotifications"); + .getStringTopic("/Elastic/robotnotifications"); private static final StringPublisher publisher = topic.publish(PubSubOption.sendAll(true)); private static final ObjectMapper objectMapper = new ObjectMapper(); diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index 7fa10597..c1a37977 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -228,10 +228,11 @@ class _DashboardPageState extends State with WindowListener { fontWeight: FontWeight.bold, ), ), + toastDuration: const Duration(seconds: 3), icon: icon, description: Text(description), stackedOptions: StackedOptions( - key: 'robotnotification', + key: 'robot_notification', type: StackedType.above, itemOffset: const Offset(0, 5), ), diff --git a/lib/services/robot_notifications_listener.dart b/lib/services/robot_notifications_listener.dart index de3c1b3d..d61b955b 100644 --- a/lib/services/robot_notifications_listener.dart +++ b/lib/services/robot_notifications_listener.dart @@ -18,30 +18,43 @@ class RobotNotificationsListener { void listen() { var notifications = - ntConnection.subscribeAll("elastic/robotnotifications", 0.2); + ntConnection.subscribeAll('/Elastic/robotnotifications', 0.2); notifications.listen((alertData, alertTimestamp) { - _onAlert(alertData!, alertTimestamp); + if (alertData == null) { + return; + } + _onAlert(alertData, alertTimestamp); }); + + ntConnection.addDisconnectedListener(() => _alertFirstRun = true); } void _onAlert(Object alertData, int timestamp) { - //prevent showing a notification when we connect to NT + // prevent showing a notification when we connect to NT if (_alertFirstRun) { _alertFirstRun = false; return; } - Map data = jsonDecode(alertData.toString()); + Map data; + try { + data = jsonDecode(alertData.toString()); + } catch (e) { + return; + } + + if (!data.containsKey('level')) {} + Icon icon; - if (data["level"] == "INFO") { + if (data['level'] == 'INFO') { icon = const Icon(Icons.info); - } else if (data["level"] == "WARNING") { + } else if (data['level'] == 'WARNING') { icon = const Icon( Icons.warning_amber, color: Colors.orange, ); - } else if (data["level"] == "ERROR") { + } else if (data['level'] == 'ERROR') { icon = const Icon( Icons.error, color: Colors.red,