app-starlock/star_lock/lib/blue/sender_beforeDataManage.dart

228 lines
8.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.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';
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);
}
});
}
// 添加用户
Future<void> _replyAddUserKey(Reply reply) async {
int status = reply.data[46];
switch (status) {
case 0x00:
//成功
2024-05-09 10:48:28 +08:00
CommonDataManage().currentLockUserNo = listChangInt(reply.data.sublist(47, 49));
CommonDataManage().currentKeyInfo.lockUserNo = CommonDataManage().currentLockUserNo;
_updateLockUserNo();
break;
case 0x06:
//无权限
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
var token = reply.data.sublist(42, 46);
List<String> strTokenList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, strTokenList);
// IoSenderManage.senderAddUser(
// lockID: BlueManage().connectDeviceName,
// authUserID: CommonDataManage().currentKeyInfo.senderUserId!.toString(),
// keyID: CommonDataManage().currentKeyInfo.keyId.toString(),
// userID: await Storage.getUid(),
// openMode: 1,
// keyType: 0,
// startDate: CommonDataManage().currentKeyInfo.startDate!~/10000,
// expireDate: CommonDataManage().currentKeyInfo.endDate!~/10000,
// role: CommonDataManage().currentKeyInfo.keyRight == 1 ? 1 : 0,
// password: "123456",
// needAuthor: 1,
// publicKey: publicKeyDataList,
// privateKey: getPrivateKeyList,
// token: token);
LockListInfoItemEntity currentKeyInfo =
CommonDataManage().currentKeyInfo;
AppLog.log(
"startDate111:${currentKeyInfo.startDate} endDate:${currentKeyInfo.endDate}");
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: 0,
startDate: currentKeyInfo.startDate! ~/ 1000,
expireDate: currentKeyInfo.endDate! ~/ 1000,
useCountLimit: 0xFFFF,
// useCountLimit: 1,
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: token);
eventBus.fire(LockAddUserSucceedEvent(addUserData.packageData(), 1));
break;
case 0x0c:
//锁设备用户超过 32个需要同步锁用户列表刷新
var entity = await ApiRepository.to.getLockUserNoList(
lockId: CommonDataManage().currentKeyInfo.lockId!);
if (!entity.errorCode!.codeIsSuccessful &&
(entity.data?.userNos ?? []).isNotEmpty) {
return;
}
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
var tokenKey = await Storage.getStringList(saveBlueToken);
List<int> tokenList = changeStringListToIntList(tokenKey!);
AppLog.log('---> ${entity.data?.userNos}');
BlueManage().bludSendData(BlueManage().connectDeviceName,
(BluetoothConnectionState connectionState) async {
if (connectionState == BluetoothConnectionState.connected) {
IoSenderManage.senderCleanUpUsersCommand(
lockID: BlueManage().connectDeviceName,
authUserID:
CommonDataManage().currentKeyInfo.senderUserId!.toString(),
keyID: CommonDataManage().currentKeyInfo.keyId.toString(),
userID: await Storage.getUid(),
userNoList: entity.data!.userNos!,
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
token: tokenList);
}
});
break;
default:
//失败
break;
}
}
Future<List<int>> getAddUserKeyData() async {
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
var token = await Storage.getStringList(saveBlueToken);
List<int> getTokenList = 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: 0,
startDate: currentKeyInfo.startDate! ~/ 1000,
expireDate: currentKeyInfo.endDate! ~/ 1000,
useCountLimit: 0xFFFF,
// useCountLimit: 1,
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: getTokenList);
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();
}
}