Skip to content

Commit

Permalink
Added team 6328 network alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Oct 16, 2023
1 parent 6b4af05 commit f4e519f
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/differential_d
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/field_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/fms_info.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/gyro.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/network_alerts.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/pid_controller.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/power_distribution.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/combo_box_chooser.dart';
Expand Down Expand Up @@ -395,6 +396,11 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
key: UniqueKey(),
jsonData: jsonData['properties'],
);
case 'Alerts':
return NetworkAlerts.fromJson(
key: UniqueKey(),
jsonData: jsonData['properties'],
);
default:
return TextDisplay.fromJson(
key: UniqueKey(),
Expand Down
6 changes: 6 additions & 0 deletions lib/widgets/network_tree/tree_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/differential_d
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/field_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/fms_info.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/gyro.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/network_alerts.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/pid_controller.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/power_distribution.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/combo_box_chooser.dart';
Expand Down Expand Up @@ -170,6 +171,8 @@ class TreeRow {
return FMSInfo(key: UniqueKey(), topic: topic);
case 'RobotPreferences':
return RobotPreferences(key: UniqueKey(), topic: topic);
case 'Alerts':
return NetworkAlerts(key: UniqueKey(), topic: topic);
}

return null;
Expand Down Expand Up @@ -214,6 +217,9 @@ class TreeRow {
} else if (primary is RobotPreferences) {
width = normalGridSize * 2;
height = normalGridSize * 3;
} else if (primary is NetworkAlerts) {
width = normalGridSize * 3;
height = normalGridSize * 2;
}

return WidgetContainer(
Expand Down
138 changes: 138 additions & 0 deletions lib/widgets/nt4_widgets/multi-topic/network_alerts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class NetworkAlerts extends StatelessWidget with NT4Widget {
@override
String type = 'Alerts';

late String errorsTopicName;
late String warningsTopicName;
late String infosTopicName;

NetworkAlerts({super.key, required topic, period = Globals.defaultPeriod}) {
super.topic = topic;
super.period = period;

init();
}

NetworkAlerts.fromJson({super.key, required Map<String, dynamic> jsonData}) {
topic = tryCast(jsonData['topic']) ?? '';
period = tryCast(jsonData['period']) ?? Globals.defaultPeriod;

init();
}

@override
void init() {
super.init();

errorsTopicName = '$topic/errors';
warningsTopicName = '$topic/warnings';
infosTopicName = '$topic/infos';
}

@override
void resetSubscription() {
super.resetSubscription();

errorsTopicName = '$topic/errors';
warningsTopicName = '$topic/warnings';
infosTopicName = '$topic/infos';
}

@override
Widget build(BuildContext context) {
notifier = context.watch<NT4WidgetNotifier?>();

return StreamBuilder(
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
List<Object?> errorsRaw = nt4Connection
.getLastAnnouncedValue(errorsTopicName)
?.tryCast<List<Object?>>() ??
[];

List<Object?> warningsRaw = nt4Connection
.getLastAnnouncedValue(warningsTopicName)
?.tryCast<List<Object?>>() ??
[];

List<Object?> infosRaw = nt4Connection
.getLastAnnouncedValue(infosTopicName)
?.tryCast<List<Object?>>() ??
[];

List<String> errors = errorsRaw.whereType<String>().toList();
List<String> warnings = warningsRaw.whereType<String>().toList();
List<String> infos = infosRaw.whereType<String>().toList();

return ListView.builder(
itemCount: errors.length + warnings.length + infos.length,
itemBuilder: (context, index) {
String alertType = 'error';
String alertMessage;
if (index >= errors.length) {
index -= errors.length;
alertType = 'warning';
}
if (index >= warnings.length && alertType == 'warning') {
index -= warnings.length;
alertType = 'info';
}
if (index >= infos.length && alertType == 'info') {
alertType = 'none';
}

TextStyle? messageStyle = Theme.of(context).textTheme.bodyMedium;

switch (alertType) {
case 'error':
alertMessage = errors[index];
return ListTile(
dense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
leading: const Icon(
Icons.cancel,
size: 24,
color: Colors.red,
),
title: Text(alertMessage, style: messageStyle),
);
case 'warning':
alertMessage = warnings[index];
return ListTile(
dense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
leading: const Icon(
Icons.warning,
size: 24,
color: Colors.yellow,
),
title: Text(alertMessage, style: messageStyle),
);
case 'info':
alertMessage = infos[index];
return ListTile(
dense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
leading: const Icon(
Icons.info,
size: 24,
color: Colors.green,
),
title: Text(alertMessage, style: messageStyle),
);
default:
return Container();
}
},
);
},
);
}
}

0 comments on commit f4e519f

Please sign in to comment.