Skip to content

Commit

Permalink
Added text displaying bandwidth usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Apr 28, 2024
1 parent 8144b49 commit bc2ead6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:collection/collection.dart';
import 'package:dot_cast/dot_cast.dart';
import 'package:elegant_notification/elegant_notification.dart';
import 'package:file_selector/file_selector.dart';
import 'package:multiple_stream_builder/multiple_stream_builder.dart';
import 'package:popover/popover.dart';
import 'package:screen_retriever/screen_retriever.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -1559,13 +1560,15 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
),
),
Expanded(
child: StreamBuilder(
stream: ntConnection.latencyStream(),
child: StreamBuilder2(
streams: StreamTuple2(ntConnection.bandiwdthStream(),
ntConnection.latencyStream()),
builder: (context, snapshot) {
int latency = snapshot.data ?? 0;
double bandwidth = snapshot.snapshot1.data ?? 0.0;
int latency = snapshot.snapshot2.data ?? 0;

return Text(
'Latency: ${latency.toString().padLeft(5)} ms',
'${bandwidth.toStringAsFixed(2).padLeft(5)} kb/s | ${latency.toString().padLeft(1)} ms',
textAlign: TextAlign.right,
);
}),
Expand Down
45 changes: 43 additions & 2 deletions lib/services/nt4_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ class NT4Client {
int _clientId = 0;
int _serverTimeOffsetUS = 0;
int _latencyMs = 0;
int _bitUsage = 0;
double _dataRateKbPerSecond = 0;

WebSocketChannel? _mainWebsocket;
StreamSubscription? _mainWebsocketListener;
Expand Down Expand Up @@ -310,6 +312,21 @@ class NT4Client {
}
}

Stream<double> bandwidthStream() async* {
yield _dataRateKbPerSecond;

double lastYielded = _dataRateKbPerSecond;

while (true) {
await Future.delayed(const Duration(seconds: 1));

if (_dataRateKbPerSecond != lastYielded) {
yield _dataRateKbPerSecond;
lastYielded = _dataRateKbPerSecond;
}
}
}

void addTopicAnnounceListener(Function(NT4Topic topic) onAnnounce) {
_topicAnnounceListeners.add(onAnnounce);
}
Expand Down Expand Up @@ -504,12 +521,17 @@ class NT4Client {
var rawData =
serialize([timeTopic.pubUID, 0, timeTopic.getTypeId(), timeToSend]);

_bitUsage += rawData.lengthInBytes * 8;

if (_useRTT) {
_rttWebsocket?.sink.add(rawData);
} else {
_mainWebsocket?.sink.add(rawData);
}
}

_dataRateKbPerSecond = (_bitUsage / 1000) / (_pingInterval / 1000);
_bitUsage = 0;
}

void _rttHandleRecieveTimestamp(int serverTimestamp, int clientTimestamp) {
Expand Down Expand Up @@ -545,15 +567,22 @@ class NT4Client {
}

void _wsSendJSON(String method, Map<String, dynamic> params) {
_mainWebsocket?.sink.add(jsonEncode([
String encodedJson = jsonEncode([
{
'method': method,
'params': params,
}
]));
]);

_bitUsage += encodedJson.length * 8;

_mainWebsocket?.sink.add(encodedJson);
}

void _wsSendBinary(dynamic data) {
if (data is Uint8List) {
_bitUsage += data.lengthInBytes * 8;
}
_mainWebsocket?.sink.add(data);
}

Expand Down Expand Up @@ -685,6 +714,10 @@ class NT4Client {
return;
}

if (data is Uint8List) {
_bitUsage += data.lengthInBytes * 8;
}

var msg = Unpacker.fromList(data).unpackList();

int topicID = msg[0] as int;
Expand Down Expand Up @@ -758,6 +791,9 @@ class NT4Client {
_lastPongTime = 0;
_latencyMs = 0;

_bitUsage = 0;
_dataRateKbPerSecond = 0;

logger.info('Network Tables disconnected');
onDisconnect?.call();

Expand All @@ -770,6 +806,8 @@ class NT4Client {

void _wsOnMessage(data) {
if (data is String) {
_bitUsage += data.length * 8;

var rxArr = jsonDecode(data.toString());

if (rxArr is! List) {
Expand Down Expand Up @@ -846,6 +884,9 @@ class NT4Client {
}
}
} else {
if (data is Uint8List) {
_bitUsage += data.lengthInBytes * 8;
}
var u = Unpacker.fromList(data);

bool done = false;
Expand Down
4 changes: 4 additions & 0 deletions lib/services/nt_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class NTConnection {
return nt4Client.latencyStream();
}

Stream<double> bandiwdthStream() {
return nt4Client.bandwidthStream();
}

void changeIPAddress(String ipAddress) {
if (_ntClient.serverBaseAddress == ipAddress) {
return;
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
multiple_stream_builder:
dependency: "direct main"
description:
name: multiple_stream_builder
sha256: fec7a8909d0942d1a8d726b7e3057bd73e2ab64a5ae2da6b7b44a959f17f09fb
url: "https://pub.dev"
source: hosted
version: "3.0.2"
nested:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
logger: ^2.0.2+1
messagepack: ^0.2.1
msgpack_dart: ^1.0.1
multiple_stream_builder: ^3.0.2
package_info_plus: ^5.0.1
path: ^1.8.3
path_provider: ^2.0.15
Expand Down
3 changes: 3 additions & 0 deletions test/pages/dashboard_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ void main() {
.thenAnswer((_) => Stream.value(true));
when(mockNT4Connection.latencyStream()).thenAnswer((_) => Stream.value(0));

when(mockNT4Connection.bandiwdthStream())
.thenAnswer((_) => Stream.value(0.0));

when(mockSubscription.periodicStream())
.thenAnswer((_) => Stream.value(null));

Expand Down
3 changes: 3 additions & 0 deletions test/services/shuffleboard_nt_listener_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ void main() {

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

when(mockNT4Connection.bandiwdthStream())
.thenAnswer((_) => Stream.value(0.0));

when(mockNT4Connection.getLastAnnouncedValue(any)).thenAnswer(
(realInvocation) =>
lastAnnouncedValues[realInvocation.positionalArguments[0]],
Expand Down
6 changes: 6 additions & 0 deletions test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ void setupMockOfflineNT4() {

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

when(mockNT4Connection.bandiwdthStream())
.thenAnswer((_) => Stream.value(0.0));

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

when(mockNT4Connection.subscribe(any, any)).thenReturn(mockSubscription);
Expand Down Expand Up @@ -81,6 +84,9 @@ void setupMockOnlineNT4() {

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

when(mockNT4Connection.bandiwdthStream())
.thenAnswer((_) => Stream.value(0.0));

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

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

0 comments on commit bc2ead6

Please sign in to comment.