import 'dart:async'; import 'package:flutter/scheduler.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:get/get.dart'; import 'package:star_lock/blue/io_type.dart'; import 'package:star_lock/tools/showTipView.dart'; import '../../../../blue/blue_manage.dart'; import '../../../../blue/io_protocol/io_factoryDataReset.dart'; import '../../../../blue/io_protocol/io_setSupportFunctionsNoParameters.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/commonDataManage.dart'; import '../../../../tools/eventBusEventManage.dart'; import '../../../../tools/storage.dart'; import 'checkingInInfoData_entity.dart'; import 'lockSetInfo_entity.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 _replySubscription; void _initReplySubscription() { _replySubscription = EventBusManager().eventBus!.on().listen((reply) async { // 删除用户 // if (reply is DeletUserReply) { // _replyDeletUserKey(reply); // } // 恢复出厂设置 if (reply is FactoryDataResetReply && (state.ifCurrentScreen.value == true)) { _replyFactoryDataResetKey(reply); } // 设置支持功能(不带参数) if ((reply is SetSupportFunctionsNoParametersReply) && (state.ifCurrentScreen.value == true)) { _replySetSupportFunctionsWithParameters(reply); } // 读取支持功能(不带参数) // if ((reply is ReadSupportFunctionsNoParametersReply) && (state.settingUpSupportFeatures == 56)) { // _readSupportFunctionsWithParametersReply(reply); // } }); } // 删除用户 // Future _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}解析成功"); // dismissEasyLoading(); // cancelBlueConnetctToastTimer(); // deletKeyData(); // break; // case 0x06: // //无权限 // print("${reply.commandType}需要鉴权"); // // var privateKey = await Storage.getStringList(saveBluePrivateKey); // List getPrivateKeyList = changeStringListToIntList(privateKey!); // // var publicKey = await Storage.getStringList(saveBluePublicKey); // List publicKeyDataList = changeStringListToIntList(publicKey!); // // IoSenderManage.deletUser( // lockID: BlueManage().connectDeviceName, // authUserID: await Storage.getUid(), // keyID: "1", // delUserID: await Storage.getUid(), // needAuthor: 1, // publicKey: publicKeyDataList, // privateKey: getPrivateKeyList, // token: tokenData); // break; // case 0x07: // //无权限 // print("${reply.commandType!.typeValue} 用户无权限"); // dismissEasyLoading(); // showToast("用户无权限"); // break; // case 0x09: // // 权限校验错误 // print("${reply.commandType!.typeValue} "); // dismissEasyLoading(); // showToast("权限校验错误"); // break; // default: // //失败 // print("${reply.commandType!.typeValue} 失败"); // dismissEasyLoading(); // showToast("删除失败"); // break; // } // } // 恢复出厂设置数据解析 Future _replyFactoryDataResetKey(Reply reply) async { var token = reply.data.sublist(2, 6); var saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); int status = reply.data[6]; switch (status) { case 0x00: //成功 Get.log("${reply.commandType!.typeValue} 数据解析成功"); dismissEasyLoading(); cancelBlueConnetctToastTimer(); deletLockInfoData(); break; case 0x06: //无权限 Get.log("${reply.commandType!.typeValue} 需要鉴权"); var privateKey = await Storage.getStringList(saveBluePrivateKey); List getPrivateKeyList = changeStringListToIntList(privateKey!); var publicKey = await Storage.getStringList(saveBluePublicKey); List publicKeyDataList = changeStringListToIntList(publicKey!); var token = await Storage.getStringList(saveBlueToken); List 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: //无权限 Get.log("${reply.commandType!.typeValue} 用户无权限"); dismissEasyLoading(); showToast("用户无权限".tr); break; case 0x09: // 权限校验错误 Get.log("${reply.commandType!.typeValue} "); dismissEasyLoading(); // showToast("权限校验错误"); break; default: //失败 Get.log("${reply.commandType!.typeValue} 失败"); dismissEasyLoading(); // showToast("权限校验错误"); break; } } // 读取支持功能带参数数据解析 // Future _readSupportFunctionsWithParametersReply(Reply reply) async { // int status = reply.data[2]; // switch (status) { // case 0x00: // //成功 // print("${reply.commandType}数据解析成功"); // state.isOpenBlueBroadcast.value = reply.data[6]; // print("isOpenBlueBroadcast:${state.isOpenBlueBroadcast.value}"); // break; // case 0x06: // //无权限 // print("${reply.commandType}需要鉴权"); // break; // case 0x07: // //无权限 // print("${reply.commandType}用户无权限"); // break; // case 0x09: // // 权限校验错误 // print("${reply.commandType}权限校验错误"); // break; // default: // //失败 // print("${reply.commandType}失败"); // break; // } // } // 锁设置数据解析 Future _replySetSupportFunctionsWithParameters(Reply reply) async { int status = reply.data[2]; switch (status) { case 0x00: //成功 Get.log("${reply.commandType}数据解析成功"); // Toast.show(msg: "操作成功");featureEnable = state.isOpenStayWarn.value == 1 ? 0 : 1; dismissEasyLoading(); cancelBlueConnetctToastTimer(); if (state.settingUpSupportFeatures == 55) { // APP开锁时是否需联网 // state.isOpenLockNeedOnline.value = state.isOpenLockNeedOnline.value == 1 ? 0 : 1; // state.lockSetInfoData.value.lockSettingInfo!.appUnlockOnline = state.isOpenLockNeedOnline.value; setLockSetOpenLockNeedOnline(); // eventBus.fire(LockSetChangeSetRefreshLockDetailWithType(0, state.lockSetInfoData.value.lockSettingInfo!.appUnlockOnline!)); } else if (state.settingUpSupportFeatures == 56) { // 蓝牙广播 state.isOpenBlueBroadcast.value = state.isOpenBlueBroadcast.value == 1 ? 0 : 1; state.lockSetInfoData.value.lockSettingInfo!.bluetoothBroadcast = state.isOpenBlueBroadcast.value; } else if (state.settingUpSupportFeatures == 61) { // 逗留警告 state.isOpenStayWarn.value = state.isOpenStayWarn.value == 1 ? 0 : 1; state.lockSetInfoData.value.lockSettingInfo!.stayWarn = state.isOpenStayWarn.value; } else if (state.settingUpSupportFeatures == 62) { // 异常警告 state.isOpenExceptionWarnings.value = state.isOpenExceptionWarnings.value == 1 ? 0 : 1; state.lockSetInfoData.value.lockSettingInfo!.abnormalWarn = state.isOpenExceptionWarnings.value; } Get.log("isOpenBlueBroadcast111:${state.isOpenBlueBroadcast.value}"); break; case 0x06: //无权限 break; default: break; } } // 删除用户 // Future deletUserAction() async { // showEasyLoading(); // showBlueConnetctToastTimer(isShowBlueConnetctToast: false, action: (){ // dismissEasyLoading(); // // print("1111111"); // showDeletAlertTipDialog(); // }); // BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async { // if (connectionState == DeviceConnectionState.connected) { // var privateKey = await Storage.getStringList(saveBluePrivateKey); // List getPrivateKeyList = changeStringListToIntList(privateKey!); // // var publicKey = await Storage.getStringList(saveBluePublicKey); // List publicKeyDataList = changeStringListToIntList(publicKey!); // // var token = await Storage.getStringList(saveBlueToken); // List getTokenList = changeStringListToIntList(token!); // // IoSenderManage.deletUser( // lockID: BlueManage().connectDeviceName, // authUserID: state.lockBasicInfo.value.senderUserId.toString(), // keyID: state.lockBasicInfo.value.keyId.toString(), // delUserID: await Storage.getUid(), // // authUserID: "100001", // // keyID: "1", // // delUserID: "100001", // needAuthor: 1, // publicKey: publicKeyDataList, // privateKey: getPrivateKeyList, // token: getTokenList); // } else if (connectionState == DeviceConnectionState.disconnected) { // dismissEasyLoading(); // cancelBlueConnetctToastTimer(); // print("222222"); // if(state.ifCurrentScreen.value == true){ // showDeletAlertTipDialog(); // } // } // }); // } // 恢复出厂设置 Future factoryDataResetAction() async { showEasyLoading(); showBlueConnetctToastTimer(isShowBlueConnetctToast: false, action: (){ dismissEasyLoading(); showDeletAlertTipDialog(); }); BlueManage().bludSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { var privateKey = await Storage.getStringList(saveBluePrivateKey); List getPrivateKeyList = changeStringListToIntList(privateKey!); var publicKey = await Storage.getStringList(saveBluePublicKey); List publicKeyDataList = changeStringListToIntList(publicKey!); var token = await Storage.getStringList(saveBlueToken); List getTokenList = changeStringListToIntList(token!); IoSenderManage.senderFactoryDataReset( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), keyID: "1", needAuthor: 1, publicKey: publicKeyDataList, privateKey: getPrivateKeyList, token: getTokenList); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); if(state.ifCurrentScreen.value == true){ showDeletAlertTipDialog(); } } }); } // 读取支持功能-带参数 // Future _readSupportFunctionsNoParameters(int type) async { // BlueManage().bludSendData(BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async { // if (connectionState == DeviceConnectionState.connected) { // var privateKey = await Storage.getStringList(saveBluePrivateKey); // List getPrivateKeyList = changeStringListToIntList(privateKey!); // // var token = await Storage.getStringList(saveBlueToken); // List getTokenList = changeStringListToIntList(token!); // // var publicKey = await Storage.getStringList(saveBluePublicKey); // List getPublicKeyList = changeStringListToIntList(publicKey!); // // IoSenderManage.readSupportFunctionsNoParametersCommand( // keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(), // userID: await Storage.getUid(), // featureBit: type, // token: getTokenList, // needAuthor: 1, // publicKey: getPublicKeyList, // privateKey: getPrivateKeyList // ); // } // }, isShowLoading: true); // } // 设置支持功能(带参数) Future sendBurglarAlarm(int type) async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); }); BlueManage().bludSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { var privateKey = await Storage.getStringList(saveBluePrivateKey); List getPrivateKeyList = changeStringListToIntList(privateKey!); var token = await Storage.getStringList(saveBlueToken); List getTokenList = changeStringListToIntList(token!); var publicKey = await Storage.getStringList(saveBluePublicKey); List getPublicKeyList = changeStringListToIntList(publicKey!); state.settingUpSupportFeatures = type; var featureEnable = 0; if (type == 55) { // APP开锁时是否需联网 featureEnable = state.isOpenLockNeedOnline.value == 1 ? 0 : 1; } else if (type == 56) { // 蓝牙广播 featureEnable = state.isOpenBlueBroadcast.value == 1 ? 0 : 1; } else if (type == 61) { // 逗留警告 featureEnable = state.isOpenStayWarn.value == 1 ? 0 : 1; } else if (type == 62) { // 异常警告 featureEnable = state.isOpenExceptionWarnings.value == 1 ? 0 : 1; } IoSenderManage.setSupportFunctionsNoParametersCommand( keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(), userID: await Storage.getUid(), featureBit: type, featureEnable: featureEnable, token: getTokenList, needAuthor: 1, publicKey: getPublicKeyList, privateKey: getPrivateKeyList); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 获取锁设置信息 Future getLockSettingInfoData() async { LockSetInfoEntity entity = await ApiRepository.to.getLockSettingInfoData( lockId: state.lockId.toString(), ); if (entity.errorCode!.codeIsSuccessful) { state.lockSetInfoData.value = entity.data!; CommonDataManage().currentLockSetInfoData = entity.data!; state.lockSettingInfo.value = state.lockSetInfoData.value.lockSettingInfo!; state.lockFeature.value = state.lockSetInfoData.value.lockFeature!; state.lockStatus.value = state.lockSetInfoData.value.lockStatus!; state.lockBasicInfo.value = state.lockSetInfoData.value.lockBasicInfo!; state.isAttendance.value = state.lockSettingInfo.value.attendance!; state.isOpenLockNeedOnline.value = state.lockSettingInfo.value.appUnlockOnline!; state.isOpenBlueBroadcast.value = state.lockSettingInfo.value.bluetoothBroadcast!; state.isOpenExceptionWarnings.value = state.lockSettingInfo.value.bluetoothBroadcast!; } return entity; } // 开启考勤获取是否有公司 void openCheckingInData(BlockSetCheckInCallback blockSetCheckInCallback) async { var entity = await ApiRepository.to.openCheckingInData( lockId: state.lockSetInfoData.value.lockId.toString(), ); if (entity.errorCode!.codeIsSuccessful) { blockSetCheckInCallback(entity); } } // 设置是否打开考勤 void setLockSetGeneralSetting() async { var entity = await ApiRepository.to.setCheckInData( lockId: state.lockSetInfoData.value.lockId!, attendance: state.isAttendance.value == 1 ? 0 : 1, ); if (entity.errorCode!.codeIsSuccessful) { state.isAttendance.value = (state.isAttendance.value == 1 ? 0 : 1); state.lockSettingInfo.value.attendance = state.isAttendance.value; showToast("设置成功".tr, something: (){ eventBus.fire(RefreshLockListInfoDataEvent()); eventBus.fire(LockSetChangeSetRefreshLockDetailWithType(0, state.lockSettingInfo.value.attendance!.toString())); }); } } // 设置是否打开开锁提醒 void setLockPickingReminder() async { var entity = await ApiRepository.to.setLockPickingReminderData( lockId: state.lockSetInfoData.value.lockId!, unlockReminderPush: state.isLockPickingReminder.value == 1 ? 0 : 1, ); if (entity.errorCode!.codeIsSuccessful) { state.isLockPickingReminder.value = (state.isLockPickingReminder.value == 1 ? 0 : 1); state.lockSettingInfo.value.unlockReminderPush = state.isLockPickingReminder.value; showToast("设置成功".tr, something: (){ eventBus.fire(RefreshLockListInfoDataEvent()); }); } } // 设置是否打开开锁时是否需联网 void setLockSetOpenLockNeedOnline() async { var entity = await ApiRepository.to.setOpenLockNeedOnlineData( lockId: state.lockSetInfoData.value.lockId!, appUnlockOnline: state.isOpenLockNeedOnline.value == 1 ? 0 : 1, ); if (entity.errorCode!.codeIsSuccessful) { state.isOpenLockNeedOnline.value = (state.isOpenLockNeedOnline.value == 1 ? 0 : 1); state.lockSettingInfo.value.appUnlockOnline = state.isOpenLockNeedOnline.value; showToast("设置成功".tr,something: (){ eventBus.fire(LockSetChangeSetRefreshLockDetailWithType(1, state.lockSettingInfo.value.appUnlockOnline!.toString())); eventBus.fire(RefreshLockListInfoDataEvent()); // print("state.lockSettingInfo.value.appUnlockOnline:${state.lockSettingInfo.value.appUnlockOnline}"); }); } } // 下级界面修改成功后传递数据 StreamSubscription? _passCurrentLockInformationEvent; void initLoadDataAction(BlockSetStateCallback blockSetStateCallback) { // 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus _passCurrentLockInformationEvent = eventBus.on().listen((event) { getLockSettingInfoData(); }); } void showDeletAlertTipDialog({String? showContent = ""}) { var content = showContent!.isEmpty ? "${"删除设备失败,请确保在设备附近,设备未被连接,设备已打开".tr}${BlueManage().connectDeviceName!.contains("T9A") == true ? "。如果是全自动锁,请使屏幕变亮".tr : ""}" : showContent; ShowTipView().showSureAlertDialog(content); } /// 以下为删除逻辑 deleyLockLogicOfRoles(){ if (state.lockBasicInfo.value.isLockOwner == 1) { // 超级管理员必须通过连接蓝牙删除 ShowTipView().showIosTipWithContentDialog("删除锁后,所有信息都会一起删除,确定删除锁吗?".tr, (){ // 删除锁 ShowTipView().showTFViewAlertDialog(state.passwordTF, "请输入登录密码".tr, "请输入登录密码".tr, (){ checkLoginPassword(); }); }); } else if (state.lockBasicInfo.value.keyRight == 1){ // 授权管理员弹框提示 ShowTipView().showDeleteAdministratorIsHaveAllDataDialog("同时删除其发送的所有钥匙,钥匙删除后不能恢复".tr, (a){ // 授权管理员删除 state.deleteAdministratorIsHaveAllData.value = a; deletKeyData(); }); } else{ // 普通用户直接删除 ShowTipView().showIosTipWithContentDialog("是否删除钥匙?".tr, (){ deletKeyData(); }); } } // 查询账户密码 void checkLoginPassword() async { var entity = await ApiRepository.to.checkLoginPassword( password: state.passwordTF.text, ); if (entity.errorCode!.codeIsSuccessful) { Get.back(); factoryDataResetAction(); } } // 当是锁拥有者的时候,删除锁 void deletLockInfoData() async { var entity = await ApiRepository.to.deletOwnerLockData( lockId: state.lockSetInfoData.value.lockId!, ); if (entity.errorCode!.codeIsSuccessful) { BlueManage().connectDeviceMacAddress = ""; SchedulerBinding.instance.addPostFrameCallback((_) { eventBus.fire(RefreshLockListInfoDataEvent()); }); if(state.isOnlyOneData.value == true){ Future.delayed(const Duration(milliseconds: 200)).then((e) { Get.close(1); }); }else{ Future.delayed(const Duration(milliseconds: 200)).then((e) { Get.close(2); }); } } } // 普通用户或者授权管理员删除钥匙 void deletKeyData() async { var entity = await ApiRepository.to.deletOwnerKeyData( lockId: state.lockSetInfoData.value.lockId.toString(), keyId: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(), includeUnderlings: state.deleteAdministratorIsHaveAllData.value == true ? 1 : 0 ); if (entity.errorCode!.codeIsSuccessful) { BlueManage().connectDeviceMacAddress = ""; SchedulerBinding.instance.addPostFrameCallback((_) { eventBus.fire(RefreshLockListInfoDataEvent()); }); if(state.isOnlyOneData.value == true){ Future.delayed(const Duration(milliseconds: 200)).then((e) { Get.close(1); }); }else{ Future.delayed(const Duration(milliseconds: 200)).then((e) { Get.close(2); }); } // Get.offAllNamed(Routers.starLockMain); } } @override void onReady() { // TODO: implement onReady super.onReady(); _initReplySubscription(); // _scanListDiscoveredDeviceSubscriptionAction(); } @override void onInit() { // TODO: implement onInit super.onInit(); } @override void onClose() { // TODO: implement onClose _replySubscription.cancel(); _passCurrentLockInformationEvent!.cancel(); // _scanListDiscoveredDeviceSubscription.cancel(); } }