app-starlock/lib/blue/sender_beforeDataManage.dart

291 lines
11 KiB
Dart
Raw Permalink Normal View History

import 'dart:async';
import 'package:star_lock/blue/entity/lock_user_no_list_entity.dart';
2024-05-09 13:41:53 +08:00
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';
2024-05-09 14:37:39 +08:00
import 'sender_data.dart';
class SenderBeforeDataManage {
factory SenderBeforeDataManage() => shareManager()!;
SenderBeforeDataManage._init();
2025-10-27 09:27:37 +08:00
static SenderBeforeDataManage? _manager;
static SenderBeforeDataManage? shareManager() {
_manager ??= SenderBeforeDataManage._init();
_manager!._init();
return _manager;
}
SenderBeforeDataManage? get manager => shareManager();
void _init() {
_initReplySubscription();
}
// 监听设备返回的数据
StreamSubscription<Reply>? _replySubscription;
2025-10-27 09:27:37 +08:00
// 是否是添加用户之前的调用
bool isBeforeAddUser = true;
// 启动订阅
void _initReplySubscription() {
_replySubscription ??= EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
// 添加用户
if (reply is AddUserReply && isBeforeAddUser == false) {
_replyAddUserKey(reply);
}
2024-05-09 13:41:53 +08:00
if (reply is CleanUpUsersReply) {
2024-05-09 14:37:39 +08:00
_cleanUpUsersReply(reply);
2024-05-09 13:41:53 +08:00
}
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;
}
}
// 解析清理用户订阅
2024-05-09 14:37:39 +08:00
Future<void> _cleanUpUsersReply(Reply reply) async {
final int status = reply.data[6];
2024-05-09 14:37:39 +08:00
switch (status) {
case 0x00:
//成功
//添加用户
final List<int> addUserData = await getAddUserKeyData();
CommandSenderManager().sendNormalData(addUserData);
2024-05-09 14:37:39 +08:00
break;
case 0x06:
//无权限
final List<int> token = reply.data.sublist(2, 6);
final List<String> strTokenList = changeIntListToStringList(token);
2024-05-09 14:37:39 +08:00
Storage.setStringList(saveBlueToken, strTokenList);
final List<int> addUserData = await getCleanUpUsers(tokenList: token);
CommandSenderManager().sendNormalData(addUserData);
2024-05-09 14:37:39 +08:00
break;
default:
//失败
2024-05-09 14:37:39 +08:00
break;
}
}
// 转移锁指令
Future<void> _transferSmartLockReply(Reply reply) async {
final int status = reply.data[6];
switch (status) {
case 0x00:
2025-10-27 09:27:37 +08:00
//成功
CommonDataManage().initUserNo = 0;
CommonDataManage().currentKeyInfo.initUserNo = 0;
_updateLockInitUserNo();
break;
case 0x06:
2025-10-27 09:27:37 +08:00
//无权限
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:
2025-10-27 09:27:37 +08:00
//失败
break;
}
}
//获取清除用户列表指令
Future<List<int>> getCleanUpUsers({List<int>? tokenList}) async {
2025-10-27 09:27:37 +08:00
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!);
2024-05-09 14:37:39 +08:00
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
2024-05-09 14:37:39 +08:00
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!);
}
2024-05-09 14:37:39 +08:00
final LockListInfoItemEntity currentKeyInfo = CommonDataManage().currentKeyInfo;
DateTime? startTime;
DateTime? endTime;
int startDateTime = 0;
int endDateTime = 0;
bool isRound = false;
int useCountLimit = 0xffff;
2025-10-27 09:27:37 +08:00
if (currentKeyInfo.keyType == XSConstantMacro.keyTypeTime) {
// 限时
startDateTime = currentKeyInfo.startDate! ~/ 1000;
endDateTime = currentKeyInfo.endDate! ~/ 1000;
2025-10-27 09:27:37 +08:00
} 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;
2025-10-27 09:27:37 +08:00
endDateTime =
(DateTool().dateToTimestamp(DateTool().dateToYMDString(currentKeyInfo.endDate!.toString()), 1) + CommonDataManage().dayLatestTime) ~/ 1000;
} else if (currentKeyInfo.keyType == XSConstantMacro.keyTypeOnce) {
// 单次
useCountLimit = 1;
}
2025-10-27 09:27:37 +08:00
// 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,
2025-10-27 09:27:37 +08:00
authUserID: currentKeyInfo.senderUserId?.toString() ?? '1',
keyID: currentKeyInfo.keyId.toString(),
userID: await Storage.getUid(),
openMode: 1,
keyType: 0,
startDate: startDateTime,
expireDate: endDateTime,
useCountLimit: useCountLimit,
isRound: isRound ? 1 : 0,
2025-10-27 09:27:37 +08:00
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(
2025-10-27 09:27:37 +08:00
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 {
2025-10-27 09:27:37 +08:00
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();
}
}