473 lines
17 KiB
Dart
Executable File
473 lines
17 KiB
Dart
Executable File
import 'dart:async';
|
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/apm/apm_helper.dart';
|
|
import 'package:star_lock/flavors.dart';
|
|
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
|
import 'package:star_lock/main/lockDetail/fingerprint/addFingerprint/addFingerprint_entity.dart';
|
|
import 'package:star_lock/tools/dateTool.dart';
|
|
|
|
import '../../../../../blue/blue_manage.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 '../../../../blue/io_protocol/io_addFingerprintWithTimeCycleCoercion.dart';
|
|
import '../../../../tools/bugly/bugly_tool.dart';
|
|
import 'addFingerprint_state.dart';
|
|
|
|
class AddFingerprintLogic extends BaseGetXController {
|
|
final AddFingerprintState state = AddFingerprintState();
|
|
|
|
// 监听设备返回的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
|
|
void _initReplySubscription() {
|
|
_replySubscription =
|
|
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
|
|
// 添加指纹开始
|
|
if ((reply is SenderAddFingerprintWithTimeCycleCoercionReply) &&
|
|
(state.ifCurrentScreen.value == true)) {
|
|
_replyAddFingerprintBegin(reply);
|
|
}
|
|
|
|
// 添加指纹过程
|
|
if (reply is SenderAddFingerprintProcessReply) {
|
|
_replyAddFingerprintProcess(reply);
|
|
}
|
|
|
|
// 添加指纹确认
|
|
if (reply is SenderAddFingerprintConfirmationReply) {
|
|
_replyAddFingerprintConfirmation(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _replyAddFingerprintBegin(Reply reply) async {
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹开始,解析数据',
|
|
detail: '添加指纹开始,解析数据 _replyAddFingerprintBegin:${reply.data}',
|
|
upload: false);
|
|
|
|
final int status = reply.data[2];
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
state.ifConnectScuess.value = true;
|
|
state.ifAddState.value = true;
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
// 最大图片数
|
|
state.maxRegCount.value = reply.data[11];
|
|
// AppLog.log("state.maxRegCount.value:${state.maxRegCount.value}");
|
|
// state.fingerprintNumber.value = reply.data.last.toString();
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
final List<String>? privateKey =
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
final List<int> getPrivateKeyList =
|
|
changeStringListToIntList(privateKey!);
|
|
|
|
final List<String>? signKey =
|
|
await Storage.getStringList(saveBlueSignKey);
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
final List<int> token = reply.data.sublist(5, 9);
|
|
final List<String> saveStrList = changeIntListToStringList(token);
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
IoSenderManage.senderAddFingerprintWithTimeCycleCoercionCommand(
|
|
keyID: '1',
|
|
userID: await Storage.getUid(),
|
|
fingerNo: 0,
|
|
useCountLimit: 0xffff,
|
|
isForce: state.isCoerced.value == '1' ? 1 : 0,
|
|
// 是否是胁迫
|
|
isAdmin: state.isAdministrator.value == '2' ? 1 : 0,
|
|
operate: 0,
|
|
isRound: state.selectType.value == '2' ? 1 : 0,
|
|
// 是否是循环
|
|
weekRound: DateTool().accordingTheCycleIntoTheCorrespondingNumber(
|
|
state.weekDay.value),
|
|
// 周循环
|
|
startDate: int.parse(state.startDate.value) ~/ 1000,
|
|
endDate: int.parse(state.endDate.value) ~/ 1000,
|
|
startTime: DateTool().dateToHNString(state.effectiveDateTime.value),
|
|
endTime: DateTool().dateToHNString(state.failureDateTime.value),
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: token,
|
|
isBeforeAddUser: false);
|
|
break;
|
|
case 0xFE:
|
|
case 12:
|
|
// 管理员已满
|
|
state.ifAddState.value = false;
|
|
showToast('管理员已满'.tr, something: () {
|
|
Get.back();
|
|
});
|
|
break;
|
|
default:
|
|
//失败
|
|
state.ifAddState.value = false;
|
|
showToast('添加指纹失败'.tr, something: () {
|
|
Get.back();
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
|
|
Future<void> _replyAddFingerprintProcess(Reply reply) async {
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹过程,解析数据',
|
|
detail: '添加指纹过程,解析数据 _replyAddFingerprintProcess:${reply.data}',
|
|
upload: false);
|
|
final int status = reply.data[2];
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
switch (reply.data[5]) {
|
|
case 0xff:
|
|
// 注册指纹失败
|
|
showToast('退出添加'.tr);
|
|
state.ifAddState.value = false;
|
|
Get.close(1);
|
|
break;
|
|
case 0xFE:
|
|
case 12:
|
|
// 管理员已满
|
|
state.ifAddState.value = false;
|
|
showToast('管理员已满'.tr, something: () {
|
|
Get.back();
|
|
});
|
|
break;
|
|
case 0xFD:
|
|
// 用户已满
|
|
showToast('用户已满'.tr);
|
|
state.ifAddState.value = false;
|
|
Get.close(1);
|
|
break;
|
|
case 0xFC:
|
|
// 指纹已满
|
|
showToast('锁上面添加指纹已满'.tr);
|
|
state.ifAddState.value = false;
|
|
Get.close(1);
|
|
break;
|
|
case 0xFB:
|
|
// 指纹已存在
|
|
showToast('指纹已存在'.tr);
|
|
state.ifAddState.value = false;
|
|
break;
|
|
default:
|
|
// 添加指纹中
|
|
// 当前注册数
|
|
state.regIndex.value = reply.data[6];
|
|
// AppLog.log("当前注册数 state.regIndex.value:${state.regIndex.value}");
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
//失败
|
|
state.ifAddState.value = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
Future<void> _replyAddFingerprintConfirmation(Reply reply) async {
|
|
final int status = reply.data[2];
|
|
final String getMobile = (await Storage.getMobile())!;
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
// var fingerprintNum = listChangInt(reply.data.sublist(9, 11)).toString();
|
|
ApmHelper.instance.trackEvent('add_fingerprint', {
|
|
'lock_name': BlueManage().connectDeviceName,
|
|
'account':
|
|
getMobile.isNotEmpty ? getMobile : (await Storage.getEmail())!,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'add_fingerprint_result': '成功',
|
|
});
|
|
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹确认结果,解析数据',
|
|
detail:
|
|
'添加指纹确认结果,解析数据 _replyAddFingerprintConfirmation:${reply.data}',
|
|
upload: false);
|
|
|
|
final List<int> fingerprintList =
|
|
reply.data.sublist(reply.data.length - 2);
|
|
final String fingerprintNum = listChangInt(fingerprintList).toString();
|
|
state.regIndex.value++;
|
|
if (state.fingerprintNumber.value == fingerprintNum) {
|
|
return;
|
|
} else {
|
|
state.fingerprintNumber.value = fingerprintNum;
|
|
}
|
|
// if(state.isCoerced.value == "1"){
|
|
// 调用添加指纹接口
|
|
state.ifAddState.value = false;
|
|
await addFingerprintsData();
|
|
// }else{
|
|
// // 如果是胁迫指纹在 添加完之后以后再调用添加胁迫指纹的
|
|
// senderAddStressFingerprint();
|
|
// }
|
|
break;
|
|
default:
|
|
//失败
|
|
ApmHelper.instance.trackEvent('add_fingerprint', {
|
|
'lock_name': BlueManage().connectDeviceName,
|
|
'account':
|
|
getMobile.isNotEmpty ? getMobile : (await Storage.getEmail())!,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'add_fingerprint_result': '失败-${reply.data}',
|
|
});
|
|
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹确认结果,解析数据',
|
|
detail:
|
|
'添加指纹确认结果蓝牙返回失败结果,解析数据 _replyAddFingerprintConfirmation:${reply.data}',
|
|
eventStr: '添加指纹事件蓝牙返回失败结果',
|
|
upload: true);
|
|
state.ifAddState.value = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 添加指纹开始
|
|
Future<void> senderAddFingerprint() async {
|
|
final List<String>? privateKey =
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
|
String startTime = DateTool().dateToHNString(state.effectiveDateTime.value);
|
|
String endTime = DateTool().dateToHNString(state.failureDateTime.value);
|
|
// if (F.isSKY) {
|
|
// startTime = '255:00';
|
|
// endTime = '255:00';
|
|
// }
|
|
|
|
final String command = SenderAddFingerprintWithTimeCycleCoercionCommand(
|
|
keyID: '1',
|
|
userID: await Storage.getUid(),
|
|
fingerNo: 0,
|
|
useCountLimit: 0xffff,
|
|
isForce: state.isCoerced.value == '1' ? 1 : 0,
|
|
// 是否是胁迫
|
|
isAdmin: state.isAdministrator.value == '2' ? 1 : 0,
|
|
operate: 0,
|
|
isRound: state.selectType.value == '2' ? 1 : 0,
|
|
// 是否是循环
|
|
weekRound: DateTool()
|
|
.accordingTheCycleIntoTheCorrespondingNumber(state.weekDay.value),
|
|
// 周循环
|
|
startDate: int.parse(state.startDate.value) ~/ 1000,
|
|
endDate: int.parse(state.endDate.value) ~/ 1000,
|
|
startTime: startTime,
|
|
endTime: endTime,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: getTokenList,
|
|
).toString();
|
|
showBlueConnetctToastTimer(action: () async {
|
|
final String getMobile = (await Storage.getMobile())!;
|
|
ApmHelper.instance.trackEvent('add_fingerprint', {
|
|
'lock_name': BlueManage().connectDeviceName,
|
|
'account':
|
|
getMobile.isNotEmpty ? getMobile : (await Storage.getEmail())!,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'add_fingerprint_result': '超时',
|
|
});
|
|
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹超时处理-添加指纹失败',
|
|
detail:
|
|
'添加指纹超时处理,断开连接,添加指纹失败--SenderAddFingerprintWithTimeCycleCoercionCommand:$command',
|
|
eventStr: '添加指纹事件超时',
|
|
upload: true);
|
|
Get.close(1);
|
|
});
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
|
(BluetoothConnectionState deviceConnectionState) async {
|
|
if (deviceConnectionState == BluetoothConnectionState.connected) {
|
|
final List<String>? privateKey =
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
final List<int> getPrivateKeyList =
|
|
changeStringListToIntList(privateKey!);
|
|
|
|
final List<String>? signKey =
|
|
await Storage.getStringList(saveBlueSignKey);
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
IoSenderManage.senderAddFingerprintWithTimeCycleCoercionCommand(
|
|
keyID: '1',
|
|
userID: await Storage.getUid(),
|
|
fingerNo: 0,
|
|
useCountLimit: 0xffff,
|
|
isForce: state.isCoerced.value == '1' ? 1 : 0,
|
|
// 是否是胁迫
|
|
isAdmin: state.isAdministrator.value == '2' ? 1 : 0,
|
|
operate: 0,
|
|
isRound: state.selectType.value == '2' ? 1 : 0,
|
|
// 是否是循环
|
|
weekRound: DateTool().accordingTheCycleIntoTheCorrespondingNumber(
|
|
state.weekDay.value),
|
|
// 周循环
|
|
startDate: int.parse(state.startDate.value) ~/ 1000,
|
|
endDate: int.parse(state.endDate.value) ~/ 1000,
|
|
startTime: startTime,
|
|
endTime: endTime,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: getTokenList,
|
|
isBeforeAddUser: false);
|
|
} else if (deviceConnectionState ==
|
|
BluetoothConnectionState.disconnected) {
|
|
final String getMobile = (await Storage.getMobile())!;
|
|
ApmHelper.instance.trackEvent('add_fingerprint', {
|
|
'lock_name': BlueManage().connectDeviceName,
|
|
'account':
|
|
getMobile.isNotEmpty ? getMobile : (await Storage.getEmail())!,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'add_fingerprint_result': '断开',
|
|
});
|
|
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹断开连接-添加指纹失败',
|
|
detail:
|
|
'添加指纹断开连接-添加指纹失败--SenderAddFingerprintWithTimeCycleCoercionCommand:$command',
|
|
eventStr: '添加指纹事件断开连接',
|
|
upload: true);
|
|
|
|
if (state.ifCurrentScreen.value == true) {
|
|
showBlueConnetctToast();
|
|
}
|
|
cancelBlueConnetctToastTimer();
|
|
Get.close(1);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 取消添加指纹
|
|
Future<void> senderCancelAddFingerprintCommand() async {
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
|
(BluetoothConnectionState deviceConnectionState) async {
|
|
if (deviceConnectionState == BluetoothConnectionState.connected) {
|
|
final List<String>? privateKey =
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
final List<int> getPrivateKeyList =
|
|
changeStringListToIntList(privateKey!);
|
|
|
|
final List<String>? signKey =
|
|
await Storage.getStringList(saveBlueSignKey);
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
IoSenderManage.senderCancelAddFingerprintCommand(
|
|
keyID: '1',
|
|
userID: await Storage.getUid(),
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
token: getTokenList,
|
|
);
|
|
} else if (deviceConnectionState ==
|
|
BluetoothConnectionState.disconnected) {
|
|
if (state.ifCurrentScreen.value == true) {
|
|
showBlueConnetctToast();
|
|
}
|
|
cancelBlueConnetctToastTimer();
|
|
Get.close(1);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 添加指纹
|
|
Future<void> addFingerprintsData() async {
|
|
final AddFingerprintEntity entity =
|
|
await ApiRepository.to.addFingerprintsData(
|
|
lockId: state.lockId.value.toString(),
|
|
endDate: state.endDate.value,
|
|
addType: state.addType.value,
|
|
fingerprintName: state.fingerprintName.value,
|
|
fingerprintNumber: state.fingerprintNumber.value,
|
|
fingerprintType: state.fingerprintType.value,
|
|
isCoerced: state.isCoerced.value,
|
|
startDate: state.startDate.value,
|
|
weekDay: state.weekDay.value,
|
|
fingerRight: state.isAdministrator.value == '2' ? 1 : 0,
|
|
startTime: int.parse(state.effectiveDateTime.value),
|
|
endTime: int.parse(state.failureDateTime.value),
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
updateFingerprintUserNoLoadData(entity.data!.fingerprintId.toString());
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹调用接口成功',
|
|
detail: '添加指纹调用接口成功',
|
|
eventStr: '添加指纹事件用接口成功',
|
|
upload: true);
|
|
} else {
|
|
updateFingerprintUserNoLoadData(entity.data!.fingerprintId.toString());
|
|
BuglyTool.uploadException(
|
|
message: '添加指纹调用接口失败',
|
|
detail: '添加指纹调用接口添加指纹调用接口失败 - ${entity.errorMsg}',
|
|
eventStr: '添加指纹事件接口失败',
|
|
upload: true);
|
|
}
|
|
}
|
|
|
|
// 更新指纹用户账号
|
|
Future<void> updateFingerprintUserNoLoadData(String fingerprintId) async {
|
|
final LoginEntity entity =
|
|
await ApiRepository.to.updateFingerprintUserNoLoadData(
|
|
fingerprintId: fingerprintId,
|
|
lockId: state.lockId.value.toString(),
|
|
fingerprintUserNo: state.fingerprintNumber.value,
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
showToast('添加成功'.tr, something: () {
|
|
if (state.fromType.value == 2) {
|
|
// 回调指纹号
|
|
eventBus.fire(ChickInAddStaffCardAndFingerprintBlockNumberEvent(
|
|
state.fingerprintNumber.value));
|
|
} else if (state.fromType.value == 1) {
|
|
eventBus.fire(OtherTypeRefreshListEvent());
|
|
}
|
|
Get.close(2);
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
super.onReady();
|
|
_initReplySubscription();
|
|
senderAddFingerprint();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
super.onClose();
|
|
|
|
_replySubscription.cancel();
|
|
}
|
|
}
|