Skip to content

Commit

Permalink
Prevent wrongly typed data from crashing program when loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Oct 15, 2023
1 parent a4aadfe commit a1a988e
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 199 deletions.
8 changes: 5 additions & 3 deletions lib/widgets/nt4_widgets/multi-topic/camera_stream.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
Expand Down Expand Up @@ -32,8 +33,8 @@ class CameraStreamWidget extends StatelessWidget with NT4Widget {

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

init();
}
Expand Down Expand Up @@ -110,7 +111,8 @@ class CameraStreamWidget extends StatelessWidget with NT4Widget {

rawStreams = value;

List<Object?> rawStreamsList = rawStreams as List<Object?>? ?? [];
List<Object?> rawStreamsList =
rawStreams?.tryCast<List<Object?>>() ?? [];

List<String> streams = [];
for (Object? stream in rawStreamsList) {
Expand Down
26 changes: 10 additions & 16 deletions lib/widgets/nt4_widgets/multi-topic/combo_box_chooser.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
Expand Down Expand Up @@ -32,8 +34,8 @@ class ComboBoxChooser extends StatelessWidget with NT4Widget {

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

init();
}
Expand Down Expand Up @@ -99,34 +101,26 @@ class ComboBoxChooser extends StatelessWidget with NT4Widget {
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
List<Object?> rawOptions = nt4Connection
.getLastAnnouncedValue(optionsTopicName) as List<Object?>? ??
.getLastAnnouncedValue(optionsTopicName)
?.tryCast<List<Object?>>() ??
[];

List<String> options = [];

for (Object? option in rawOptions) {
if (option == null || option is! String) {
continue;
}

options.add(option);
}
List<String> options = rawOptions.whereType<String>().toList();

String? active =
nt4Connection.getLastAnnouncedValue(activeTopicName) as String?;
tryCast(nt4Connection.getLastAnnouncedValue(activeTopicName));
if (active != null && active == '') {
active = null;
}

String? selected =
nt4Connection.getLastAnnouncedValue(selectedTopicName) as String?;
tryCast(nt4Connection.getLastAnnouncedValue(selectedTopicName));
if (selected != null && selected == '') {
selected = null;
}

String? defaultOption =
nt4Connection.getLastAnnouncedValue(defaultTopicName) as String?;

tryCast(nt4Connection.getLastAnnouncedValue(defaultTopicName));
if (defaultOption != null && defaultOption == '') {
defaultOption = null;
}
Expand Down
48 changes: 15 additions & 33 deletions lib/widgets/nt4_widgets/multi-topic/command_scheduler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
Expand Down Expand Up @@ -25,8 +26,8 @@ class CommandSchedulerWidget extends StatelessWidget with NT4Widget {

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

init();
}
Expand All @@ -53,18 +54,12 @@ class CommandSchedulerWidget extends StatelessWidget with NT4Widget {

void cancelCommand(int id) {
List<Object?> currentCancellationsRaw = nt4Connection
.getLastAnnouncedValue(cancelTopicName) as List<Object?>? ??
.getLastAnnouncedValue(cancelTopicName)
?.tryCast<List<Object?>>() ??
[];

List<int> currentCancellations = [];

for (Object? cancelID in currentCancellationsRaw) {
if (cancelID == null || cancelID is! int) {
continue;
}

currentCancellations.add(cancelID);
}
List<int> currentCancellations =
currentCancellationsRaw.whereType<int>().toList();

currentCancellations.add(id);

Expand All @@ -86,30 +81,17 @@ class CommandSchedulerWidget extends StatelessWidget with NT4Widget {
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
List<Object?> rawNames = nt4Connection
.getLastAnnouncedValue(namesTopicName) as List<Object?>? ??
[];
List<Object?> rawIds = nt4Connection.getLastAnnouncedValue(idsTopicName)
as List<Object?>? ??
.getLastAnnouncedValue(namesTopicName)
?.tryCast<List<Object?>>() ??
[];

List<String> names = [];
List<int> ids = [];

for (Object? name in rawNames) {
if (name == null || name is! String) {
continue;
}

names.add(name);
}

for (Object? id in rawIds) {
if (id == null || id is! int) {
continue;
}
List<Object?> rawIds = nt4Connection
.getLastAnnouncedValue(idsTopicName)
?.tryCast<List<Object?>>() ??
[];

ids.add(id);
}
List<String> names = rawNames.whereType<String>().toList();
List<int> ids = rawIds.whereType<int>().toList();

return Column(
mainAxisAlignment: MainAxisAlignment.start,
Expand Down
19 changes: 11 additions & 8 deletions lib/widgets/nt4_widgets/multi-topic/command_widget.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
Expand All @@ -22,8 +23,8 @@ class CommandWidget extends StatelessWidget with NT4Widget {
}

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

init();
}
Expand Down Expand Up @@ -53,12 +54,14 @@ class CommandWidget extends StatelessWidget with NT4Widget {
return StreamBuilder(
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
bool running =
nt4Connection.getLastAnnouncedValue(runningTopicName) as bool? ??
false;
String name =
nt4Connection.getLastAnnouncedValue(nameTopicName) as String? ??
'Unknown';
bool running = nt4Connection
.getLastAnnouncedValue(runningTopicName)
?.tryCast<bool>() ??
false;
String name = nt4Connection
.getLastAnnouncedValue(nameTopicName)
?.tryCast<String>() ??
'Unknown';

String buttonText = topic.substring(topic.lastIndexOf('/') + 1);

Expand Down
11 changes: 7 additions & 4 deletions lib/widgets/nt4_widgets/multi-topic/differential_drive.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math';

import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
Expand Down Expand Up @@ -36,8 +37,8 @@ class DifferentialDrive extends StatelessWidget with NT4Widget {

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

init();
}
Expand Down Expand Up @@ -73,10 +74,12 @@ class DifferentialDrive extends StatelessWidget with NT4Widget {
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
double leftSpeed = nt4Connection
.getLastAnnouncedValue(leftSpeedTopicName) as double? ??
.getLastAnnouncedValue(leftSpeedTopicName)
?.tryCast<double>() ??
0.0;
double rightSpeed = nt4Connection
.getLastAnnouncedValue(rightSpeedTopicName) as double? ??
.getLastAnnouncedValue(rightSpeedTopicName)
?.tryCast<double>() ??
0.0;

if (leftSpeed != leftSpeedPreviousValue) {
Expand Down
21 changes: 12 additions & 9 deletions lib/widgets/nt4_widgets/multi-topic/field_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math';

import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/field_images.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
Expand Down Expand Up @@ -52,16 +53,16 @@ class FieldWidget extends StatelessWidget with NT4Widget {
}

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

fieldGame = jsonData['field_game'] ?? fieldGame;
fieldGame = tryCast(jsonData['field_game']) ?? fieldGame;

robotWidthMeters = jsonData['robot_width'] ?? 0.82;
robotLengthMeters = jsonData['robot_length'] ?? 1.00;
robotWidthMeters = tryCast(jsonData['robot_width']) ?? 0.82;
robotLengthMeters = tryCast(jsonData['robot_length']) ?? 1.00;

showOtherObjects = jsonData['show_other_objects'] ?? true;
showTrajectories = jsonData['show_trajectories'] ?? true;
showOtherObjects = tryCast(jsonData['show_other_objects']) ?? true;
showTrajectories = tryCast(jsonData['show_trajectories']) ?? true;

init();
}
Expand Down Expand Up @@ -332,7 +333,8 @@ class FieldWidget extends StatelessWidget with NT4Widget {
}

List<Object?> robotPositionRaw = nt4Connection
.getLastAnnouncedValue(robotTopicName) as List<Object?>? ??
.getLastAnnouncedValue(robotTopicName)
?.tryCast<List<Object?>>() ??
[];

List<double>? robotPosition = [];
Expand Down Expand Up @@ -374,7 +376,8 @@ class FieldWidget extends StatelessWidget with NT4Widget {
if (showOtherObjects || showTrajectories) {
for (String objectTopic in otherObjectTopics) {
List<Object?>? objectPositionRaw = nt4Connection
.getLastAnnouncedValue(objectTopic) as List<Object?>?;
.getLastAnnouncedValue(objectTopic)
?.tryCast<List<Object?>>();

if (objectPositionRaw == null) {
continue;
Expand Down
20 changes: 11 additions & 9 deletions lib/widgets/nt4_widgets/multi-topic/fms_info.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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';
Expand Down Expand Up @@ -32,8 +33,8 @@ class FMSInfo extends StatelessWidget with NT4Widget {
}

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

init();
}
Expand Down Expand Up @@ -89,18 +90,19 @@ class FMSInfo extends StatelessWidget with NT4Widget {
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
String eventName =
nt4Connection.getLastAnnouncedValue(eventNameTopic) as String? ??
'';
tryCast(nt4Connection.getLastAnnouncedValue(eventNameTopic)) ?? '';
int controlData =
nt4Connection.getLastAnnouncedValue(controlDataTopic) as int? ?? 32;
tryCast(nt4Connection.getLastAnnouncedValue(controlDataTopic)) ??
32;
bool redAlliance =
nt4Connection.getLastAnnouncedValue(allianceTopic) as bool? ?? true;
tryCast(nt4Connection.getLastAnnouncedValue(allianceTopic)) ?? true;
int matchNumber =
nt4Connection.getLastAnnouncedValue(matchNumberTopic) as int? ?? 0;
tryCast(nt4Connection.getLastAnnouncedValue(matchNumberTopic)) ?? 0;
int matchType =
nt4Connection.getLastAnnouncedValue(matchTypeTopic) as int? ?? 0;
tryCast(nt4Connection.getLastAnnouncedValue(matchTypeTopic)) ?? 0;
int replayNumber =
nt4Connection.getLastAnnouncedValue(replayNumberTopic) as int? ?? 0;
tryCast(nt4Connection.getLastAnnouncedValue(replayNumberTopic)) ??
0;

String eventNameDisplay = '$eventName${(eventName != '') ? ' ' : ''}';
String matchTypeString = _getMatchTypeString(matchType);
Expand Down
10 changes: 6 additions & 4 deletions lib/widgets/nt4_widgets/multi-topic/gyro.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
Expand Down Expand Up @@ -34,9 +35,10 @@ class Gyro extends StatelessWidget with NT4Widget {
}

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

init();
}
Expand Down Expand Up @@ -109,7 +111,7 @@ class Gyro extends StatelessWidget with NT4Widget {
stream: valueSubscription.periodicStream(),
initialData: nt4Connection.getLastAnnouncedValue(valueTopic),
builder: (context, snapshot) {
double value = (snapshot.data as double?) ?? 0.0;
double value = tryCast(snapshot.data) ?? 0.0;

if (counterClockwisePositive) {
value *= -1;
Expand Down
Loading

0 comments on commit a1a988e

Please sign in to comment.