All dem changes

This commit is contained in:
ImBenji
2024-05-24 20:17:02 +01:00
parent 2846a061cc
commit ffde62a730
13 changed files with 990 additions and 195 deletions

View File

@@ -10,6 +10,7 @@ import 'package:bus_infotainment/auth/api_constants.dart';
import 'package:bus_infotainment/auth/auth_api.dart';
import 'package:bus_infotainment/backend/modules/announcement.dart';
import 'package:bus_infotainment/backend/modules/commands.dart';
import 'package:bus_infotainment/backend/modules/directconnection.dart';
import 'package:bus_infotainment/backend/modules/networking.dart';
import 'package:bus_infotainment/backend/modules/synced_time.dart';
import 'package:bus_infotainment/backend/modules/tracker.dart';
@@ -24,6 +25,15 @@ import 'package:http/http.dart' as http;
import 'package:ntp/ntp.dart';
import 'package:permission_handler/permission_handler.dart';
enum RoomConnectionMethod {
Cloud,
Local,
P2P,
None
}
class LiveInformation {
static final LiveInformation _singleton = LiveInformation._internal();
@@ -39,11 +49,42 @@ class LiveInformation {
{
// By default, load the bus sequences from the assets
print("Loading bus sequences from assets");
String destinations = await rootBundle.loadString("assets/datasets/destinations.json");
String routes = await rootBundle.loadString("assets/datasets/bus-sequences.csv");
// Try to grab the routes from TfL
try {
http.Response response = await http.get(Uri.parse('https://tfl.gov.uk/bus-sequences.csv'));
routes = response.body;
print("Loaded bus sequences from TFL");
} catch (e) {
print("Failed to load bus sequences from TFL. Using local copy.");
}
// Try to grab the destinations from github
try {
http.Response response = await http.get(Uri.parse('https://raw.githubusercontent.com/RailboundStudios/LondonBusDatasets/main/destinations.json'));
destinations = response.body;
print("Loaded destinations from Github");
} catch (e) {
print("Failed to load destinations from Github. Using local copy.");
}
busSequences = BusSequences.fromCSV(
await rootBundle.loadString("assets/datasets/destinations.json"),
await rootBundle.loadString("assets/datasets/bus-sequences.csv")
destinations,
routes
);
print("Loaded bus sequences from assets");
print("Loaded all datasets");
try {
@@ -78,6 +119,11 @@ class LiveInformation {
}
networkingModule = NetworkingModule();
if (defaultTargetPlatform == TargetPlatform.android){
p2pModule = NearbyServiceWrapper();
}
}
Future<void> initTrackerModule() async {
@@ -86,6 +132,9 @@ class LiveInformation {
}
}
// Multi-device stuff
RoomConnectionMethod connectionMethod = RoomConnectionMethod.None;
// Auth
AuthAPI auth = AuthAPI(
autoLoad: false,
@@ -108,6 +157,7 @@ class LiveInformation {
late TrackerModule trackerModule;
late TubeStations tubeStations;
late NetworkingModule networkingModule;
late NearbyServiceWrapper p2pModule;
// Important variables
BusRouteVariant? _currentRouteVariant;
@@ -234,7 +284,7 @@ class LiveInformation {
_listenerReciept = networkingModule.onMessageReceived?.addListener(
(p0) {
print("Received local command: $p0");
ExecuteCommand(p0);
executeCommand(p0);
}
);
}
@@ -311,7 +361,9 @@ class LiveInformation {
}
Future<void> joinRoom(String infoJson) async {
Future<bool> joinRoom(String info) async {
String infoJson = utf8.decode(base64.decode(info));
try {
{
@@ -338,11 +390,12 @@ class LiveInformation {
_listenerReciept = networkingModule.onMessageReceived?.addListener(
(p0) {
print("Received local command: $p0");
ExecuteCommand(p0);
executeCommand(p0);
}
);
inRoom = true;
return; // We dont need to connect to the cloud room if we are connected to the local room.
connectionMethod = RoomConnectionMethod.Local;
return true; // We dont need to connect to the cloud room if we are connected to the local room.
} else {
print("Failed to connect to local room at $host");
print("Falling back to cloud room");
@@ -376,7 +429,7 @@ class LiveInformation {
);
if (response.documents.isEmpty) {
throw Exception("Room not found");
return false;
}
final document = response.documents.first;
@@ -417,7 +470,9 @@ class LiveInformation {
print("Failed to set route");
}
inRoom = true;
connectionMethod = RoomConnectionMethod.Cloud;
print("Joined cloud room with code $roomCode");
return true;
}
}
@@ -498,7 +553,7 @@ class LiveInformation {
}
*/
return jsonEncode({
String json = jsonEncode({
"cloud": {
"roomCode": roomCode,
},
@@ -512,6 +567,10 @@ class LiveInformation {
}
});
// Encode in base64
return base64Encode(utf8.encode(json));
}
String? lastCommand;
@@ -542,7 +601,9 @@ class LiveInformation {
// If the route arent the same, then update the route
if (routeNumber != _currentRouteVariant!.busRoute.routeNumber || routeVariantIndex != _currentRouteVariant!.busRoute.routeVariants.values.toList().indexOf(_currentRouteVariant!)) {
// Set the route
await setRouteVariantQuery(routeNumber, routeVariantIndex);
await setRouteVariantQuery(routeNumber, routeVariantIndex,
sendToServer: false
);
// announce the route
// announcementModule.queueAnnouncementByRouteVariant(routeVariant: _currentRouteVariant!);
@@ -554,14 +615,17 @@ class LiveInformation {
// Execute the command
List<String> commands = response.payload["Commands"].cast<String>();
ExecuteCommand(commands.last);
executeCommand(commands.last);
}
void ExecuteCommand(String command) {
void executeCommand(String command) {
if (command == lastCommand) {
return;
}
print("Executing command: $command");
lastCommand = command;
List<String> commandParts = _splitCommand(command);
@@ -636,10 +700,20 @@ class LiveInformation {
Future<void> SendCommand(String command) async {
{
// Wfi Direct Commands
p2pModule.sendMessage(command);
}
{
// Local Commands
networkingModule.sendMessage(command);
try {
networkingModule.sendMessage(command);
} catch (e) {
print("Failed to send local command: $e");
}
}