Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
kruegercharles committed Nov 27, 2023
1 parent fa52d29 commit 785f8b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/positioning/services/positioning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/views/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class SettingsViewState extends State<SettingsView> {
),
const VSpace(),
SettingsElement(
title: "Simulator aktivieren ${getIt<Simulator>().deviceId}",
title: "Simulator aktivieren (ID: ${getIt<Simulator>().deviceId})",
icon: settings.enableSimulatorMode ? Icons.check_box : Icons.check_box_outline_blank,
callback: () => settings.setSimulatorMode(!settings.enableSimulatorMode),
),
Expand Down
35 changes: 29 additions & 6 deletions lib/simulator/services/simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.");
Expand All @@ -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<void> connectMQTTClient() async {
resetVariables();

// Get the backend that is currently selected.
final settings = getIt<Settings>();

Expand Down Expand Up @@ -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";
Expand All @@ -244,15 +259,23 @@ class Simulator {
}
}

/// Disconnects the MQTT client from the simulator.
Future<void> 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;
}
}

0 comments on commit 785f8b2

Please sign in to comment.