Skip to content

Commit

Permalink
Added text to display latency
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Dec 2, 2023
1 parent 0e3e6dc commit c22665c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 55 deletions.
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class _ElasticState extends State<Elastic> {
title: 'Elastic',
theme: theme,
home: DashboardPage(
connectionStream: nt4Connection.connectionStatus(),
preferences: widget.preferences,
version: widget.version,
onColorChanged: (color) => setState(() {
Expand Down
78 changes: 45 additions & 33 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import 'package:url_launcher/url_launcher.dart';
import 'package:window_manager/window_manager.dart';

class DashboardPage extends StatefulWidget {
final Stream<dynamic> connectionStream;
final SharedPreferences preferences;
final String version;
final Function(Color color)? onColorChanged;

const DashboardPage({
super.key,
required this.connectionStream,
required this.preferences,
required this.version,
this.onColorChanged,
Expand Down Expand Up @@ -1193,37 +1191,51 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
),
),
// Bottom bar
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: SizedBox(
height: 32,
child: StreamBuilder(
stream: widget.connectionStream,
builder: (context, snapshot) {
bool connected = snapshot.data ?? false;

Widget connectedText = (connected)
? Text(
'Network Tables: Connected (${_preferences.getString(PrefKeys.ipAddress)})',
style: footerStyle!.copyWith(
color: Colors.green,
))
: Text('Network Tables: Disconnected',
style: footerStyle!.copyWith(
color: Colors.red,
));

return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
connectedText,
Text(
'Team ${_preferences.getInt(PrefKeys.teamNumber)?.toString() ?? 'Unknown'}',
),
Opacity(opacity: 0.0, child: connectedText),
],
);
},
Container(
color: const Color.fromARGB(255, 20, 20, 20),
height: 32,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Row(
children: [
Expanded(
child: StreamBuilder(
stream: nt4Connection.connectionStatus(),
builder: (context, snapshot) {
bool connected = snapshot.data ?? false;

String connectedText = (connected)
? 'Network Tables: Connected (${_preferences.getString(PrefKeys.ipAddress)})'
: 'Network Tables: Disconnected';

return Text(
connectedText,
style: footerStyle?.copyWith(
color: (connected) ? Colors.green : Colors.red,
),
textAlign: TextAlign.left,
);
}),
),
Expanded(
child: Text(
'Team ${_preferences.getInt(PrefKeys.teamNumber)?.toString() ?? '9999'}',
textAlign: TextAlign.center,
),
),
Expanded(
child: StreamBuilder(
stream: nt4Connection.latencyStream(),
builder: (context, snapshot) {
int latency = snapshot.data ?? 0;

return Text(
'Latency: ${latency.toString().padLeft(5)} ms',
textAlign: TextAlign.right,
);
}),
),
],
),
),
),
Expand Down
22 changes: 20 additions & 2 deletions lib/services/nt4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class NT4Client {
final Map<String, NT4Topic> _clientPublishedTopics = {};
final Map<int, NT4Topic> announcedTopics = {};
int _clientId = 0;
final String _serverAddr = '';
bool _serverConnectionActive = false;
int _serverTimeOffsetUS = 0;
int _latencyMs = 0;

WebSocketChannel? _mainWebsocket;
// TODO: Uncomment for 2024 NT4.1 updates
Expand Down Expand Up @@ -57,6 +57,21 @@ class NT4Client {
_wsOnClose();
}

Stream<int> latencyStream() async* {
yield _latencyMs;

int lastYielded = _latencyMs;

while (true) {
if (_latencyMs != lastYielded) {
yield _latencyMs;
lastYielded = _latencyMs;
}

await Future.delayed(const Duration(seconds: 1));
}
}

void addTopicAnnounceListener(Function(NT4Topic topic) onAnnounce) {
_topicAnnounceListeners.add(onAnnounce);
}
Expand Down Expand Up @@ -266,7 +281,9 @@ class NT4Client {
int serverTimeAtRx = (serverTimestamp - rtt / 2.0).round();
_serverTimeOffsetUS = serverTimeAtRx - rxTime;

lastPongTime = DateTime.now().millisecondsSinceEpoch;
lastPongTime = rxTime;

_latencyMs = (rtt / 2) ~/ 1000;
}

void _wsSubscribe(NT4Subscription sub) {
Expand Down Expand Up @@ -413,6 +430,7 @@ class NT4Client {
_serverConnectionActive = false;

lastPongTime = 0;
_latencyMs = 0;

onDisconnect?.call();

Expand Down
4 changes: 4 additions & 0 deletions lib/services/nt4_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class NT4Connection {
}
}

Stream<int> latencyStream() {
return nt4Client.latencyStream();
}

void changeIPAddress(String ipAddress) {
if (_ntClient.serverBaseAddress == ipAddress) {
return;
Expand Down
20 changes: 1 addition & 19 deletions test/pages/dashboard_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand All @@ -67,7 +66,7 @@ void main() {

await widgetTester.pumpAndSettle();

expect(find.textContaining('Network Tables: Disconnected'), findsWidgets);
expect(find.textContaining('Network Tables: Disconnected'), findsOneWidget);
expect(find.textContaining('Network Tables: Connected'), findsNothing);
expect(find.textContaining('(10.3.53.2)'), findsNothing);
expect(find.text('Team 353'), findsOneWidget);
Expand All @@ -83,7 +82,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
version: '0.0.0.0',
),
Expand All @@ -108,7 +106,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -141,7 +138,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -205,7 +201,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -261,7 +256,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -406,7 +400,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -485,7 +478,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -518,7 +510,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -548,7 +539,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -579,7 +569,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -622,7 +611,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -681,7 +669,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -735,7 +722,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand All @@ -761,7 +747,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -795,7 +780,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -836,7 +820,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down Expand Up @@ -881,7 +864,6 @@ void main() {
await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
version: '0.0.0.0',
),
Expand Down
4 changes: 4 additions & 0 deletions test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void setupMockOfflineNT4() {
when(mockNT4Connection.dsConnectionStatus())
.thenAnswer((_) => Stream.value(false));

when(mockNT4Connection.latencyStream()).thenAnswer((_) => Stream.value(0));

when(mockNT4Connection.getLastAnnouncedValue(any)).thenReturn(null);

when(mockNT4Connection.subscribe(any, any)).thenReturn(mockSubscription);
Expand Down Expand Up @@ -76,6 +78,8 @@ void setupMockOnlineNT4() {
when(mockNT4Connection.dsConnectionStatus())
.thenAnswer((_) => Stream.value(true));

when(mockNT4Connection.latencyStream()).thenAnswer((_) => Stream.value(0));

when(mockNT4Connection.getLastAnnouncedValue(any)).thenReturn(null);

when(mockNT4Connection.subscribe(any, any)).thenReturn(mockSubscription);
Expand Down

0 comments on commit c22665c

Please sign in to comment.