314 lines
10 KiB
Dart
314 lines
10 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_deletUser.dart';
|
||
import 'package:star_lock/blue/io_type.dart';
|
||
|
||
import '../../../../appRouters.dart';
|
||
import '../../../../blue/blue_manage.dart';
|
||
import '../../../../blue/io_protocol/io_factoryDataReset.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/storage.dart';
|
||
import '../../../../tools/toast.dart';
|
||
import 'CheckingInInfoDataEntity.dart';
|
||
import 'lockSet_state.dart';
|
||
|
||
typedef BlockSetStateCallback = void Function();
|
||
typedef BlockSetCheckInCallback = void Function(
|
||
CheckingInInfoDataEntity checkingInInfoDataEntity);
|
||
|
||
class LockSetLogic extends BaseGetXController {
|
||
final LockSetState state = LockSetState();
|
||
|
||
// 监听蓝牙协议返回结果
|
||
late StreamSubscription<Reply> _replySubscription;
|
||
void _initReplySubscription() {
|
||
_replySubscription =
|
||
EventBusManager().eventBus!.on<Reply>().listen((reply) async {
|
||
// 删除用户
|
||
if (reply is DeletUserReply) {
|
||
_replyDeletUserKey(reply);
|
||
}
|
||
|
||
// 恢复出厂设置
|
||
if(reply is FactoryDataResetReply){
|
||
_replyFactoryDataResetKey(reply);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 删除用户
|
||
Future<void> _replyDeletUserKey(Reply reply) async {
|
||
var tokenData = reply.data.sublist(2, 6);
|
||
var saveStrList = changeIntListToStringList(tokenData);
|
||
print("openDoorToken:$tokenData");
|
||
Storage.setStringList(saveBlueToken, saveStrList);
|
||
|
||
int status = reply.data[6];
|
||
print("status:$status");
|
||
switch (status) {
|
||
case 0x00:
|
||
//成功
|
||
print("${reply.commandType}解析成功");
|
||
deletLockInfoData();
|
||
break;
|
||
case 0x06:
|
||
//无权限
|
||
print("${reply.commandType}需要鉴权");
|
||
|
||
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
||
List<int> getPrivateKeyList =
|
||
changeStringListToIntList(privateKey!);
|
||
|
||
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
||
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
||
|
||
IoSenderManage.deletUser(
|
||
lockID:
|
||
state.getKeyInfosData.value.bluetooth!.bluetoothDeviceName,
|
||
authUserID: await Storage.getUid(),
|
||
keyID: "1",
|
||
delUserID: await Storage.getUid(),
|
||
needAuthor: 1,
|
||
publicKey: publicKeyDataList,
|
||
privateKey: getPrivateKeyList,
|
||
token: tokenData);
|
||
break;
|
||
case 0x07:
|
||
//无权限
|
||
print("${reply.commandType}用户无权限");
|
||
|
||
break;
|
||
case 0x09:
|
||
// 权限校验错误
|
||
print("${reply.commandType}权限校验错误");
|
||
|
||
break;
|
||
default:
|
||
//失败
|
||
print("${reply.commandType}失败");
|
||
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 恢复出厂设置数据解析
|
||
Future<void> _replyFactoryDataResetKey(Reply reply) async {
|
||
var token = reply.data.sublist(2, 6);
|
||
var saveStrList = changeIntListToStringList(token);
|
||
print("_replyFactoryDataResetKeyToken:$token");
|
||
Storage.setStringList(saveBlueToken, saveStrList);
|
||
|
||
int status = reply.data[6];
|
||
print("status:$status");
|
||
|
||
switch(status){
|
||
case 0x00:
|
||
//成功
|
||
print("${reply.commandType!.typeValue} 数据解析成功");
|
||
|
||
break;
|
||
case 0x06:
|
||
//无权限
|
||
print("${reply.commandType!.typeValue} 需要鉴权");
|
||
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!);
|
||
|
||
IoSenderManage.senderFactoryDataReset(
|
||
lockID:BlueManage().connectDeviceName,
|
||
userID:await Storage.getUid(),
|
||
keyID:"1",
|
||
needAuthor:1,
|
||
publicKey:publicKeyDataList,
|
||
privateKey:getPrivateKeyList,
|
||
token: getTokenList
|
||
);
|
||
break;
|
||
case 0x07:
|
||
//无权限
|
||
print("${reply.commandType!.typeValue} 用户无权限");
|
||
|
||
break;
|
||
case 0x09:
|
||
// 权限校验错误
|
||
print("${reply.commandType!.typeValue} 权限校验错误");
|
||
|
||
break;
|
||
default:
|
||
//失败
|
||
print("${reply.commandType!.typeValue} 失败");
|
||
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 删除用户蓝牙协议
|
||
Future<void> deletUserAction() async {
|
||
BlueManage().judgeReconnect(
|
||
BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName,
|
||
(DeviceConnectionState connectionState) async {
|
||
if (connectionState == DeviceConnectionState.connected) {
|
||
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!);
|
||
|
||
IoSenderManage.deletUser(
|
||
lockID: state.getKeyInfosData.value.bluetooth!.bluetoothDeviceName,
|
||
authUserID: "100001",
|
||
keyID: "1",
|
||
delUserID: "100001",
|
||
needAuthor: 1,
|
||
publicKey: publicKeyDataList,
|
||
privateKey: getPrivateKeyList,
|
||
token: getTokenList);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 恢复出厂设置
|
||
Future<void> factoryDataResetAction() async {
|
||
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName, (DeviceConnectionState state) async {
|
||
if (state == DeviceConnectionState.connected){
|
||
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!);
|
||
|
||
IoSenderManage.senderFactoryDataReset(
|
||
lockID:BlueManage().connectDeviceName,
|
||
userID:await Storage.getUid(),
|
||
keyID:"1",
|
||
needAuthor:1,
|
||
publicKey:publicKeyDataList,
|
||
privateKey:getPrivateKeyList,
|
||
token: getTokenList
|
||
);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 删除锁调用后台接口
|
||
void deletLockInfoData() async {
|
||
var entity = await ApiRepository.to.deletLockData(
|
||
lockId: state.getKeyInfosData.value.lockId.toString(),
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
Get.offAllNamed(Routers.starLockMain);
|
||
}
|
||
}
|
||
|
||
// 查询账户密码
|
||
void checkLoginPassword() async {
|
||
var entity = await ApiRepository.to.checkLoginPassword(
|
||
password: state.passwordTF.text,
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
|
||
}
|
||
}
|
||
|
||
// 开启考勤获取是否有公司
|
||
void openCheckingInData(
|
||
BlockSetCheckInCallback blockSetCheckInCallback) async {
|
||
var entity = await ApiRepository.to.openCheckingInData(
|
||
lockId: state.getKeyInfosData.value.lockId.toString(),
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
blockSetCheckInCallback(entity);
|
||
}
|
||
}
|
||
|
||
// 设置是否打开考勤
|
||
void setLockSetGeneralSetting(String isOn) async {
|
||
var entity = await ApiRepository.to.setLockSetGeneralSettingData(
|
||
lockId: state.getKeyInfosData.value.lockId.toString(),
|
||
changeType: "1",
|
||
isOn: isOn,
|
||
type: "1",
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
eventBus.fire(RefreshLockListInfoDataEvent());
|
||
|
||
state.getKeyInfosData.value.isAttendance = int.parse(isOn);
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.getKeyInfosData.value));
|
||
Toast.show(msg: "设置成功");
|
||
} else {}
|
||
}
|
||
|
||
// 设置是否打开开锁提醒
|
||
void setLockPickingReminder() async {
|
||
var entity = await ApiRepository.to.setLockPickingReminderData(
|
||
lockId: state.getKeyInfosData.value.lockId.toString(),
|
||
keyId: state.getKeyInfosData.value.keyId.toString(),
|
||
monitorFlag:
|
||
state.isLockPickingReminder.value == 1 ? "1" : "2", // 1开启,2关闭
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
eventBus.fire(RefreshLockListInfoDataEvent());
|
||
|
||
state.getKeyInfosData.value.monitorFlag =
|
||
state.isLockPickingReminder.value;
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.getKeyInfosData.value));
|
||
Toast.show(msg: "设置成功");
|
||
} else {}
|
||
}
|
||
|
||
// 下级界面修改成功后传递数据
|
||
StreamSubscription? _passCurrentLockInformationEvent;
|
||
void initLoadDataAction(BlockSetStateCallback blockSetStateCallback) {
|
||
// 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus
|
||
_passCurrentLockInformationEvent =
|
||
eventBus.on<PassCurrentLockInformationEvent>().listen((event) {
|
||
state.getKeyInfosData.value = event.keyInfo;
|
||
blockSetStateCallback();
|
||
});
|
||
}
|
||
|
||
@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();
|
||
_passCurrentLockInformationEvent!.cancel();
|
||
}
|
||
}
|