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/login/login/entity/LoginEntity.dart'; import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.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_protocol/io_updataLockSet.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 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); // } // 上传数据获取锁设置 if (reply is UpdataLockSetReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockSetReply(reply); } }); } // 删除用户 // Future _replyDeletUserKey(Reply reply) async { // var tokenData = reply.data.sublist(2, 6); // var saveStrList = changeIntListToStringList(tokenData); // Storage.setStringList(saveBlueToken, saveStrList); // // int status = reply.data[6]; // switch (status) { // case 0x00: // //成功 // dismissEasyLoading(); // cancelBlueConnetctToastTimer(); // deletKeyData(); // break; // case 0x06: // //无权限 // 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; // default: // //失败 // dismissEasyLoading(); // showToast("删除失败"); // break; // } // } // 恢复出厂设置数据解析 Future _replyFactoryDataResetKey(Reply reply) async { final List token = reply.data.sublist(2, 6); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); final int status = reply.data[6]; switch (status) { case 0x00: //成功 dismissEasyLoading(); cancelBlueConnetctToastTimer(); deletLockInfoData(); break; case 0x06: //无权限 final List? privateKey = await Storage.getStringList(saveBluePrivateKey); final List getPrivateKeyList = changeStringListToIntList(privateKey!); final List? publicKey = await Storage.getStringList(saveBluePublicKey); final List publicKeyDataList = changeStringListToIntList(publicKey!); final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); IoSenderManage.senderFactoryDataReset( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), keyID: '1', needAuthor: 1, publicKey: publicKeyDataList, privateKey: getPrivateKeyList, token: getTokenList); break; default: //失败 dismissEasyLoading(); cancelBlueConnetctToastTimer(); showDeletAlertTipDialog(); break; } } // 读取支持功能带参数数据解析 // Future _readSupportFunctionsWithParametersReply(Reply reply) async { // int status = reply.data[2]; // switch (status) { // case 0x00: // //成功 // state.isOpenBlueBroadcast.value = reply.data[6]; // break; // case 0x06: // //无权限 // break; // default: // //失败 // break; // } // } // 锁设置数据解析 Future _replySetSupportFunctionsWithParameters(Reply reply) async { final int status = reply.data[2]; switch (status) { case 0x00: //成功 // Toast.show(msg: "操作成功");featureEnable = state.isOpenStayWarn.value == 1 ? 0 : 1; dismissEasyLoading(); state.sureBtnState.value = 0; 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; } break; case 0x06: //无权限 state.sureBtnState.value = 0; break; default: state.sureBtnState.value = 0; break; } } // 上传数据获取锁设置解析 Future _replyUpdataLockSetReply(Reply reply) async { final int status = reply.data[2]; switch (status) { case 0x00: //成功 dismissEasyLoading(); cancelBlueConnetctToastTimer(); _lockDataUpload( uploadType: 1, recordType: 0, records: reply.data.sublist(7, reply.data.length)); break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockSet(token); break; default: dismissEasyLoading(); cancelBlueConnetctToastTimer(); break; } } // 删除用户 // Future deletUserAction() async { // showEasyLoading(); // showBlueConnetctToastTimer(isShowBlueConnetctToast: false, action: (){ // dismissEasyLoading(); // 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(); // if(state.ifCurrentScreen.value == true){ // showDeletAlertTipDialog(); // } // } // }); // } // 恢复出厂设置 Future factoryDataResetAction() async { showEasyLoading(); showBlueConnetctToastTimer( isShowBlueConnetctToast: false, action: () { dismissEasyLoading(); showDeletAlertTipDialog(); }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? privateKey = await Storage.getStringList(saveBluePrivateKey); final List getPrivateKeyList = changeStringListToIntList(privateKey!); final List? publicKey = await Storage.getStringList(saveBluePublicKey); final List publicKeyDataList = changeStringListToIntList(publicKey!); final List? token = await Storage.getStringList(saveBlueToken); final 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 { if(state.sureBtnState.value == 1){ return; } state.sureBtnState.value = 1; showEasyLoading(); showBlueConnetctToastTimer(action: () { state.sureBtnState.value = 0; dismissEasyLoading(); }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? privateKey = await Storage.getStringList(saveBluePrivateKey); final List getPrivateKeyList = changeStringListToIntList(privateKey!); final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); final List? publicKey = await Storage.getStringList(saveBluePublicKey); final List getPublicKeyList = changeStringListToIntList(publicKey!); state.settingUpSupportFeatures = type; int 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(); state.sureBtnState.value = 0; if (state.ifCurrentScreen.value == true) { showBlueConnetctToast(); } } }); } // 获取锁设置信息 Future getLockSettingInfoData() async { final 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!; state.isUnlockReminder.value = state.lockSettingInfo.value.unlockReminder!; } return entity; } // 开启考勤获取是否有公司 Future openCheckingInData( BlockSetCheckInCallback blockSetCheckInCallback) async { final CheckingInInfoDataEntity entity = await ApiRepository.to.openCheckingInData( lockId: state.lockSetInfoData.value.lockId.toString(), ); if (entity.errorCode!.codeIsSuccessful) { blockSetCheckInCallback(entity); } } // 设置是否打开考勤 Future setLockSetGeneralSetting() async { final LoginEntity 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())); }); } } // 设置是否打开开锁提醒 Future setLockPickingReminder() async { final LoginEntity entity = await ApiRepository.to.setLockPickingReminderData( lockId: state.lockSetInfoData.value.lockId!, unlockReminder: state.isUnlockReminder.value == 1 ? 0 : 1, ); if (entity.errorCode!.codeIsSuccessful) { state.isUnlockReminder.value = (state.isUnlockReminder.value == 1 ? 0 : 1); state.lockSettingInfo.value.unlockReminder = state.isUnlockReminder.value; showToast('设置成功'.tr, something: () { eventBus.fire(RefreshLockListInfoDataEvent()); }); } } // 设置是否打开开锁时是否需联网 Future setLockSetOpenLockNeedOnline() async { final LoginEntity 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()); }); } } // 下级界面修改成功后传递数据 StreamSubscription? _passCurrentLockInformationEvent; void initLoadDataAction(BlockSetStateCallback blockSetStateCallback) { // 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus _passCurrentLockInformationEvent = eventBus .on() .listen((PassCurrentLockInformationEvent event) { getLockSettingInfoData(); }); } void showDeletAlertTipDialog({String? showContent = ''}) { final String content = showContent!.isEmpty ? "${"删除设备失败,请确保在设备附近,设备未被连接,设备已打开".tr}${BlueManage().connectDeviceName!.contains("T9A") == true ? "。如果是全自动锁,请使屏幕变亮".tr : ""}" : showContent; state.showTipView.showSureAlertDialog(content); } /// 以下为删除逻辑 void deleyLockLogicOfRoles() { if (state.lockBasicInfo.value.isLockOwner == 1) { // 超级管理员必须通过连接蓝牙删除 state.showTipView.showIosTipWithContentDialog('删除锁后,所有信息都会一起删除,确定删除锁吗?'.tr, () { // 删除锁 state.showTipView.resetGetController(); state.showTipView.showTFViewAlertDialog(state.passwordTF, '请输入登录密码'.tr, '请输入登录密码'.tr, checkLoginPassword); }); } else if (state.lockBasicInfo.value.keyRight == 1) { // 授权管理员弹框提示 state.showTipView.showDeleteAdministratorIsHaveAllDataDialog( '同时删除其发送的所有钥匙,钥匙删除后不能恢复'.tr, (bool a) { // 授权管理员删除 state.deleteAdministratorIsHaveAllData.value = a; deletKeyData(); }); } else { // 普通用户直接删除 state.showTipView.showIosTipWithContentDialog('是否删除钥匙?'.tr, deletKeyData); } } // 查询账户密码 Future checkLoginPassword() async { final LockListInfoEntity entity = await ApiRepository.to.checkLoginPassword( password: state.passwordTF.text, ); if (entity.errorCode!.codeIsSuccessful) { Get.back(); factoryDataResetAction(); } } // 当是锁拥有者的时候,删除锁 Future deletLockInfoData() async { final LockListInfoEntity entity = await ApiRepository.to.deletOwnerLockData( lockId: state.lockSetInfoData.value.lockId!, ); if (entity.errorCode!.codeIsSuccessful) { BlueManage().connectDeviceMacAddress = ''; final bool isOnlyOneData = state.isOnlyOneData.value == true; await Future.delayed(const Duration(milliseconds: 200)) .then((e) { Get.close(isOnlyOneData ? 1 : 2); }); SchedulerBinding.instance.addPostFrameCallback((_) { eventBus.fire(RefreshLockListInfoDataEvent()); }); } } // 普通用户或者授权管理员删除钥匙 Future deletKeyData() async { final LockListInfoEntity 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 = ''; final bool isOnlyOneData = state.isOnlyOneData.value == true; await Future.delayed(const Duration(milliseconds: 200)) .then((e) { Get.close(isOnlyOneData ? 1 : 2); }); SchedulerBinding.instance.addPostFrameCallback((_) { eventBus.fire(RefreshLockListInfoDataEvent()); }); } } // 上传数据获取设置 Future getUpdataLockSet() async { // showBlueConnetctToastTimer(action: (){ // }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockSet(getTokenList); } else if (connectionState == BluetoothConnectionState.disconnected) { // cancelBlueConnetctToastTimer(); // if(state.ifCurrentScreen.value == true){ // showBlueConnetctToast(); // } } }); } // 公共的上传锁设置 Future updataLockSet(List token) async { final List? privateKey = await Storage.getStringList(saveBluePrivateKey); final List getPrivateKeyList = changeStringListToIntList(privateKey!); final List? signKey = await Storage.getStringList(saveBlueSignKey); final List signKeyDataList = changeStringListToIntList(signKey!); IoSenderManage.updataLockSetCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList); } // 锁数据上传服务器 Future _lockDataUpload( {required int uploadType, required int recordType, required List records}) async { final LoginEntity entity = await ApiRepository.to.lockDataUpload( lockId: CommonDataManage().currentKeyInfo.lockId!, uploadType: uploadType, recordType: recordType, records: records, isUnShowLoading: true); if (entity.errorCode!.codeIsSuccessful) { await getLockSettingInfoData(); update(); } } @override void onReady() { super.onReady(); getUpdataLockSet(); _initReplySubscription(); // _scanListDiscoveredDeviceSubscriptionAction(); } @override void onClose() { _replySubscription.cancel(); _passCurrentLockInformationEvent!.cancel(); // _scanListDiscoveredDeviceSubscription.cancel(); } }