1. Log().debug('this transport not be realsed, will realse $uuid'); Do not use $uuid, which will cause an error:
NoSuchMethodError: Class 'Log' has no instance method 'call'.
2. A previous way of writing
try {
res = await callResponse.timeout(Duration(seconds: 3)).firstWhere((r) => r.isNotEmpty && r[3] == params.opcode);
} on TimeoutException catch (e) {
Log.error('请求超时: $e');
}
return getCallResInfo(res, params);
Timer timer = Timer(Duration(seconds: 5), () {
subscription.cancel();
if (!completer.isCompleted) {
Log.error('请求超时: Stream was closed due to timeout = 5s.');
completer.complete(res);
}
});
subscription = callResponse.listen((r) {
print('gs.response :$r');
if (r.length > 3 && r[3] == params.opcode!) {
timer.cancel(); // 取消计时器
subscription.cancel(); // 取消订阅
res = getCallResInfo(r, params);
completer.complete(res);
}
});
Combining the two usages
It can be used, but the effect is not good, discard it;
The final choice is:
try{
print('call 无法进行数据处理 之前的configuredDate');
final value = configuredDate(data as CallParams);
// await transport.nofitySubscription!;
transport.writeCharacteristic.write(value);
res = await getCallResponse(data as CallParams);
}catch(e){
res = CallResInfo(false, null, null);
Log.error('获取数据有误: ',e);
}finally{
runCompleter = null;
}
return {'res':'res'};
}
Future<CallResInfo> getCallResponse(CallParams params) async{
late StreamSubscription subscription;
var completer = Completer<CallResInfo>();
CallResInfo res = CallResInfo(false, null, null);
var r = await runCompleter!.future;
if (r.length > 3 && r[3] == params.opcode!) {
res = getCallResInfo(r, params);
completer.complete(res);
}else{
Log.error('runCompleter: wrong .');
}
return completer.future; // 添加返回语句
}