app-starlock/lib/blue/sender_beforeDataManage.dart
魏少阳 2368bb631c 1、完成网关模块国际化
2、删除蓝牙协议模块不用的蓝牙协议文件
2024-10-15 14:24:35 +08:00

297 lines
11 KiB
Dart
Executable File
Raw Permalink 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 'package:star_lock/blue/entity/lock_user_no_list_entity.dart';
import 'package:star_lock/blue/io_protocol/io_cleanUpUsers.dart';
import 'package:star_lock/common/XSConstantMacro/XSConstantMacro.dart';
import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.dart';
import 'package:star_lock/tools/baseGetXController.dart';
import 'package:star_lock/tools/dateTool.dart';
import 'package:star_lock/tools/eventBusEventManage.dart';
import '../main/lockDetail/lockDetail/lockNetToken_entity.dart';
import '../network/api_repository.dart';
import '../tools/commonDataManage.dart';
import '../tools/storage.dart';
import 'blue_manage.dart';
import 'io_protocol/io_addUser.dart';
import 'io_protocol/io_transferSmartLock.dart';
import 'io_reply.dart';
import 'io_tool/io_tool.dart';
import 'io_tool/manager_event_bus.dart';
import 'sender_data.dart';
class SenderBeforeDataManage {
factory SenderBeforeDataManage() => shareManager()!;
SenderBeforeDataManage._init();
static SenderBeforeDataManage? _manager;
static SenderBeforeDataManage? shareManager() {
_manager ??= SenderBeforeDataManage._init();
_manager!._init();
return _manager;
}
SenderBeforeDataManage? get manager => shareManager();
void _init() {
_initReplySubscription();
}
// 监听设备返回的数据
StreamSubscription<Reply>? _replySubscription;
// 是否是添加用户之前的调用
bool isBeforeAddUser = true;
// 启动订阅
void _initReplySubscription() {
_replySubscription ??= EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
// 添加用户
if (reply is AddUserReply && isBeforeAddUser == false) {
_replyAddUserKey(reply);
}
if (reply is CleanUpUsersReply) {
_cleanUpUsersReply(reply);
}
if (reply is TransferSmartLockReply) {
_transferSmartLockReply(reply);
}
});
}
// 解析添加用户订阅
Future<void> _replyAddUserKey(Reply reply) async {
final int status = reply.data[46];
switch (status) {
case 0x00:
//成功
final List<int> userNoData = reply.data.sublist(47, 49);
CommonDataManage().currentLockUserNo = listChangInt(userNoData);
CommonDataManage().currentKeyInfo.lockUserNo = CommonDataManage().currentLockUserNo;
_updateLockUserNo(userNoData);
break;
case 0x06:
//无权限
final List<int> token = reply.data.sublist(42, 46);
final List<String> strTokenList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, strTokenList);
final List<int> addUserData = await getAddUserKeyData(tokenList: token);
eventBus.fire(LockAddUserSucceedEvent(addUserData, 1));
break;
case 0x0c:
//锁设备用户超过 32个需要同步锁用户列表刷新
final List<int> addUserData = await getCleanUpUsers();
CommandSenderManager().sendNormalData(addUserData);
break;
default:
//失败
break;
}
}
// 解析清理用户订阅
Future<void> _cleanUpUsersReply(Reply reply) async {
final int status = reply.data[6];
switch (status) {
case 0x00:
//成功
//添加用户
final List<int> addUserData = await getAddUserKeyData();
CommandSenderManager().sendNormalData(addUserData);
break;
case 0x06:
//无权限
final List<int> token = reply.data.sublist(2, 6);
final List<String> strTokenList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, strTokenList);
final List<int> addUserData = await getCleanUpUsers(tokenList: token);
CommandSenderManager().sendNormalData(addUserData);
break;
default:
//失败
break;
}
}
// 转移锁指令
Future<void> _transferSmartLockReply(Reply reply) async {
final int status = reply.data[6];
switch (status) {
case 0x00:
//成功
CommonDataManage().initUserNo = 0;
CommonDataManage().currentKeyInfo.initUserNo = 0;
_updateLockInitUserNo();
break;
case 0x06:
//无权限
final List<int> token = reply.data.sublist(2, 6);
final List<String> strTokenList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, strTokenList);
final List<int> transferSmartLockData = await getTransferSmartLockData(tokenList: token);
CommandSenderManager().sendNormalData(transferSmartLockData);
break;
default:
//失败
break;
}
}
//获取清除用户列表指令
Future<List<int>> getCleanUpUsers({List<int>? tokenList}) async {
final LockUserNoListEntity entity = await ApiRepository.to
.getLockUserNoList(lockId: CommonDataManage().currentKeyInfo.lockId!);
if (!entity.errorCode!.codeIsSuccessful ||
(entity.data?.userNos ?? <int>[]).isEmpty) {
throw Exception('ApiRepository.to.getLockUserNoList 访问失败');
}
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
if (tokenList == null) {
final List<String>? tokenKey = await Storage.getStringList(saveBlueToken);
tokenList = changeStringListToIntList(tokenKey!);
}
final CleanUpUsersCommand cleanUpUsersData = CleanUpUsersCommand(
lockID: BlueManage().connectDeviceName,
authUserID: CommonDataManage().currentKeyInfo.senderUserId!.toString(),
keyID: CommonDataManage().currentKeyInfo.keyId.toString(),
userID: await Storage.getUid(),
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
userNoList: entity.data!.userNos!,
token: tokenList,
);
return cleanUpUsersData.packageData();
}
//获取添加用户指令
Future<List<int>> getAddUserKeyData({List<int>? tokenList}) async {
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
if (tokenList == null) {
final List<String>? token = await Storage.getStringList(saveBlueToken);
tokenList = changeStringListToIntList(token!);
}
final LockListInfoItemEntity currentKeyInfo = CommonDataManage().currentKeyInfo;
DateTime? startTime;
DateTime? endTime;
int startDateTime = 0;
int endDateTime = 0;
bool isRound = false;
int useCountLimit = 0xffff;
if(currentKeyInfo.keyType == XSConstantMacro.keyTypeTime){
// 限时
startDateTime = currentKeyInfo.startDate! ~/ 1000;
endDateTime = currentKeyInfo.endDate! ~/ 1000;
}else if(currentKeyInfo.keyType == XSConstantMacro.keyTypeLoop){
// 循环
isRound = true;
startTime = DateTime.fromMillisecondsSinceEpoch(currentKeyInfo.startDate!);
endTime = DateTime.fromMillisecondsSinceEpoch(currentKeyInfo.endDate!);
startDateTime = DateTool().dateToTimestamp(DateTool().dateToYMDString(currentKeyInfo.startDate!.toString()), 1) ~/ 1000;
endDateTime = (DateTool().dateToTimestamp(DateTool().dateToYMDString(currentKeyInfo.endDate!.toString()), 1) + CommonDataManage().dayLatestTime) ~/ 1000;
}else if(currentKeyInfo.keyType == XSConstantMacro.keyTypeOnce){
// 单次
useCountLimit = 1;
}
// AppLog.log("startTime.hour:${startTime!.hour} startTime.minute:${startTime!.minute} endTime.hour:${endTime!.hour} endTime.minute:${endTime!.minute}}");
final AddUserCommand addUserData = AddUserCommand(
lockID: BlueManage().connectDeviceName,
authUserID: currentKeyInfo.senderUserId!.toString(),
keyID: currentKeyInfo.keyId.toString(),
userID: await Storage.getUid(),
openMode: 1,
keyType: 0,
startDate: startDateTime,
expireDate: endDateTime,
useCountLimit: useCountLimit,
isRound: isRound ? 1 : 0,
weekRound: isRound
? DateTool().accordingTheCycleIntoTheCorrespondingNumber(
currentKeyInfo.weekDays!)
: 0,
startHour: isRound ? startTime!.hour : 0,
startMin: isRound ? startTime!.minute : 0,
endHour: isRound ? endTime!.hour : 0,
endMin: isRound ? endTime!.minute : 0,
role: currentKeyInfo.keyRight == 1 ? 1 : 0,
password: '123456',
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
token: tokenList);
return addUserData.packageData();
}
//获取转移智能锁指令数据
Future<List<int>> getTransferSmartLockData({List<int>? tokenList}) async {
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
if (tokenList == null) {
final List<String>? token = await Storage.getStringList(saveBlueToken);
tokenList = changeStringListToIntList(token!);
}
final LockListInfoItemEntity currentKeyInfo = CommonDataManage().currentKeyInfo;
final TransferSmartLockCommand transferSmartLockData = TransferSmartLockCommand(
lockID: BlueManage().connectDeviceName,
keyID: currentKeyInfo.keyId.toString(),
userID: await Storage.getUid(),
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
token: tokenList);
return transferSmartLockData.packageData();
}
// 普通用户接收电子钥匙之后 更新锁用户NO
Future<void> _updateLockUserNo(List<int> dataList) async {
final LockNetTokenEntity entity = await ApiRepository.to.updateLockUserNo(
keyId: CommonDataManage().currentKeyInfo.keyId.toString(),
lockUserNo: CommonDataManage().currentKeyInfo.lockUserNo.toString());
if (entity.errorCode!.codeIsSuccessful) {
eventBus.fire(RefreshLockListInfoDataEvent());
eventBus.fire(LockAddUserSucceedEvent(<int>[0], 0));
}
}
// 更新锁用户InitUserNo
Future<void> _updateLockInitUserNo() async {
final LockNetTokenEntity entity = await ApiRepository.to.updateLockInitUserNo(
lockId: CommonDataManage().currentKeyInfo.lockId ?? 0,
initUserNo: CommonDataManage().currentKeyInfo.initUserNo ?? 0);
if (entity.errorCode!.codeIsSuccessful) {
eventBus.fire(RefreshLockListInfoDataEvent());
eventBus.fire(LockInitUserNoEvent());
}
}
dispose() {
_replySubscription!.cancel();
}
}