All dem changes
This commit is contained in:
@@ -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,
|
||||
|
||||
123
lib/backend/modules/directconnection.dart
Normal file
123
lib/backend/modules/directconnection.dart
Normal 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
|
||||
];
|
||||
@@ -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 {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user