490 lines
16 KiB
Dart
490 lines
16 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/blue/io_protocol/io_changeAdministratorPassword.dart';
|
|
import 'package:star_lock/blue/io_protocol/io_senderCustomPasswords.dart';
|
|
import 'package:star_lock/blue/io_type.dart';
|
|
|
|
import '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_protocol/io_addUser.dart';
|
|
import '../../../blue/io_reply.dart';
|
|
import '../../../blue/io_tool/io_tool.dart';
|
|
import '../../../blue/io_tool/manager_event_bus.dart';
|
|
import '../../../blue/sender_manage.dart';
|
|
import '../../../network/api_repository.dart';
|
|
import '../../../tools/baseGetXController.dart';
|
|
import '../../../tools/eventBusEventManage.dart';
|
|
import '../../../tools/showTFView.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'saveLock_state.dart';
|
|
|
|
class SaveLockLogic extends BaseGetXController {
|
|
final SaveLockState state = SaveLockState();
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if (reply is AddUserReply) {
|
|
_replyAddUserKey(reply);
|
|
}
|
|
|
|
if (reply is SenderCustomPasswordsReply) {
|
|
_replySenderCustomPasswords(reply);
|
|
}
|
|
|
|
if (reply is ChangeAdministratorPasswordReply) {
|
|
_replyChangeAdministratorPassword(reply);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
// 添加用户
|
|
Future<void> _replyAddUserKey(Reply reply) async {
|
|
var lockId = reply.data.sublist(2, 42);
|
|
// print("lockId:$lockId");
|
|
|
|
var token = reply.data.sublist(42, 46);
|
|
List<String> strTokenList = changeIntListToStringList(token);
|
|
Storage.setStringList(saveBlueToken, strTokenList);
|
|
// print("token:$token");
|
|
|
|
int status = reply.data[46];
|
|
// print("status:$status reply.data:${reply.data}");
|
|
|
|
print("status:$status");
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
print("添加用户数据解析成功");
|
|
state.lockUserNo = reply.data[47];
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
bindBlueAdmin();
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("需要鉴权");
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
IoSenderManage.senderAddUser(
|
|
lockID: BlueManage().connectDeviceName,
|
|
authUserID: await Storage.getUid(),
|
|
keyID: "1",
|
|
userID: await Storage.getUid(),
|
|
openMode: 1,
|
|
keyType: 1,
|
|
startDate: DateTime.now().millisecondsSinceEpoch,
|
|
expireDate: 0x11223344,
|
|
role: 255,
|
|
password: "123456",
|
|
needAuthor: 1,
|
|
publicKey: publicKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: token);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("用户无权限");
|
|
dismissEasyLoading();
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("添加用户权限校验错误");
|
|
dismissEasyLoading();
|
|
break;
|
|
default:
|
|
//失败
|
|
print("领锁失败");
|
|
dismissEasyLoading();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 添加管理员密码
|
|
Future<void> _replySenderCustomPasswords(Reply reply) async {
|
|
var token = reply.data.sublist(5, 9);
|
|
var saveStrList = changeIntListToStringList(token);
|
|
print("_replyFactoryDataResetKeyToken:$token");
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
int status = reply.data[2];
|
|
print("status:$status");
|
|
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType!.typeValue} 数据解析成功");
|
|
dismissEasyLoading();
|
|
addLockAdminPassword(true);
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("${reply.commandType!.typeValue} 需要鉴权");
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
IoSenderManage.senderCustomPasswordsCommand(
|
|
keyID: "1",
|
|
userID: await Storage.getUid(),
|
|
pwdNo: 1,
|
|
pwd: "123456",
|
|
useCountLimit: 0xff,
|
|
startTime: 0x11223344,
|
|
endTime: 0x11223344,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: token);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType!.typeValue} 用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType!.typeValue} 权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType!.typeValue} 失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 修改管理员密码
|
|
Future<void> _replyChangeAdministratorPassword(Reply reply) async {
|
|
var token = reply.data.sublist(5, 9);
|
|
var saveStrList = changeIntListToStringList(token);
|
|
print("_replyFactoryDataResetKeyToken:$token");
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
int status = reply.data[2];
|
|
print("status:$status");
|
|
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType!.typeValue} 数据解析成功");
|
|
cancelBlueConnetctToastTimer();
|
|
addLockAdminPassword(false);
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("${reply.commandType!.typeValue} 需要鉴权");
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
IoSenderManage.changeAdministratorPasswordCommand(
|
|
keyID: "1",
|
|
userID: await Storage.getUid(),
|
|
pwdNo: 0,
|
|
pwd:state.adminPasswordTF.text,
|
|
useCountLimit: 0xff,
|
|
startTime: 0x11223344,
|
|
endTime: 0x11223344,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: token);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType!.typeValue} 用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType!.typeValue} 权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType!.typeValue} 失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 添加用户
|
|
Future<void> addUserConnectBlue() async {
|
|
// print("111111");
|
|
// 进来之后首先连接
|
|
showEasyLoading();
|
|
showBlueConnetctToastTimer(action: (){
|
|
dismissEasyLoading();
|
|
state.saveBtnIsUsable.value = true;
|
|
});
|
|
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState deviceConnectionState) async {
|
|
// print("222222");
|
|
if (deviceConnectionState == DeviceConnectionState.connected){
|
|
// print("333333");
|
|
// 私钥
|
|
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 = [0, 0, 0, 0];
|
|
if (token != null) {
|
|
getTokenList = changeStringListToIntList(token);
|
|
}
|
|
|
|
IoSenderManage.senderAddUser(
|
|
lockID: BlueManage().connectDeviceName,
|
|
authUserID:await Storage.getUid(),
|
|
keyID:"1",
|
|
userID:await Storage.getUid(),
|
|
openMode:1,
|
|
keyType:1,
|
|
startDate:DateTime.now().millisecondsSinceEpoch,
|
|
expireDate:0x11223344,
|
|
role:255,
|
|
password:"123456",
|
|
needAuthor:1,
|
|
publicKey:publicKeyDataList,
|
|
privateKey:getPrivateKeyList,
|
|
token: getTokenList
|
|
);
|
|
} else if (deviceConnectionState == DeviceConnectionState.disconnected) {
|
|
print("444444");
|
|
dismissEasyLoading();
|
|
cancelBlueConnetctToastTimer();
|
|
state.saveBtnIsUsable.value = true;
|
|
if(state.ifCurrentScreen.value == true){
|
|
showBlueConnetctToast();
|
|
}
|
|
}
|
|
}, isAddEquipment: true);
|
|
}
|
|
|
|
// 设置管理员密码(同添加自定义开锁密码,指纹/密码/卡片前5个是管理员)
|
|
Future<void> senderCustomPasswords() async {
|
|
var rng = Random();
|
|
var number = rng.nextInt(900000) + 100000; // 生成 100000 到 999999 之间的随机整数
|
|
state.adminPassword = number.toString();
|
|
state.adminPasswordTF.text = number.toString();
|
|
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState deviceConnectionState) async {
|
|
if (deviceConnectionState == DeviceConnectionState.connected) {
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = changeStringListToIntList(token!);
|
|
print("openDoorTokenPubToken:$getTokenList");
|
|
|
|
IoSenderManage.senderCustomPasswordsCommand(
|
|
keyID: "1",
|
|
userID: await Storage.getUid(),
|
|
pwdNo: 1,
|
|
pwd:state.adminPasswordTF.text,
|
|
useCountLimit: 0xff,
|
|
startTime: 0x11223344,
|
|
endTime: 0x11223344,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: getTokenList);
|
|
}
|
|
}, isAddEquipment: true);
|
|
}
|
|
|
|
// 修改管理员密码(同添加自定义开锁密码,指纹/密码/卡片前5个是管理员)
|
|
Future<void> changeAdministratorPasswordCommand() async {
|
|
showEasyLoading();
|
|
showBlueConnetctToastTimer(action: (){
|
|
dismissEasyLoading();
|
|
});
|
|
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState deviceConnectionState) async {
|
|
if (deviceConnectionState == DeviceConnectionState.connected) {
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = changeStringListToIntList(token!);
|
|
print("openDoorTokenPubToken:$getTokenList");
|
|
|
|
IoSenderManage.changeAdministratorPasswordCommand(
|
|
keyID: "1",
|
|
userID: await Storage.getUid(),
|
|
pwdNo: 0,
|
|
pwd:state.adminPasswordTF.text,
|
|
useCountLimit: 0xff,
|
|
startTime: 0x11223344,
|
|
endTime: 0x11223344,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: getTokenList);
|
|
} else if (deviceConnectionState == DeviceConnectionState.disconnected) {
|
|
print("444444");
|
|
dismissEasyLoading();
|
|
cancelBlueConnetctToastTimer();
|
|
state.saveBtnIsUsable.value = true;
|
|
if(state.ifCurrentScreen.value == true){
|
|
showBlueConnetctToast();
|
|
}
|
|
}
|
|
}, isAddEquipment: true);
|
|
}
|
|
|
|
void bindBlueAdmin() async{
|
|
// print("state.lockInfo:${state.lockInfo}");
|
|
|
|
var positionMap = {};
|
|
positionMap['longitude'] = state.addressInfo["longitude"];
|
|
positionMap['latitude'] = state.addressInfo["latitude"];
|
|
positionMap['country'] = state.addressInfo["country"];
|
|
positionMap['province'] = state.addressInfo["province"];
|
|
positionMap['city'] = state.addressInfo["city"];
|
|
positionMap['district'] = state.addressInfo["district"];
|
|
positionMap['township'] = state.addressInfo["street"];
|
|
positionMap['address'] = state.addressInfo["address"];
|
|
|
|
var bluetooth = {};
|
|
bluetooth['bluetoothDeviceId'] = BlueManage().connectDeviceMacAddress;
|
|
bluetooth['bluetoothDeviceName'] = BlueManage().connectDeviceName;
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
bluetooth['publicKey'] = publicKeyDataList;
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
bluetooth['privateKey'] = getPrivateKeyList;
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
bluetooth['signKey'] = signKeyDataList;
|
|
|
|
// print("addUser:publicKeyDataList$publicKeyDataList getPrivateKeyList:$getPrivateKeyList signKeyDataList:$signKeyDataList");
|
|
|
|
var entity = await ApiRepository.to.bindingBlueAdmin(
|
|
lockAlias: state.aliName.value,
|
|
position: positionMap,
|
|
bluetooth: bluetooth,
|
|
lockInfo: state.lockInfo,
|
|
lockUserNo: state.lockUserNo.toString(),
|
|
pwdTimestamp: state.pwdTimestamp.value.toString(),
|
|
featureValue: state.featureValue,
|
|
featureSettingValue: state.featureSettingValue,
|
|
featureSettingParams: state.featureSettingParams,
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.lockId = entity.data!.lockId!;
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
senderCustomPasswords();
|
|
|
|
// BlueManage().disconnect(BlueManage().connectDeviceMacAddress);
|
|
// eventBus.fire(RefreshLockListInfoDataEvent());
|
|
// Get.close(state.isFromMap == 1 ? 5 : 6);
|
|
}
|
|
}
|
|
|
|
// 更新管理员密码
|
|
void addLockAdminPassword(bool isAddLockAdminPassword) async {
|
|
var entity = await ApiRepository.to.setAdminPasswordData(
|
|
lockId: state.lockId,
|
|
adminPwd: state.adminPasswordTF.text,
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
if(isAddLockAdminPassword == true){
|
|
showDeletPasswordAlertDialog();
|
|
}else{
|
|
backAction();
|
|
}
|
|
}
|
|
}
|
|
|
|
void showDeletPasswordAlertDialog() {
|
|
showDialog(
|
|
context: Get.context!,
|
|
builder: (BuildContext context) {
|
|
return ShowTFView(
|
|
title: "管理员密码",
|
|
tipTitle: "如需修改,请输入新的管理员密码(6位),点击确定即可修改",
|
|
leftBtnTitle: "确定",
|
|
rightBtnTitle: "修改",
|
|
keyboardType: TextInputType.number,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.allow(RegExp('[0-9]')),
|
|
LengthLimitingTextInputFormatter(6),
|
|
],
|
|
controller: state.adminPasswordTF,
|
|
sureClick: () {
|
|
if(state.adminPasswordTF.text.length < 6){
|
|
showToast("请输入6位管理员密码");
|
|
return;
|
|
}
|
|
|
|
if(state.adminPasswordTF.text == state.adminPassword){
|
|
showToast("请输入新的管理员密码");
|
|
return;
|
|
}
|
|
changeAdministratorPasswordCommand();
|
|
},
|
|
cancelClick: () {
|
|
backAction();
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void backAction(){
|
|
BlueManage().disconnect(BlueManage().connectDeviceMacAddress);
|
|
Get.close(state.isFromMap == 1 ? 6 : 7);
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
_replySubscription.cancel();
|
|
BlueManage().stopScan();
|
|
super.onClose();
|
|
}
|
|
}
|