From 785f8b2e08fecab7824800dc269a50f9ade34e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles=20Kr=C3=BCger?= Date: Mon, 27 Nov 2023 13:30:20 +0100 Subject: [PATCH] stash --- lib/positioning/services/positioning.dart | 2 +- lib/settings/views/main.dart | 2 +- lib/simulator/services/simulator.dart | 35 +++++++++++++++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/positioning/services/positioning.dart b/lib/positioning/services/positioning.dart index f3953ad93..92266847c 100644 --- a/lib/positioning/services/positioning.dart +++ b/lib/positioning/services/positioning.dart @@ -211,7 +211,7 @@ class Positioning with ChangeNotifier { if (!isGeolocating) return; lastPosition = position; - if (settings.enableSimulatorMode) simulator.sendCurrentPosition(); + if (settings.enableSimulatorMode && !simulator.receivedStopRide) simulator.sendCurrentPosition(); positions.add(position); // Snap the position to the route. diff --git a/lib/settings/views/main.dart b/lib/settings/views/main.dart index 2513ad616..5f8b147d7 100644 --- a/lib/settings/views/main.dart +++ b/lib/settings/views/main.dart @@ -395,7 +395,7 @@ class SettingsViewState extends State { ), const VSpace(), SettingsElement( - title: "Simulator aktivieren ${getIt().deviceId}", + title: "Simulator aktivieren (ID: ${getIt().deviceId})", icon: settings.enableSimulatorMode ? Icons.check_box : Icons.check_box_outline_blank, callback: () => settings.setSimulatorMode(!settings.enableSimulatorMode), ), diff --git a/lib/simulator/services/simulator.dart b/lib/simulator/services/simulator.dart index 4a42fe23e..39905c5cd 100644 --- a/lib/simulator/services/simulator.dart +++ b/lib/simulator/services/simulator.dart @@ -18,8 +18,8 @@ class Simulator { /// The mqtt client. MqttServerClient? client; - /// The unique key to identify the device in the simulator. - final deviceId = UniqueKey().toString(); + /// The unique key to identify the device in the simulator. Remove the brackets and hash sign. + final deviceId = UniqueKey().toString().replaceAll("[", "").replaceAll("]", "").replaceAll("#", ""); /// Whether the device received a successful pair response from the simulator. bool pairSuccessful = false; @@ -30,6 +30,12 @@ class Simulator { /// The last time a pair request was sent to the simulator. DateTime? lastSendPairRequest; + /// Whether the device received a stop ride message from the simulator, indicating the simulation needs to be stopped. + bool receivedStopRide = false; + + /// The subscription for the MQTT messages. + Subscription? subscription; + askForPermission() { // TODO: implement askForPermission } @@ -67,8 +73,6 @@ class Simulator { message: message, qualityOfService: qualityOfService, ); - - client?.subscribe(topic, MqttQos.atLeastOnce); } /// Sends a start ride message to the simulator via MQTT. @@ -169,6 +173,8 @@ class Simulator { final data = MqttPublishPayload.bytesToStringAsString(recMess.payload.message); final json = jsonDecode(data); log.i("Received for simulator: $json"); + + // Paring if (json['type'] == 'PairStart' && json['deviceID'] == deviceId) { pairSuccessful = true; log.i("Pairing with simulator successful."); @@ -181,11 +187,19 @@ class Simulator { qualityOfService: qualityOfService, ); } + + // Stop ride + if (json['type'] == 'StopRide' && json['deviceID'] == deviceId) { + log.i("Stop ride received from simulator."); + receivedStopRide = true; + } } } /// Connects the MQTT client to the simulator. Future connectMQTTClient() async { + resetVariables(); + // Get the backend that is currently selected. final settings = getIt(); @@ -229,6 +243,7 @@ class Simulator { client!.updates?.listen(onData); await sendReadyPairRequest(); + subscription = client?.subscribe(topic, MqttQos.atLeastOnce); } catch (e, stacktrace) { client = null; final hint = "Failed to connect the simulator MQTT client: $e, $stacktrace"; @@ -244,15 +259,23 @@ class Simulator { } } + /// Disconnects the MQTT client from the simulator. Future disconnectMQTTClient() async { if (client != null) { await sendStopRide(); client!.unsubscribe(topic); client!.disconnect(); client = null; - pairSuccessful = false; - lastSendPairRequest = null; + resetVariables(); log.i("Disconnected from simulator MQTT broker."); } } + + /// Resets the variables for the simulation. + resetVariables() { + pairSuccessful = false; + lastSendPairRequest = null; + receivedStopRide = false; + subscription = null; + } }