2024-04-23 15:15:56 +08:00
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
2023-07-27 15:29:37 +08:00
|
|
|
|
import '../app_settings/app_settings.dart';
|
2024-04-23 15:15:56 +08:00
|
|
|
|
import '../tools/commonDataManage.dart';
|
|
|
|
|
|
import '../tools/eventBusEventManage.dart';
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import 'io_sender.dart';
|
2023-07-27 15:29:37 +08:00
|
|
|
|
import 'io_tool/io_model.dart';
|
|
|
|
|
|
import 'io_tool/manager_event_bus.dart';
|
2024-04-23 15:15:56 +08:00
|
|
|
|
import 'sender_beforeDataManage.dart';
|
2023-07-27 15:29:37 +08:00
|
|
|
|
|
|
|
|
|
|
typedef CommandSendCallBack = void Function(ErrorType errorType);
|
|
|
|
|
|
|
2024-04-24 16:04:07 +08:00
|
|
|
|
class CommandSenderManager {
|
|
|
|
|
|
factory CommandSenderManager() => _manager;
|
|
|
|
|
|
|
|
|
|
|
|
CommandSenderManager._init() {
|
2023-07-27 15:29:37 +08:00
|
|
|
|
init();
|
|
|
|
|
|
}
|
2024-06-14 15:46:43 +08:00
|
|
|
|
static final CommandSenderManager _manager = CommandSenderManager._init();
|
|
|
|
|
|
|
|
|
|
|
|
static CommandSenderManager getInstance() => _manager;
|
2023-07-27 15:29:37 +08:00
|
|
|
|
|
2024-10-15 14:24:35 +08:00
|
|
|
|
void init() {
|
2024-06-14 15:46:43 +08:00
|
|
|
|
_initLockAddUserSucceedEvent();
|
|
|
|
|
|
_initTransferSmartLockSucceedEvent();
|
2024-04-23 15:15:56 +08:00
|
|
|
|
}
|
2023-07-27 15:29:37 +08:00
|
|
|
|
|
2024-06-14 15:46:43 +08:00
|
|
|
|
// 蓝牙添加用户成功 继续发送上次未发送的数据
|
2024-04-23 15:15:56 +08:00
|
|
|
|
StreamSubscription? _passCurrentLockInformationEvent;
|
2024-06-14 15:46:43 +08:00
|
|
|
|
List<int> dataBeforeAddTheUser = <int>[];
|
|
|
|
|
|
void _initLockAddUserSucceedEvent() {
|
2024-04-23 15:15:56 +08:00
|
|
|
|
// 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus
|
2024-11-06 09:28:18 +08:00
|
|
|
|
_passCurrentLockInformationEvent = eventBus
|
|
|
|
|
|
.on<LockAddUserSucceedEvent>()
|
|
|
|
|
|
.listen((LockAddUserSucceedEvent event) {
|
2024-05-03 14:38:09 +08:00
|
|
|
|
if (event.type == 0) {
|
2024-05-09 14:37:39 +08:00
|
|
|
|
sendNormalData(dataBeforeAddTheUser);
|
2024-05-03 14:38:09 +08:00
|
|
|
|
} else {
|
2024-05-09 14:37:39 +08:00
|
|
|
|
sendNormalData(event.dataList);
|
2024-04-23 15:15:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-07-27 15:29:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-14 15:46:43 +08:00
|
|
|
|
StreamSubscription? _transferSmartLockEvent;
|
|
|
|
|
|
List<int> dataTransferSmartLock = <int>[];
|
|
|
|
|
|
void _initTransferSmartLockSucceedEvent() {
|
|
|
|
|
|
// 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus
|
2024-11-06 09:28:18 +08:00
|
|
|
|
_transferSmartLockEvent =
|
|
|
|
|
|
eventBus.on<LockInitUserNoEvent>().listen((LockInitUserNoEvent event) {
|
2024-06-14 15:46:43 +08:00
|
|
|
|
sendNormalData(dataTransferSmartLock);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-11 15:54:38 +08:00
|
|
|
|
// CommandType lastCommandType = CommandType.readLockStatusInfo;
|
2023-07-27 15:29:37 +08:00
|
|
|
|
bool canSendControlCommand = false;
|
|
|
|
|
|
|
2024-05-03 14:38:09 +08:00
|
|
|
|
Future<void> managerSendData(
|
|
|
|
|
|
{required SenderProtocol command,
|
|
|
|
|
|
bool isBeforeAddUser = false,
|
|
|
|
|
|
CommandSendCallBack? callBack}) async {
|
2023-08-08 09:42:35 +08:00
|
|
|
|
if (callBack != null) {
|
2024-05-03 14:38:09 +08:00
|
|
|
|
AppLog.log('managerSendData ❌ callBack');
|
|
|
|
|
|
callBack(ErrorType.notConnected);
|
2023-08-16 18:21:42 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-07-27 15:29:37 +08:00
|
|
|
|
|
2024-05-14 15:11:14 +08:00
|
|
|
|
SenderBeforeDataManage().isBeforeAddUser = isBeforeAddUser;
|
2024-06-14 15:46:43 +08:00
|
|
|
|
final List<int> value = command.packageData();
|
2024-05-03 14:38:09 +08:00
|
|
|
|
if (isBeforeAddUser == true) {
|
2024-05-09 14:37:39 +08:00
|
|
|
|
// 如果是添加用户之前调用协议 直接发送
|
|
|
|
|
|
sendNormalData(value);
|
2024-05-03 14:38:09 +08:00
|
|
|
|
} else {
|
2024-06-14 15:46:43 +08:00
|
|
|
|
// 当前锁被转移了 需要更新锁的userid 调用转移锁协议
|
2024-11-06 09:28:18 +08:00
|
|
|
|
if (CommonDataManage().initUserNo == 1) {
|
|
|
|
|
|
final List<int> entity =
|
|
|
|
|
|
await SenderBeforeDataManage().getTransferSmartLockData();
|
2024-06-14 15:46:43 +08:00
|
|
|
|
sendNormalData(entity);
|
|
|
|
|
|
dataTransferSmartLock = value;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-05-09 14:37:39 +08:00
|
|
|
|
// 添加用户之后调用协议就要判断是否添加用户
|
2024-05-03 14:38:09 +08:00
|
|
|
|
if (CommonDataManage().currentLockUserNo == 0) {
|
2024-05-09 14:37:39 +08:00
|
|
|
|
// 如果LockUserNo为0,先添加用户
|
2024-11-06 09:28:18 +08:00
|
|
|
|
final List<int> entity =
|
|
|
|
|
|
await SenderBeforeDataManage().getAddUserKeyData();
|
2024-05-09 14:37:39 +08:00
|
|
|
|
sendNormalData(entity);
|
2024-04-23 15:15:56 +08:00
|
|
|
|
dataBeforeAddTheUser = value;
|
2024-05-03 14:38:09 +08:00
|
|
|
|
} else {
|
2024-05-09 14:37:39 +08:00
|
|
|
|
sendNormalData(value);
|
2024-04-23 15:15:56 +08:00
|
|
|
|
}
|
2024-04-24 18:01:41 +08:00
|
|
|
|
}
|
2023-07-27 15:29:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-14 15:46:43 +08:00
|
|
|
|
Future<void> sendNormalData(List<int> data) async {
|
2023-08-08 09:42:35 +08:00
|
|
|
|
if (data.isNotEmpty) {
|
2024-05-03 14:38:09 +08:00
|
|
|
|
EventBusManager().eventBusFir(
|
|
|
|
|
|
EventSendModel(data: data, sendChannel: DataChannel.ble));
|
2023-07-27 15:29:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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){
|
2024-04-26 15:38:59 +08:00
|
|
|
|
// // AppLog.log('重发重置帧序号');
|
2023-07-27 15:29:37 +08:00
|
|
|
|
// // 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 = [];
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2024-10-15 14:24:35 +08:00
|
|
|
|
void dispose() {
|
2024-04-23 15:15:56 +08:00
|
|
|
|
_passCurrentLockInformationEvent?.cancel();
|
2024-06-14 15:46:43 +08:00
|
|
|
|
_transferSmartLockEvent?.cancel();
|
2024-04-23 15:15:56 +08:00
|
|
|
|
}
|
2024-04-24 16:04:07 +08:00
|
|
|
|
}
|