app-starlock/star_lock/lib/command/sender_data.dart
2023-07-27 15:29:37 +08:00

107 lines
3.3 KiB
Dart

import '../app_settings/app_settings.dart';
import 'io_protocol/io_sender.dart';
import 'io_protocol/io_type.dart';
import 'io_tool/io_model.dart';
import 'io_tool/manager_event_bus.dart';
typedef CommandSendCallBack = void Function(ErrorType errorType);
class CommandSenderManager {
static final CommandSenderManager _manager = CommandSenderManager
._init();
factory CommandSenderManager()=>_manager;
static CommandSenderManager getInstance()=>_manager;
CommandSenderManager._init(){
init();
}
init(){
}
CommandType lastCommandType = CommandType.readLockStatusInfo;
bool canSendControlCommand = false;
//TODO:发送常规数据
void managerSendData ({
required SenderProtocol command,
CommandSendCallBack? callBack}) {
if (callBack != null && command.commandType != CommandType.readLockStatusInfo) {
// if (!BluetoothManager().connected) {
print('❌ 蓝牙断开了');
if (callBack != null) {
print('managerSendData ❌ callBack');
// EasyLoading.dismiss();
callBack(ErrorType.notConnected);
}
return;
}
List<int> value = command.packageData();
_sendNormalData(value);
// startCommandCutDown(command.commandType, value, callBack);
}
void _sendNormalData(List<int> data) async {
if (data != null && data.isNotEmpty) {
EventBusManager().eventBusFir(EventSendModel(data: data, sendChannel: DataChannel.ble));
}
}
// Timer? _commandTimer;
// List<int>? bufferList = [];
// int? outTimeCount = 1;
// CommandType? sendCommandType; //发送指令类型
//
// void startCommandCutDown(CommandType? commandType, List<int>? data, Function? callBack) async {
//
// bool needCutDownTime = commandType.cutDown;
// int maxResendCount = commandType.resendCnt;
// int outMax = commandType.duration;
//
// if(needCutDownTime){
// lastCommandType = commandType;
// cancelCommandCutDown();
// if(needCutDownTime){
// bufferList = data;
// _commandTimer = Timer.periodic( Duration(
// milliseconds: outMax,
// ), (t){
// if(outTimeCount < maxResendCount){
// outTimeCount++;
// if(bufferList.length > 0){
// AppLog.log(''''
// ------->\n超时 第 $outTimeCount 次 重发 $commandType 指令 ''',error: true);
// // if(commandType != CommandType.upgrade){
// // print('重发重置帧序号');
// // bufferList.replaceRange(1, 2, [IoManager().commandIndex]);
// // }
// _sendNormalData(bufferList);
// }
// }else{
// bufferList = [];
// cancelCommandCutDown();
// print('managerSendData ❌ callBack');
// if(callBack != null){
// callBack(ErrorType.timeOut);
// }
// }
// });
// }
// }
// }
//
// void cancelCommandCutDown({CommandType commandType}){
// AppLog.log('发送指令类取消定时');
// if(commandType != null && commandType != lastCommandType)return;
// if(_commandTimer != null){
// _commandTimer.cancel();
// _commandTimer = null;
// }
// outTimeCount = 1;
// bufferList.clear();
// bufferList = [];
// }
}