app-starlock/lib/blue/sender_data.dart
2024-11-06 09:28:18 +08:00

161 lines
5.1 KiB
Dart
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import '../app_settings/app_settings.dart';
import '../tools/commonDataManage.dart';
import '../tools/eventBusEventManage.dart';
import 'io_sender.dart';
import 'io_tool/io_model.dart';
import 'io_tool/manager_event_bus.dart';
import 'sender_beforeDataManage.dart';
typedef CommandSendCallBack = void Function(ErrorType errorType);
class CommandSenderManager {
factory CommandSenderManager() => _manager;
CommandSenderManager._init() {
init();
}
static final CommandSenderManager _manager = CommandSenderManager._init();
static CommandSenderManager getInstance() => _manager;
void init() {
_initLockAddUserSucceedEvent();
_initTransferSmartLockSucceedEvent();
}
// 蓝牙添加用户成功 继续发送上次未发送的数据
StreamSubscription? _passCurrentLockInformationEvent;
List<int> dataBeforeAddTheUser = <int>[];
void _initLockAddUserSucceedEvent() {
// 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus
_passCurrentLockInformationEvent = eventBus
.on<LockAddUserSucceedEvent>()
.listen((LockAddUserSucceedEvent event) {
if (event.type == 0) {
sendNormalData(dataBeforeAddTheUser);
} else {
sendNormalData(event.dataList);
}
});
}
StreamSubscription? _transferSmartLockEvent;
List<int> dataTransferSmartLock = <int>[];
void _initTransferSmartLockSucceedEvent() {
// 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus
_transferSmartLockEvent =
eventBus.on<LockInitUserNoEvent>().listen((LockInitUserNoEvent event) {
sendNormalData(dataTransferSmartLock);
});
}
// CommandType lastCommandType = CommandType.readLockStatusInfo;
bool canSendControlCommand = false;
Future<void> managerSendData(
{required SenderProtocol command,
bool isBeforeAddUser = false,
CommandSendCallBack? callBack}) async {
if (callBack != null) {
AppLog.log('managerSendData ❌ callBack');
callBack(ErrorType.notConnected);
return;
}
SenderBeforeDataManage().isBeforeAddUser = isBeforeAddUser;
final List<int> value = command.packageData();
if (isBeforeAddUser == true) {
// 如果是添加用户之前调用协议 直接发送
sendNormalData(value);
} else {
// 当前锁被转移了 需要更新锁的userid 调用转移锁协议
if (CommonDataManage().initUserNo == 1) {
final List<int> entity =
await SenderBeforeDataManage().getTransferSmartLockData();
sendNormalData(entity);
dataTransferSmartLock = value;
return;
}
// 添加用户之后调用协议就要判断是否添加用户
if (CommonDataManage().currentLockUserNo == 0) {
// 如果LockUserNo为0先添加用户
final List<int> entity =
await SenderBeforeDataManage().getAddUserKeyData();
sendNormalData(entity);
dataBeforeAddTheUser = value;
} else {
sendNormalData(value);
}
}
}
Future<void> sendNormalData(List<int> data) async {
if (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){
// // AppLog.log('重发重置帧序号');
// // 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 = [];
// }
void dispose() {
_passCurrentLockInformationEvent?.cancel();
_transferSmartLockEvent?.cancel();
}
}