211 lines
7.2 KiB
Dart
211 lines
7.2 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_cleanUpUsers.dart';
|
||
import 'package:star_lock/blue/sender_manage.dart';
|
||
import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.dart';
|
||
import 'package:star_lock/network/api_provider.dart';
|
||
import 'package:star_lock/tools/baseGetXController.dart';
|
||
import 'package:star_lock/tools/dateTool.dart';
|
||
import 'package:star_lock/tools/eventBusEventManage.dart';
|
||
|
||
import '../app_settings/app_settings.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';
|
||
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;
|
||
|
||
// 启动订阅
|
||
void _initReplySubscription() {
|
||
_replySubscription ??= EventBusManager().eventBus!.on<Reply>().listen((reply) async {
|
||
// 添加用户
|
||
if (reply is AddUserReply) {
|
||
_replyAddUserKey(reply);
|
||
}
|
||
|
||
if (reply is CleanUpUsersReply) {
|
||
_cleanUpUsersReply(reply);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 解析添加用户订阅
|
||
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;
|
||
}
|
||
}
|
||
|
||
// 解析清理用户订阅
|
||
Future<void> _cleanUpUsersReply(Reply reply) async {
|
||
int status = reply.data[6];
|
||
switch (status) {
|
||
case 0x00:
|
||
//成功
|
||
//添加用户
|
||
var addUserData = await getAddUserKeyData();
|
||
CommandSenderManager().sendNormalData(addUserData);
|
||
break;
|
||
case 0x06:
|
||
//无权限
|
||
var token = reply.data.sublist(2, 6);
|
||
List<String> strTokenList = changeIntListToStringList(token);
|
||
Storage.setStringList(saveBlueToken, strTokenList);
|
||
var addUserData = await getCleanUpUsers(tokenList: token);
|
||
CommandSenderManager().sendNormalData(addUserData);
|
||
break;
|
||
default:
|
||
//失败
|
||
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!);
|
||
|
||
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
||
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
||
|
||
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!);
|
||
}
|
||
|
||
LockListInfoItemEntity currentKeyInfo = CommonDataManage().currentKeyInfo;
|
||
DateTime startTime =
|
||
DateTime.fromMillisecondsSinceEpoch(currentKeyInfo.startDate! ~/ 1000);
|
||
DateTime endTime =
|
||
DateTime.fromMillisecondsSinceEpoch(currentKeyInfo.endDate! ~/ 1000);
|
||
bool isRound = currentKeyInfo.keyType == 2;
|
||
|
||
var addUserData = AddUserCommand(
|
||
lockID: BlueManage().connectDeviceName,
|
||
authUserID: currentKeyInfo.senderUserId!.toString(),
|
||
keyID: currentKeyInfo.keyId.toString(),
|
||
userID: await Storage.getUid(),
|
||
openMode: 1,
|
||
keyType: currentKeyInfo.keyType,
|
||
startDate: currentKeyInfo.startDate! ~/ 1000,
|
||
expireDate: currentKeyInfo.endDate! ~/ 1000,
|
||
useCountLimit: 0xFFFF,
|
||
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();
|
||
}
|
||
}
|