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");
}
}

View File

@@ -111,7 +111,7 @@ class AnnouncementModule extends InfoModule {
} else {
if (queue.isNotEmpty) {
await Future.delayed(const Duration(seconds: 5));
// await Future.delayed(const Duration(seconds: 2));
}
}
@@ -156,7 +156,7 @@ class AnnouncementModule extends InfoModule {
// Configuration
Duration get defaultAnnouncementDelay {
if (liveInformation.inRoom) {
return Duration(milliseconds: 500);
return Duration(milliseconds: 1000);
} else {
print("Not in room");
return Duration.zero;
@@ -182,7 +182,6 @@ class AnnouncementModule extends InfoModule {
for (var audioName in audioNames) {
audioNamesString += "\"$audioName\" ";
}
liveInformation.SendCommand("announce manual \"$displayText\" $audioNamesString ${scheduledTime.millisecondsSinceEpoch}");
queueAnnounceByAudioName(
displayText: displayText,

View File

@@ -0,0 +1,123 @@
import 'dart:async';
import 'package:bus_infotainment/backend/live_information.dart';
import 'package:flutter/foundation.dart';
import 'package:nearby_service/nearby_service.dart';
import 'package:nearby_service/src/model/model.dart';
class NearbyServiceWrapper {
late NearbyService _nearbyService;
final ValueNotifier<bool> isConnected = ValueNotifier(false);
final ValueNotifier<NearbyDevice?> connectedDevice = ValueNotifier(null);
NearbyServiceWrapper() {
_nearbyService = NearbyService.getInstance();
_initializeService();
}
Future<void> _initializeService() async {
await _nearbyService.initialize();
_listenForIncomingConnections();
await startDiscovery();
}
Future<void> startDiscovery() async {
await _nearbyService.discover();
}
Future<void> stopDiscovery() async {
await _nearbyService.stopDiscovery();
}
Future<List<NearbyDevice>> getDiscoveredDevices() async {
List<NearbyDevice> devices = await _nearbyService.getPeers();
// Remove devices that we should avoid
devices.removeWhere((element) => _deviceNamesToAvoid.any((avoid) => element.info.displayName.contains(avoid)));
return devices;
}
Future<void> connectToDevice(NearbyDevice device) async {
bool success = await _nearbyService.connect(device);
if (success) {
connectedDevice.value = device;
isConnected.value = true;
_startCommunicationChannel();
}
}
Future<void> _startCommunicationChannel() async {
await _nearbyService.startCommunicationChannel(
NearbyCommunicationChannelData(
connectedDevice.value!.info.id,
messagesListener: NearbyServiceMessagesListener(
onCreated: () {
print('Communication channel created');
},
onData: (ReceivedNearbyMessage<NearbyMessageContent> value) {
if (value.content is NearbyMessageTextRequest) {
String message = (value.content as NearbyMessageTextRequest).value;
print('Received message: ${message}');
LiveInformation().executeCommand(message);
}
}
)
)
);
}
Future<void> sendMessage(String message) async {
if (isConnected.value && connectedDevice.value != null) {
await _nearbyService.send(
OutgoingNearbyMessage(
content: NearbyMessageTextRequest.create(value: message),
receiver: connectedDevice.value!.info,
)
);
}
}
Future<void> disconnect() async {
if (isConnected.value && connectedDevice.value != null) {
await _nearbyService.disconnect(connectedDevice.value);
connectedDevice.value = null;
isConnected.value = false;
}
}
void _listenForIncomingConnections() {
_nearbyService.getPeersStream().listen((peers) async {
for (var peer in peers) {
// Lets avoid accidentally connecting to things
if (_deviceNamesToAvoid.any((element) => peer.info.displayName.contains(element))) {
print('Avoiding device: ${peer.info.displayName}');
continue;
}
if (!isConnected.value) {
await connectToDevice(peer);
print('Reconnected to: ${peer.info.displayName}');
}
}
});
_nearbyService.getConnectedDeviceStream(connectedDevice.value!).listen((device) {
if (device == null) {
isConnected.value = false;
connectedDevice.value = null;
}
});
}
ValueListenable<NearbyDevice?> get connectedDeviceNotifier => connectedDevice;
ValueListenable<bool> get isConnectedNotifier => isConnected;
}
// If a device name contains any of these strings, it will be avoided
List<String> _deviceNamesToAvoid = [
"DIRECT-", // Avoid connecting to printers
];

View File

@@ -9,11 +9,16 @@ import 'package:shelf_web_socket/shelf_web_socket.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:bus_infotainment/backend/modules/info_module.dart';
import 'package:bus_infotainment/utils/delegates.dart';
import 'package:http/http.dart' as http;
class NetworkingModule extends InfoModule {
// Host websocket server
String host = "ws://0.0.0.0:8080";
HttpServer? _server;
int webSocketPort = 8080;
int httpPort = 8081;
HttpServer? _sockerServer;
HttpServer? _httpServer;
WebSocketChannel? _channel;
// Store connected WebSocket channels
@@ -50,8 +55,13 @@ class NetworkingModule extends InfoModule {
});
});
_server = await io.serve(handler, InternetAddress.anyIPv4, 8080);
print('WebSocket server started at ${_server?.address.address}:${_server?.port}');
_sockerServer = await io.serve(handler, InternetAddress.anyIPv4, webSocketPort);
print('WebSocket server started at ${_sockerServer?.address.address}:${_sockerServer?.port}');
// Start Http api server
_httpServer = await io.serve((Request request) async {
return Response.ok('bus infotainment server');
}, InternetAddress.anyIPv4, httpPort);
return true;
} catch (e) {
@@ -61,7 +71,7 @@ class NetworkingModule extends InfoModule {
}
bool stopWebSocketServer() {
if (_server == null) {
if (_sockerServer == null) {
throw Exception('WebSocket server is not running');
}
@@ -70,8 +80,12 @@ class NetworkingModule extends InfoModule {
client.sink.close();
}
_connectedClients.clear();
_server?.close(force: true);
_server = null;
_sockerServer?.close(force: true);
_sockerServer = null;
_httpServer?.close(force: true);
_httpServer = null;
print('WebSocket server stopped');
return true;
} catch (e) {
@@ -82,6 +96,17 @@ class NetworkingModule extends InfoModule {
Future<bool> connectToWebSocketServer(String url) async {
try {
{
// Verify that the server we are connecting to is running, and is a bus infotainment server
var response = await http.get(Uri.parse('http://$url:$httpPort'));
if (response.statusCode != 200 || response.body != 'bus infotainment server') {
print('Server at $url is not a bus infotainment server');
return false;
}
}
_channel = await WebSocketChannel.connect(Uri.parse(url));
_channel?.stream.listen((message) {
// Handle messages from the server here
@@ -116,7 +141,7 @@ class NetworkingModule extends InfoModule {
bool sendMessage(String message) {
// If hosting a server, send message to all clients
if (_server != null) {
if (_sockerServer != null) {
return sendMessageToClients(message);
}
@@ -193,4 +218,4 @@ class NetworkingModule extends InfoModule {
}
}
}
}
}