shtuff
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
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/tube_info.dart';
|
||||
import 'package:bus_infotainment/tfl_datasets.dart';
|
||||
import 'package:bus_infotainment/utils/audio%20wrapper.dart';
|
||||
import 'package:bus_infotainment/utils/delegates.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'info_module.dart';
|
||||
|
||||
@@ -33,12 +36,26 @@ class AnnouncementModule extends InfoModule {
|
||||
return _bundleBytes!;
|
||||
} else {
|
||||
|
||||
if (kIsWeb) {
|
||||
throw Exception("Cannot load bundle bytes on web");
|
||||
// Try to load them from shared preferences
|
||||
try {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
String fileLocation = prefs.getString("AnnouncementsFileLocation")!;
|
||||
|
||||
File file = File(fileLocation);
|
||||
setBundleBytes(file.readAsBytesSync());
|
||||
return _bundleBytes!;
|
||||
} catch (e) {
|
||||
throw Exception("Loading announcements from assets has been deprecated.");
|
||||
}
|
||||
|
||||
final bytes = await rootBundle.load(_bundleLocation);
|
||||
return bytes.buffer.asUint8List();
|
||||
|
||||
|
||||
// if (kIsWeb) {
|
||||
// throw Exception("Cannot load bundle bytes on web");
|
||||
// }
|
||||
//
|
||||
// final bytes = await rootBundle.load(_bundleLocation);
|
||||
// return bytes.buffer.asUint8List();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -274,11 +291,21 @@ class AnnouncementModule extends InfoModule {
|
||||
|
||||
String audioRoute = "R_${routeVariant.busRoute.routeNumber}_001.mp3";
|
||||
print("Checkpoint 5");
|
||||
await announcementCache.loadAnnouncementsFromBytes(await getBundleBytes(), [audioRoute, "R_RAIL_REPLACEMENT_SERVICE_001.mp3"]);
|
||||
await announcementCache.loadAnnouncementsFromBytes(await getBundleBytes(), [audioRoute]);
|
||||
print("Checkpoint 6");
|
||||
AudioWrapperSource sourceRoute = !routeNumber.toLowerCase().startsWith("ul") ?
|
||||
AudioWrapperByteSource(announcementCache[audioRoute]!) :
|
||||
AudioWrapperByteSource(announcementCache["R_RAIL_REPLACEMENT_SERVICE_001.mp3"]!);
|
||||
// AudioWrapperByteSource(announcementCache["R_RAIL_REPLACEMENT_SERVICE_001.mp3"]!);
|
||||
AudioWrapperAssetSource("audio/R_RAIL_REPLACEMENT_SERVICE_001.mp3");
|
||||
|
||||
if (routeNumber.toLowerCase().startsWith("ul")) {
|
||||
|
||||
TubeLine? closestLine = liveInformation.tubeStations.getClosestLine(routeVariant);
|
||||
|
||||
sourceRoute = closestLine?.getAudio() ?? sourceRoute;
|
||||
routeNumber = closestLine?.getShortName() ?? routeNumber;
|
||||
}
|
||||
|
||||
print("Checkpoint 6.1");
|
||||
AudioWrapperSource sourceDestination = AudioWrapperByteSource(await routeVariant.destination!.getAudioBytes());
|
||||
print("Checkpoint 7");
|
||||
|
||||
@@ -90,7 +90,9 @@ class TrackerModule extends InfoModule {
|
||||
|
||||
int stopIndex = liveInformation.getRouteVariant()!.busStops.indexOf(closestStop);
|
||||
|
||||
closestStop = liveInformation.getRouteVariant()!.busStops[stopIndex + 1];
|
||||
int maxStops = liveInformation.getRouteVariant()!.busStops.length;
|
||||
|
||||
closestStop = liveInformation.getRouteVariant()!.busStops[min(stopIndex + 1, maxStops)];
|
||||
|
||||
print("Closest stop is now: ${closestStop.formattedStopName}");
|
||||
} else {
|
||||
|
||||
133
lib/backend/modules/tube_info.dart
Normal file
133
lib/backend/modules/tube_info.dart
Normal file
@@ -0,0 +1,133 @@
|
||||
|
||||
|
||||
import 'package:bus_infotainment/utils/OrdinanceSurveyUtils.dart';
|
||||
import 'package:bus_infotainment/utils/audio%20wrapper.dart';
|
||||
import 'package:vector_math/vector_math.dart';
|
||||
|
||||
import '../../tfl_datasets.dart';
|
||||
|
||||
class TubeStations {
|
||||
|
||||
List<TubeStation> stations = [];
|
||||
List<TubeLine> lines = [];
|
||||
|
||||
TubeStations.fromJson(Map<String, dynamic> json) {
|
||||
/*
|
||||
Example:
|
||||
"Acton Town": {
|
||||
"lines": [
|
||||
"District",
|
||||
"Piccadilly"
|
||||
],
|
||||
"location": {
|
||||
"lat": 51.5029984,
|
||||
"lng": -0.2803455
|
||||
}
|
||||
},
|
||||
*/
|
||||
|
||||
for (String stationName in json.keys) {
|
||||
TubeStation station = TubeStation();
|
||||
station.name = stationName;
|
||||
|
||||
Map<String, dynamic> stationData = json[stationName];
|
||||
|
||||
station.latitude = stationData["location"]["lat"];
|
||||
station.longitude = stationData["location"]["lng"];
|
||||
|
||||
for (String lineName in stationData["lines"]) {
|
||||
|
||||
// check if the line already exists
|
||||
TubeLine line = lines.firstWhere((element) => element.name == lineName, orElse: () => TubeLine()..name = lineName);
|
||||
|
||||
if (!lines.contains(line)) {
|
||||
lines.add(line);
|
||||
}
|
||||
|
||||
station.lines.add(line);
|
||||
}
|
||||
|
||||
stations.add(station);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Get the london underground line that is a close match to a given bus route variant
|
||||
TubeLine? getClosestLine(BusRouteVariant variant) {
|
||||
|
||||
Map<TubeLine, int> lineMatches = {};
|
||||
|
||||
for (TubeLine line in lines) {
|
||||
lineMatches[line] = 0;
|
||||
}
|
||||
|
||||
for (BusRouteStop stop in variant.busStops) {
|
||||
for (TubeStation station in stations) {
|
||||
|
||||
// get the distance between the bus stop and the tube station
|
||||
double distance = Vector2(stop.easting.toDouble(), stop.northing.toDouble()).distanceTo(OSGrid.toNorthingEasting(station.latitude, station.longitude));
|
||||
|
||||
// if the distance is less than 100m, then we can assume that the bus stop is near the tube station
|
||||
if (distance < 200) {
|
||||
for (TubeLine line in station.lines) {
|
||||
lineMatches[line] = lineMatches[line]! + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TubeLine? closestLine;
|
||||
int closestMatch = 0;
|
||||
for (TubeLine line in lineMatches.keys) {
|
||||
if (lineMatches[line]! > closestMatch) {
|
||||
closestLine = line;
|
||||
closestMatch = lineMatches[line]!;
|
||||
}
|
||||
}
|
||||
|
||||
return closestLine;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TubeStation {
|
||||
|
||||
late final String name;
|
||||
|
||||
late final double latitude;
|
||||
late final double longitude;
|
||||
|
||||
List<TubeLine> lines = [];
|
||||
|
||||
}
|
||||
|
||||
class TubeLine {
|
||||
|
||||
late final String name;
|
||||
|
||||
AudioWrapperSource? getAudio() {
|
||||
|
||||
String lineName = name.toLowerCase().replaceAll(" ", "_");
|
||||
lineName = lineName.replaceAll("and", "N");
|
||||
lineName = lineName.toUpperCase();
|
||||
|
||||
return AudioWrapperAssetSource("audio/rail_replacement/$lineName.mp3");
|
||||
}
|
||||
|
||||
// Get short name
|
||||
// example: "District" -> "DIS", "London Overground" -> "LO", "TfL Rail" -> "TFL", "Waterloo and City" -> "W&C"
|
||||
String getShortName() {
|
||||
String shortName = name;
|
||||
|
||||
List<String> words = shortName.split(" ");
|
||||
shortName = words.map((e) => e[0]).join("");
|
||||
|
||||
if (shortName.length < 3) {
|
||||
shortName = name.substring(0, 3).toUpperCase();
|
||||
}
|
||||
|
||||
return shortName;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user