app-starlock/lib/blue/sender_beforeDataManage.dart

226 lines
8.0 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:star_lock/app_settings/app_settings.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_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 {
static SenderBeforeDataManage? _manager;
SenderBeforeDataManage._init();
static SenderBeforeDataManage? shareManager() {
_manager ??= SenderBeforeDataManage._init();
_manager!._init();
return _manager;
}
factory SenderBeforeDataManage() => shareManager()!;
SenderBeforeDataManage? get manager => shareManager();
void _init() {
_initReplySubscription();
}
// 监听设备返回的数据
StreamSubscription<Reply>? _replySubscription;
bool isBeforeAddUser = true;
// 启动订阅
void _initReplySubscription() {
_replySubscription ??= EventBusManager().eventBus!.on<Reply>().listen((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
}
});
}
// 解析添加用户订阅
Future<void> _replyAddUserKey(Reply reply) async {
int status = reply.data[46];
switch (status) {
case 0x00:
//成功
CommonDataManage().currentLockUserNo =
listChangInt(reply.data.sublist(47, 49));
CommonDataManage().currentKeyInfo.lockUserNo =
CommonDataManage().currentLockUserNo;
_updateLockUserNo();
break;
case 0x06:
//无权限
var token = reply.data.sublist(42, 46);
List<String> strTokenList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, strTokenList);
var addUserData = await getAddUserKeyData(tokenList: token);
eventBus.fire(LockAddUserSucceedEvent(addUserData, 1));
break;
case 0x0c:
//锁设备用户超过 32个需要同步锁用户列表刷新
var addUserData = await getCleanUpUsers();
CommandSenderManager().sendNormalData(addUserData);
break;
default:
//失败
break;
}
}
// 解析清理用户订阅
2024-05-09 14:37:39 +08:00
Future<void> _cleanUpUsersReply(Reply reply) async {
int status = reply.data[6];
switch (status) {
case 0x00:
//成功
//添加用户
var addUserData = await getAddUserKeyData();
CommandSenderManager().sendNormalData(addUserData);
2024-05-09 14:37:39 +08:00
break;
case 0x06:
//无权限
var token = reply.data.sublist(2, 6);
2024-05-09 14:37:39 +08:00
List<String> strTokenList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, strTokenList);
var 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<List<int>> getCleanUpUsers({List<int>? tokenList}) async {
var entity = await ApiRepository.to
.getLockUserNoList(lockId: CommonDataManage().currentKeyInfo.lockId!);
if (!entity.errorCode!.codeIsSuccessful ||
(entity.data?.userNos ?? []).isEmpty) {
throw Exception('ApiRepository.to.getLockUserNoList 访问失败');
}
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
2024-05-09 14:37:39 +08:00
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
2024-05-09 14:37:39 +08:00
if (tokenList == null) {
var tokenKey = await Storage.getStringList(saveBlueToken);
tokenList = changeStringListToIntList(tokenKey!);
}
var 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 {
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
if (tokenList == null) {
var token = await Storage.getStringList(saveBlueToken);
tokenList = changeStringListToIntList(token!);
}
2024-05-09 14:37:39 +08:00
LockListInfoItemEntity currentKeyInfo = CommonDataManage().currentKeyInfo;
DateTime? startTime;
DateTime? endTime;
var startDateTime = 0;
var endDateTime = 0;
bool isRound = false;
var 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}}");
var 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();
}
// 普通用户接收电子钥匙之后 更新锁用户NO
void _updateLockUserNo() async {
LockNetTokenEntity entity = await ApiRepository.to.updateLockUserNo(
keyId: CommonDataManage().currentKeyInfo.keyId.toString(),
lockUserNo: CommonDataManage().currentKeyInfo.lockUserNo.toString());
if (entity.errorCode!.codeIsSuccessful) {
eventBus.fire(LockAddUserSucceedEvent([0], 0));
}
}
dispose() {
_replySubscription!.cancel();
}
}