near final
This commit is contained in:
@@ -3,24 +3,64 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:bus_infotainment/audio_cache.dart';
|
||||
import 'package:bus_infotainment/backend/live_information.dart';
|
||||
import 'package:bus_infotainment/backend/modules/info_module.dart';
|
||||
import 'package:bus_infotainment/utils/NameBeautify.dart';
|
||||
import 'package:bus_infotainment/utils/OrdinanceSurveyUtils.dart';
|
||||
import 'package:csv/csv.dart';
|
||||
import 'package:vector_math/vector_math.dart';
|
||||
|
||||
class BusSequences {
|
||||
class BusSequences extends InfoModule {
|
||||
|
||||
Map<String, BusRoute> routes = {};
|
||||
|
||||
BusSequences.fromCSV(String csv) {
|
||||
Map<String, BusDestination> destinations = {};
|
||||
|
||||
List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(csv);
|
||||
BusSequences.fromCSV(String destinationsCSV, String busSequencesCSV) {
|
||||
|
||||
rowsAsListOfValues.removeAt(0);
|
||||
|
||||
for (int i = 0; i < rowsAsListOfValues.length; i++) {
|
||||
// Init the bus destinations
|
||||
List<List<dynamic>> destinationRows = const CsvToListConverter().convert(destinationsCSV);
|
||||
destinationRows.removeAt(0);
|
||||
|
||||
for (int i = 0; i < destinationRows.length; i++) {
|
||||
try {
|
||||
|
||||
List<dynamic> entries = rowsAsListOfValues[i];
|
||||
List<dynamic> entries = destinationRows[i];
|
||||
|
||||
String routeNumber = entries[0].toString();
|
||||
|
||||
BusRoute route = routes.containsKey(routeNumber) ? routes[routeNumber]! : BusRoute(routeNumber: routeNumber);
|
||||
|
||||
String blind = entries[1].toString();
|
||||
|
||||
double lat = double.parse(entries[2].toString());
|
||||
double long = double.parse(entries[3].toString());
|
||||
|
||||
Vector2 grid = OSGrid.toNorthingEasting(lat, long);
|
||||
|
||||
BusDestination destination = BusDestination();
|
||||
destination.destination = blind;
|
||||
destination.easting = grid.x;
|
||||
destination.northing = grid.y;
|
||||
|
||||
route.destinations.add(destination);
|
||||
|
||||
routes[routeNumber] = route;
|
||||
destinations[blind] = destination;
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
// Init the bus routes
|
||||
|
||||
List<List<dynamic>> busSequenceRows = const CsvToListConverter().convert(busSequencesCSV);
|
||||
busSequenceRows.removeAt(0);
|
||||
|
||||
for (int i = 0; i < busSequenceRows.length; i++) {
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
List<dynamic> entries = busSequenceRows[i];
|
||||
|
||||
String routeNumber = entries[0].toString();
|
||||
|
||||
@@ -28,25 +68,28 @@ class BusSequences {
|
||||
|
||||
int routeVariant = entries[1];
|
||||
|
||||
BusRouteVariant variant = route.routeVariants.containsKey(routeVariant) ? route.routeVariants[routeVariant]! : BusRouteVariant(routeVariant: routeVariant, busRoute: route);
|
||||
|
||||
BusRouteStops stop = BusRouteStops();
|
||||
BusRouteStop stop = BusRouteStop();
|
||||
|
||||
stop.stopName = entries[6].toString();
|
||||
stop.stopCode = entries[4].toString();
|
||||
stop.easting = entries[7];
|
||||
stop.northing = entries[8];
|
||||
stop.heading = entries[9] != "" ? entries[9] : -1;
|
||||
|
||||
BusRouteVariant variant = route.routeVariants.containsKey(routeVariant) ? route.routeVariants[routeVariant]! : BusRouteVariant(routeVariant: routeVariant, busRoute: route);
|
||||
|
||||
variant.busStops.add(stop);
|
||||
|
||||
|
||||
route.routeVariants[routeVariant] = variant;
|
||||
routes[routeNumber] = route;
|
||||
|
||||
} catch (e) {
|
||||
// print("Error parsing bus sequence: $e");
|
||||
}
|
||||
|
||||
catch (e) {
|
||||
print("Error parsing bus sequence: $e");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,6 +98,8 @@ class BusRoute {
|
||||
|
||||
String routeNumber = "";
|
||||
|
||||
List<BusDestination> destinations = [];
|
||||
|
||||
AnnouncementCache? announcementCache;
|
||||
|
||||
Map<int, BusRouteVariant> routeVariants = {};
|
||||
@@ -70,7 +115,7 @@ class BusRouteVariant {
|
||||
|
||||
int routeVariant = -1;
|
||||
|
||||
List<BusRouteStops> busStops = [];
|
||||
List<BusRouteStop> busStops = [];
|
||||
|
||||
late BusRoute busRoute;
|
||||
|
||||
@@ -78,14 +123,66 @@ class BusRouteVariant {
|
||||
this.routeVariant = -1,
|
||||
required this.busRoute,
|
||||
});
|
||||
|
||||
BusDestination? _destination;
|
||||
BusDestination? get destination {
|
||||
if (_destination == null) {
|
||||
|
||||
// Get the nearest destination
|
||||
BusDestination? nearestDestinationA;
|
||||
double nearestDistanceA = double.infinity;
|
||||
|
||||
Vector2 stopLocation = Vector2(busStops.last.northing.toDouble(), busStops.last.easting.toDouble());
|
||||
|
||||
for (BusDestination destination in busRoute.destinations) {
|
||||
|
||||
Vector2 destinationLocation = Vector2(destination.northing.toDouble(), destination.easting.toDouble());
|
||||
|
||||
double distance = stopLocation.distanceTo(destinationLocation);
|
||||
|
||||
if (distance < nearestDistanceA) {
|
||||
nearestDestinationA = destination;
|
||||
nearestDistanceA = distance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// get the nearest destination from global pool of destinations
|
||||
BusDestination? nearestDestinationB;
|
||||
double nearestDistanceB = double.infinity;
|
||||
|
||||
for (BusDestination destination in LiveInformation().busSequences.destinations.values) {
|
||||
|
||||
Vector2 destinationLocation = Vector2(destination.northing.toDouble(), destination.easting.toDouble());
|
||||
|
||||
double distance = stopLocation.distanceTo(destinationLocation);
|
||||
|
||||
if (distance < nearestDistanceB) {
|
||||
nearestDestinationB = destination;
|
||||
nearestDistanceB = distance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Choose the nearest destination
|
||||
if (nearestDistanceA < nearestDistanceB) {
|
||||
_destination = nearestDestinationA;
|
||||
} else {
|
||||
_destination = nearestDestinationB;
|
||||
}
|
||||
|
||||
}
|
||||
return _destination;
|
||||
}
|
||||
}
|
||||
|
||||
class BusRouteStops {
|
||||
class BusRouteStop {
|
||||
|
||||
String stopName = "";
|
||||
|
||||
int easting = -1;
|
||||
int northing = -1;
|
||||
int heading = -1;
|
||||
|
||||
String stopCode = "";
|
||||
|
||||
@@ -122,4 +219,60 @@ class BusRouteStops {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class BusDestination {
|
||||
|
||||
String destination = "";
|
||||
|
||||
double easting = -1;
|
||||
double northing = -1;
|
||||
|
||||
Future<Uint8List> getAudioBytes() async {
|
||||
|
||||
// Convert the stop name to all caps
|
||||
String name = destination.toUpperCase();
|
||||
|
||||
name = NameBeautify.beautifyStopName(name);
|
||||
|
||||
// replace & with N
|
||||
name = name.replaceAll('&', 'N');
|
||||
|
||||
name = name.replaceAll('/', '');
|
||||
|
||||
name = name.replaceAll('\'', '');
|
||||
|
||||
name = name.replaceAll(' ', ' ');
|
||||
|
||||
// Replace space with underscore
|
||||
name = name.replaceAll(' ', '_');
|
||||
|
||||
// convert to all caps
|
||||
name = name.toUpperCase();
|
||||
|
||||
String audioNameA = "D_${name}_001.mp3";
|
||||
String audioNameB = "S_${name}_001.mp3";
|
||||
String audioNameC = "A_${name}_001.mp3";
|
||||
|
||||
print("Audio name A: $audioNameA");
|
||||
print("Audio name B: $audioNameB");
|
||||
print("Audio name C: $audioNameC");
|
||||
|
||||
await LiveInformation().announcementModule.announcementCache.loadAnnouncements([audioNameA, audioNameB, audioNameC]);
|
||||
|
||||
Uint8List? audioBytesA = LiveInformation().announcementModule.announcementCache[audioNameA];
|
||||
Uint8List? audioBytesB = LiveInformation().announcementModule.announcementCache[audioNameB];
|
||||
Uint8List? audioBytesC = LiveInformation().announcementModule.announcementCache[audioNameC];
|
||||
|
||||
if (audioBytesA != null) return audioBytesA;
|
||||
if (audioBytesB != null) return audioBytesB;
|
||||
if (audioBytesC != null) return audioBytesC;
|
||||
|
||||
print("No audio bytes found for $name");
|
||||
|
||||
return Uint8List(0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user