Skip to content

Commit

Permalink
remove classic automove and listen for response before sending command
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed May 22, 2024
1 parent 5927e0b commit 4ab288c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 108 deletions.
39 changes: 16 additions & 23 deletions lib/Backend/Definitions/Device/device_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import 'package:tail_app/Backend/firmware_update.dart';
import '../../../Frontend/intn_defs.dart';
import '../../../Frontend/utils.dart';
import '../../Bluetooth/bluetooth_message.dart';
import '../../auto_move.dart';

part 'device_definition.g.dart';

Expand Down Expand Up @@ -120,9 +119,6 @@ class BaseStatefulDevice extends ChangeNotifier {
Future.delayed(const Duration(seconds: 5), () {
commandQueue.addCommand(BluetoothMessage(message: "VER", device: this, priority: Priority.low, type: Type.system, responseMSG: "VER "));
commandQueue.addCommand(BluetoothMessage(message: "HWVER", device: this, priority: Priority.low, type: Type.system, responseMSG: "HWVER "));
if (baseStoredDevice.autoMove) {
changeAutoMove(this);
}
});
}
});
Expand Down Expand Up @@ -219,16 +215,6 @@ extension AutoActionCategoryExtension on AutoActionCategory {
class BaseStoredDevice extends ChangeNotifier {
@HiveField(0)
String name = "New Gear";
@HiveField(1)
bool autoMove = false;
@HiveField(2)
double autoMoveMinPause = 15;
@HiveField(3)
double autoMoveMaxPause = 240;
@HiveField(4)
double autoMoveTotal = 60;
@HiveField(5)
double noPhoneDelayTime = 1;
@HiveField(6)
List<AutoActionCategory> selectedAutoCategories = [AutoActionCategory.calm];
@HiveField(7)
Expand Down Expand Up @@ -305,24 +291,29 @@ class CommandQueue {
try {
bluetoothLog.fine("Sending command to ${device.baseStoredDevice.name}:${message.message}");
await sendMessage(device, const Utf8Encoder().convert(message.message));
Future<String>? response;
//Start listening before the response is received
Duration timeoutDuration = const Duration(seconds: 10);
if (message.responseMSG != null) {
// We use a timeout as sometimes a response isn't sent by the gear
response = message.device.rxCharacteristicStream.timeout(timeoutDuration, onTimeout: (sink) => sink.close()).where((event) {
bluetoothLog.info('Response:$event');
return event.contains(message.responseMSG!);
}).first;
}

device.messageHistory.add(MessageHistoryEntry(type: MessageHistoryType.send, message: message.message));
if (message.onCommandSent != null) {
// Callback when the specific command is run
message.onCommandSent!();
}
try {
if (message.responseMSG != null) {
Duration timeoutDuration = const Duration(seconds: 10);
bluetoothLog.fine("Waiting for response from ${device.baseStoredDevice.name}:${message.responseMSG}");
Timer timer = Timer(timeoutDuration, () {});

// We use a timeout as sometimes a response isn't sent by the gear
Future<String> response = message.device.rxCharacteristicStream.timeout(timeoutDuration, onTimeout: (sink) => sink.close()).where((event) {
bluetoothLog.info('Response:$event');
return event.contains(message.responseMSG!);
}).first;
// Handles response value
response.then((value) {
response!.then((value) {
timer.cancel();
if (message.onResponseReceived != null) {
//callback when the command response is received
Expand All @@ -341,11 +332,13 @@ class CommandQueue {
if (state.isNotEmpty && state.first.priority.index > bluetoothMessage.priority.index) {
timer.cancel();
}
await Future.delayed(const Duration(milliseconds: 50)); // Prevent the loop from consuming too many resources
await Future.delayed(const Duration(milliseconds: 100)); // Prevent the loop from consuming too many resources
}
bluetoothLog.fine("Finished waiting for response from ${device.baseStoredDevice.name}:${message.responseMSG}");
} else {
await Future.delayed(const Duration(milliseconds: 200));
if (device.baseDeviceDefinition.deviceType == DeviceType.ears) {
await Future.delayed(const Duration(milliseconds: 200)); // delay before the next command can be sent
}
}
} catch (e, s) {
bluetoothLog.warning('Command timed out or threw error: $e', e, s);
Expand Down
24 changes: 0 additions & 24 deletions lib/Backend/auto_move.dart

This file was deleted.

64 changes: 3 additions & 61 deletions lib/Frontend/pages/shell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:tail_app/Frontend/Widgets/known_gear_scan_controller.dart';
import 'package:tail_app/Frontend/Widgets/snack_bar_overlay.dart';
import 'package:upgrader/upgrader.dart';

import '../../Backend/auto_move.dart';
import '../../constants.dart';
import '../../main.dart';
import '../Widgets/known_gear.dart';
Expand Down Expand Up @@ -344,67 +343,10 @@ class _ManageGearState extends ConsumerState<ManageGear> {
).whenComplete(() => setState(() {}));
},
),
const ListTile(
title: Divider(),
dense: true,
),
ListTile(
title: Text(manageDevicesAutoMoveTitle()),
subtitle: Text(manageDevicesAutoMoveSubTitle()),
trailing: Switch(
value: widget.device.baseStoredDevice.autoMove,
onChanged: (bool value) {
setState(() {
widget.device.baseStoredDevice.autoMove = value;
});
widget.ref.read(knownDevicesProvider.notifier).store();
changeAutoMove(widget.device);
},
),
),
ListTile(
title: Text(manageDevicesAutoMoveGroupsTitle()),
subtitle: SegmentedButton<AutoActionCategory>(
multiSelectionEnabled: true,
selected: widget.device.baseStoredDevice.selectedAutoCategories.toSet(),
onSelectionChanged: (Set<AutoActionCategory> value) {
setState(() {
widget.device.baseStoredDevice.selectedAutoCategories = value.toList();
});
widget.ref.read(knownDevicesProvider.notifier).store();
changeAutoMove(widget.device);
},
segments: AutoActionCategory.values.map<ButtonSegment<AutoActionCategory>>(
(AutoActionCategory value) {
return ButtonSegment<AutoActionCategory>(
value: value,
label: Text(value.friendly),
);
},
).toList(),
),
),
ListTile(
title: Text(manageDevicesAutoMovePauseTitle()),
subtitle: RangeSlider(
labels: RangeLabels("${widget.device.baseStoredDevice.autoMoveMinPause.round()}", "${widget.device.baseStoredDevice.autoMoveMaxPause.round()}"),
min: 15,
max: 240,
divisions: 225,
values: RangeValues(widget.device.baseStoredDevice.autoMoveMinPause, widget.device.baseStoredDevice.autoMoveMaxPause),
onChanged: (RangeValues value) {
setState(() {
widget.device.baseStoredDevice.autoMoveMinPause = value.start;
widget.device.baseStoredDevice.autoMoveMaxPause = value.end;
});
widget.ref.read(knownDevicesProvider.notifier).store();
},
onChangeEnd: (values) {
changeAutoMove(widget.device);
},
),
),
if (SentryHive.box(settings).get(showDebugging, defaultValue: showDebuggingDefault)) ...[
const ListTile(
title: Divider(),
),
ListTile(
title: const Text("Debug"),
subtitle: Column(
Expand Down

0 comments on commit 4ab288c

Please sign in to comment.