154 lines
5.6 KiB
Dart
154 lines
5.6 KiB
Dart
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/blue/sender_manage.dart';
|
|
import 'package:star_lock/tools/baseGetXController.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:
|
|
//成功
|
|
CommonDataManage().currentLockUserNo = reply.data[47];
|
|
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);
|
|
AppLog.log("startDate111:${CommonDataManage().currentKeyInfo.startDate} endDate:${CommonDataManage().currentKeyInfo.endDate}");
|
|
|
|
var addUserData = AddUserCommand(
|
|
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!~/1000,
|
|
expireDate: CommonDataManage().currentKeyInfo.endDate!~/1000,
|
|
role: CommonDataManage().currentKeyInfo.keyRight == 1 ? 1 : 0,
|
|
password: "123456",
|
|
needAuthor: 1,
|
|
publicKey: publicKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: token);
|
|
eventBus.fire(LockAddUserSucceedEvent(addUserData.packageData(), 1));
|
|
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!);
|
|
|
|
var addUserData = AddUserCommand(
|
|
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!~/1000,
|
|
expireDate: CommonDataManage().currentKeyInfo.endDate!~/1000,
|
|
role: CommonDataManage().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();
|
|
}
|
|
} |