import 'dart:async'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:get/get.dart'; import 'package:star_lock/blue/io_protocol/io_updataLockRemoteControlList.dart'; import 'package:star_lock/login/login/entity/LoginEntity.dart'; import 'package:star_lock/tools/baseGetXController.dart'; import 'package:star_lock/tools/commonDataManage.dart'; import '../../../../app_settings/app_settings.dart'; import '../../../../blue/blue_manage.dart'; import '../../../../blue/io_protocol/io_updataLockCardList.dart'; import '../../../../blue/io_protocol/io_updataLockFaceList.dart'; import '../../../../blue/io_protocol/io_updataLockFingerprintList.dart'; import '../../../../blue/io_protocol/io_updataLockPalmVeinList.dart'; import '../../../../blue/io_protocol/io_updataLockPasswordList.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/storage.dart'; import 'uploadData_state.dart'; class UploadDataLogic extends BaseGetXController{ UploadDataState state = UploadDataState(); // 监听蓝牙协议返回结果 late StreamSubscription _replySubscription; void _initReplySubscription() { _replySubscription = EventBusManager().eventBus!.on().listen((Reply reply) async { // 上传数据获取锁密码列表 if (reply is UpdataLockPasswordListReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockPasswordListReply(reply); } // 上传数据获取锁卡列表 if (reply is UpdataLockCardListReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockCardListReply(reply); } // 上传数据获取锁指纹列表 if (reply is UpdataLockFingerprintListReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockFingerprintListReply(reply); } // 上传数据获取锁人脸列表 if (reply is UpdataLockFaceListReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockFaceListReply(reply); } // 上传数据获取锁掌静脉列表 if (reply is UpdataLockPalmVeinListReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockPalmVeinListReply(reply); } // 上传数据获取锁遥控 if (reply is UpdataLockRemoteControlListReply && (state.ifCurrentScreen.value == true)) { _replyUpdataLockRemoteControlListReply(reply); } // 上传数据获取锁设置 if (reply is UpdataLockSetReply && (state.ifCurrentScreen.value == true) && (state.ifSeletUpdataBtnState == true)) { _replyUpdataLockSetReply(reply); } }); } // 上传数据获取锁密码列表 Future _replyUpdataLockPasswordListReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 cancelBlueConnetctToastTimer(); final int dataLength = reply.data[8]; state.uploadPasswordDataList.addAll(reply.data.sublist(9, reply.data.length)); if(dataLength == 10){ // 当数据是10的时候继续请求 state.uploadPasswordPage = state.uploadPasswordPage + 1; final List token = reply.data.sublist(3, 7); showEasyLoading(); updataLockPasswordList(token, state.uploadPasswordPage); }else{ // dismissEasyLoading(); // 当数据不是10的时候解析数据上传 // if(state.uploadPasswordDataList.isEmpty){ // // 如果是空的直接上传下一个 // getUpdataLockCardList(); // }else{ // // 如果不是空的解析数据上传 state.indexCount.value = state.indexCount.value + 1; _lockDataUpload(uploadType:2, recordType:2, records:state.uploadPasswordDataList); // } } break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockPasswordList(token, state.uploadPasswordPage); break; default: AppLog.log('上传密码列表失败 关闭加载菊花'); dismissEasyLoading(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁卡列表 Future _replyUpdataLockCardListReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 cancelBlueConnetctToastTimer(); final int dataLength = reply.data[8]; state.uploadCardDataList.addAll(reply.data.sublist(9, reply.data.length)); if(dataLength == 10){ // 当数据是10的时候继续请求 state.uploadCardPage = state.uploadCardPage + 1; final List token = reply.data.sublist(3, 7); showEasyLoading(); updataLockCardList(token, state.uploadCardPage); }else{ // // 当数据不是10的时候解析数据上传 // if(state.uploadCardDataList.isEmpty){ // // 如果是空的直接上传下一个 // getUpdataLockFingerprintList(); // }else{ // // 如果不是空的解析数据上传 state.indexCount.value = state.indexCount.value + 1; _lockDataUpload(uploadType:2, recordType:3, records:state.uploadCardDataList); // } } break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockCardList(token, state.uploadCardPage); break; default: dismissEasyLoading(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁指纹列表 Future _replyUpdataLockFingerprintListReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 cancelBlueConnetctToastTimer(); final int dataLength = reply.data[8]; state.uploadFingerprintDataList.addAll(reply.data.sublist(9, reply.data.length)); if(dataLength == 10){ // 当数据是10的时候继续请求 state.uploadFingerprintPage = state.uploadFingerprintPage + 1; final List token = reply.data.sublist(3, 7); showEasyLoading(); updataLockFingerprintList(token, state.uploadFingerprintPage); }else{ // // 当数据不是10的时候解析数据上传 // if(state.uploadFingerprintDataList.isEmpty){ // // 如果是空的直接上传下一个 // getUpdataLockFaceList(); // }else{ // // 如果不是空的解析数据上传 state.indexCount.value = state.indexCount.value + 1; _lockDataUpload(uploadType:2, recordType:4, records:state.uploadFingerprintDataList); // } } break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockFingerprintList(token, state.uploadFingerprintPage); break; default: dismissEasyLoading(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁人脸列表解析 Future _replyUpdataLockFaceListReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 cancelBlueConnetctToastTimer(); final int dataLength = reply.data[8]; state.uploadFaceDataList.addAll(reply.data.sublist(9, reply.data.length)); if(dataLength == 10){ // 当数据是10的时候继续请求 state.uploadFacePage = state.uploadFacePage + 1; final List token = reply.data.sublist(3, 7); showEasyLoading(); updataLockFaceList(token, state.uploadFacePage); }else{ // // 当数据不是10的时候解析数据上传 // if(state.uploadFaceDataList.isEmpty){ // // 如果是空的直接上传下一个 // getUpdataLockPalmVeinList(); // }else{ // 如果不是空的解析数据上传 state.indexCount.value = state.indexCount.value + 1; _lockDataUpload(uploadType:2, recordType:5, records:state.uploadFaceDataList); // } } break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockFaceList(token, state.uploadFacePage); break; default: dismissEasyLoading(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁掌静脉列表解析 Future _replyUpdataLockPalmVeinListReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 cancelBlueConnetctToastTimer(); final int dataLength = reply.data[8]; state.uploadPalmVeinDataList.addAll(reply.data.sublist(9, reply.data.length)); if(dataLength == 10){ // 当数据是10的时候继续请求 state.uploadPalmVeinPage = state.uploadPalmVeinPage + 1; final List token = reply.data.sublist(3, 7); showEasyLoading(); updataLockPalmVeinList(token, state.uploadPalmVeinPage); }else{ // // 当数据不是10的时候解析数据上传 // if(state.uploadPalmVeinDataList.isEmpty){ // // 不需要上传 如果是空的直接上传下一个 // getUpdataLockSet(); // }else{ // // 如果不是空的解析数据上传 state.indexCount.value = state.indexCount.value + 1; _lockDataUpload(uploadType:2, recordType:6, records:state.uploadPalmVeinDataList); // } } break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockPalmVeinList(token, state.uploadPalmVeinPage); break; default: dismissEasyLoading(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁遥控列表解析 Future _replyUpdataLockRemoteControlListReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 cancelBlueConnetctToastTimer(); final int dataLength = reply.data[8]; state.uploadRemoteControlDataList.addAll(reply.data.sublist(9, reply.data.length)); if(dataLength == 10){ // 当数据是10的时候继续请求 state.uploadRemoteControlPage = state.uploadRemoteControlPage + 1; final List token = reply.data.sublist(3, 7); showEasyLoading(); updataLockRemoteControlList(token, state.uploadRemoteControlPage); }else{ // // 当数据不是10的时候解析数据上传 // if(state.uploadPalmVeinDataList.isEmpty){ // // 不需要上传 如果是空的直接上传下一个 // getUpdataLockSet(); // }else{ // // 如果不是空的解析数据上传 state.indexCount.value = state.indexCount.value + 1; _lockDataUpload(uploadType:2, recordType:7, records:state.uploadRemoteControlDataList); // } } break; case 0x06: //无权限 final List token = reply.data.sublist(3, 7); final List saveStrList = changeIntListToStringList(token); Storage.setStringList(saveBlueToken, saveStrList); updataLockRemoteControlList(token, state.uploadRemoteControlPage); break; default: dismissEasyLoading(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁设置解析 Future _replyUpdataLockSetReply(Reply reply) async { final int status = reply.data[2]; switch(status){ case 0x00: //成功 state.indexCount.value = state.indexCount.value + 1; cancelBlueConnetctToastTimer(); state.uploadLockSetDataList.addAll(reply.data.sublist(7, reply.data.length)); _lockDataUpload(uploadType:1, recordType:0, records:state.uploadLockSetDataList); 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(); state.sureBtnState.value = 0; state.indexCount.value = 0; cancelBlueConnetctToastTimer(); break; } } // 上传数据获取锁密码列表 Future getUpdataLockPasswordList() async { if(state.sureBtnState.value == 1){ return; } state.sureBtnState.value = 1; showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); AppLog.log('上传密码列表获取超时 关闭加载菊花'); state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockPasswordList(getTokenList, state.uploadPasswordPage); } else if (connectionState == BluetoothConnectionState.disconnected) { AppLog.log('上传密码列表连接失败 关闭加载菊花'); dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 公共的获取密码列表 Future updataLockPasswordList(List token, int page) 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.updataLockPasswordListCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), page: page, countReq: state.countReq, token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList ); } // 上传数据获取锁Card列表 Future getUpdataLockCardList() async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.indexCount.value = 0; state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockCardList(getTokenList, state.uploadCardPage); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; state.indexCount.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 公共的获取Card列表 Future updataLockCardList(List token, int page) 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.updataLockCardListCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), page: page, countReq: state.countReq, token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList ); } // 上传数据获取锁指纹列表 Future getUpdataLockFingerprintList() async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.indexCount.value = 0; state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockFingerprintList(getTokenList, state.uploadFingerprintPage); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; state.indexCount.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 公共的获取指纹列表 Future updataLockFingerprintList(List token, int page) 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.updataLockFingerprintListCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), page: page, countReq: state.countReq, token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList ); } // 上传数据获取锁人脸列表 Future getUpdataLockFaceList() async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.indexCount.value = 0; state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockFaceList(getTokenList, state.uploadFacePage); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; state.indexCount.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 公共的获取人脸列表 Future updataLockFaceList(List token, int page) 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.updataLockFaceListCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), page: page, countReq: state.countReq, token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList ); } // 上传数据获取锁掌静脉列表 Future getUpdataLockPalmVeinList() async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.indexCount.value = 0; state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockPalmVeinList(getTokenList, state.uploadPalmVeinPage); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; state.indexCount.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 公共的获取掌静脉列表 Future updataLockPalmVeinList(List token, int page) 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.updataLockPalmVeinListCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), page: page, countReq: state.countReq, token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList ); } // 上传数据获取锁遥控列表 Future getUpdataLockRemoteControlList() async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.indexCount.value = 0; state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); updataLockRemoteControlList(getTokenList, state.uploadRemoteControlPage); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; state.indexCount.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 公共的获取遥控列表 Future updataLockRemoteControlList(List token, int page) 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.updataLockRemoteControlListCommand( lockID: BlueManage().connectDeviceName, userID: await Storage.getUid(), page: page, countReq: state.countReq, token: token, needAuthor: 1, signKey: signKeyDataList, privateKey: getPrivateKeyList ); } // 上传数据获取设置 Future getUpdataLockSet() async { showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.indexCount.value = 0; state.sureBtnState.value = 0; }); 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) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; state.indexCount.value = 0; 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: false ); if(entity.errorCode!.codeIsSuccessful){ if(uploadType == 1){ // 1设置 state.indexCount.value = 0; state.sureBtnState.value = 0; showToast('上传成功'.tr); }else{ // 2开门方式 switch(recordType){ // case 1: // // 电子钥匙 // // break; case 2: // 密码上传成功之后,获取卡列表 getUpdataLockCardList(); break; case 3: // IC卡上传成功,获取指纹列表 getUpdataLockFingerprintList(); break; case 4: // 指纹上传成功,获取人脸列表 getUpdataLockFaceList(); break; case 5: // 人脸上传成功,获取掌静脉列表 getUpdataLockPalmVeinList(); break; case 6: // 掌静脉上传成功,获取锁遥控 getUpdataLockRemoteControlList(); break; case 7: // 遥控上传成功,获取锁设置 getUpdataLockSet(); break; default: break; } } }else{ state.indexCount.value = 0; state.sureBtnState.value = 0; dismissEasyLoading(); } } @override void onReady() { super.onReady(); _initReplySubscription(); // getUpdataLockPalmVeinList(); } @override void onClose() { super.onClose(); _replySubscription.cancel(); } }