Skip to content

Commit

Permalink
Try to resend command if timeout (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxushka authored Oct 3, 2023
1 parent 23650eb commit 5d27b7c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 273 deletions.
5 changes: 4 additions & 1 deletion chameleonultragui/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ app.*.map.json
android/fastlane/report.xml
**/upload-keystore.jks
**/AuthKey*.p8
**/cert_key
**/cert_key

# Translation
untranslated_messages.json
24 changes: 22 additions & 2 deletions chameleonultragui/lib/bridge/chameleon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class ChameleonCommunicator {
int dataStatus = 0;
int dataLength = 0;
List<ChameleonMessage> messageQueue = [];
List<int> commandQueue = [];

final Logger log;

Expand Down Expand Up @@ -368,7 +369,8 @@ class ChameleonCommunicator {
Future<ChameleonMessage?> sendCmd(ChameleonCommand cmd,
{Uint8List? data,
Duration timeout = const Duration(seconds: 5),
bool skipReceive = false}) async {
bool skipReceive = false,
bool firstRun = false}) async {
var startTime = DateTime.now();
var dataFrame = makeDataFrameBytes(cmd, 0x00, data);

Expand All @@ -378,6 +380,17 @@ class ChameleonCommunicator {
_serialInstance!.isOpen = true;
}

while (commandQueue.contains(cmd.value)) {
if (startTime.millisecondsSinceEpoch + (timeout.inMilliseconds * 2) <
DateTime.now().millisecondsSinceEpoch) {
throw ("Timeout waiting for queue for command ${cmd.value}");
}

await asyncSleep(1);
}

commandQueue.add(cmd.value);

log.d("Sending: ${bytesToHex(dataFrame)}");

if (skipReceive) {
Expand All @@ -393,13 +406,20 @@ class ChameleonCommunicator {
for (var message in messageQueue) {
if (message.command == cmd.value) {
messageQueue.remove(message);
commandQueue.remove(cmd.value);
return message;
}
}

if (startTime.millisecondsSinceEpoch + timeout.inMilliseconds <
DateTime.now().millisecondsSinceEpoch) {
throw ("Timeout waiting for response for command ${cmd.value}");
commandQueue.remove(cmd.value);
if (firstRun) {
sendCmd(cmd, data: data, timeout: timeout, firstRun: false);
} else {
// no luck
throw ("Timeout waiting for response for command ${cmd.value}");
}
}

await asyncSleep(1);
Expand Down
1 change: 0 additions & 1 deletion chameleonultragui/lib/helpers/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:chameleonultragui/connector/serial_abstract.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

List<Map<String, String>> developers = [
{
Expand Down
4 changes: 2 additions & 2 deletions chameleonultragui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class _MainPageState extends State<MainPage> {
widget.sharedPreferencesProvider.getThemeColor(),
brightness: Brightness.light)
.surface,
statusBarBrightness: Brightness.dark,
statusBarBrightness: Brightness.light,
statusBarIconBrightness: Brightness.dark)),
),
darkTheme: ThemeData.dark().copyWith(
Expand All @@ -231,7 +231,7 @@ class _MainPageState extends State<MainPage> {
widget.sharedPreferencesProvider.getThemeColor(),
brightness: Brightness.dark)
.surface,
statusBarBrightness: Brightness.light,
statusBarBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.light)),
),
themeMode: widget.sharedPreferencesProvider.getTheme(), // Dark Theme
Expand Down
267 changes: 0 additions & 267 deletions chameleonultragui/untranslated_messages.json

This file was deleted.

0 comments on commit 5d27b7c

Please sign in to comment.