2024-06-25 11:35:20 +08:00
|
|
|
|
|
2023-12-13 11:54:34 +08:00
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
2024-03-18 16:16:51 +08:00
|
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
import 'package:get/get.dart';
|
2024-05-29 14:31:38 +08:00
|
|
|
|
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
2024-03-28 13:46:35 +08:00
|
|
|
|
import 'package:star_lock/tools/eventBusEventManage.dart';
|
2024-06-01 17:25:55 +08:00
|
|
|
|
import 'package:star_lock/tools/pickers/time_picker/time_utils.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
|
|
|
|
|
import '../../../../blue/blue_manage.dart';
|
2024-04-16 17:44:38 +08:00
|
|
|
|
import '../../../../blue/io_protocol/io_addICCardWithTimeCycleCoercion.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
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';
|
2024-03-25 15:03:02 +08:00
|
|
|
|
import '../../../../tools/dateTool.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
import '../../../../tools/storage.dart';
|
|
|
|
|
|
import 'cardDetail_state.dart';
|
|
|
|
|
|
|
2024-06-01 17:25:55 +08:00
|
|
|
|
class CardDetailLogic extends BaseGetXController {
|
2023-12-13 11:54:34 +08:00
|
|
|
|
CardDetailState state = CardDetailState();
|
|
|
|
|
|
|
2024-06-01 17:25:55 +08:00
|
|
|
|
List<dynamic> get weekDayStr {
|
|
|
|
|
|
return state.weekDay.map((e) => TimeUtils.translateWeekday(e)).toList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-13 11:54:34 +08:00
|
|
|
|
// 监听设备返回的数据
|
|
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
2024-06-01 17:25:55 +08:00
|
|
|
|
|
2023-12-13 11:54:34 +08:00
|
|
|
|
void _initReplySubscription() {
|
2024-06-01 17:25:55 +08:00
|
|
|
|
_replySubscription =
|
|
|
|
|
|
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
|
2023-12-13 11:54:34 +08:00
|
|
|
|
// 添加卡片开始(重置锁里面所有卡)
|
2024-06-01 17:25:55 +08:00
|
|
|
|
if ((reply is SenderAddICCardWithTimeCycleCoercionReply) &&
|
|
|
|
|
|
(state.ifCurrentScreen.value == true)) {
|
2023-12-13 11:54:34 +08:00
|
|
|
|
_replyAddICCardBegin(reply);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加卡片开始(此处用作删除卡片)
|
|
|
|
|
|
Future<void> _replyAddICCardBegin(Reply reply) async {
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final int status = reply.data[2];
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-06-01 17:25:55 +08:00
|
|
|
|
switch (status) {
|
2023-12-13 11:54:34 +08:00
|
|
|
|
case 0x00:
|
2024-04-11 17:06:06 +08:00
|
|
|
|
//成功
|
2024-07-27 18:18:54 +08:00
|
|
|
|
state.sureBtnState.value = 0;
|
2024-01-18 11:25:56 +08:00
|
|
|
|
cancelBlueConnetctToastTimer();
|
2024-01-12 19:05:44 +08:00
|
|
|
|
dismissEasyLoading();
|
2024-06-01 17:25:55 +08:00
|
|
|
|
if (state.isDeletCard.value == true) {
|
2024-04-22 15:38:19 +08:00
|
|
|
|
deletICCardData();
|
2024-06-01 17:25:55 +08:00
|
|
|
|
} else {
|
2024-04-22 15:38:19 +08:00
|
|
|
|
editICCardData();
|
|
|
|
|
|
}
|
2023-12-13 11:54:34 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 0x06:
|
2024-04-11 17:06:06 +08:00
|
|
|
|
//无权限
|
2024-06-01 17:25:55 +08:00
|
|
|
|
final List<String>? privateKey =
|
|
|
|
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
final List<int> getPrivateKeyList =
|
|
|
|
|
|
changeStringListToIntList(privateKey!);
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-06-01 17:25:55 +08:00
|
|
|
|
final List<String>? signKey =
|
|
|
|
|
|
await Storage.getStringList(saveBlueSignKey);
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
2024-04-15 16:30:00 +08:00
|
|
|
|
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final List<int> token = reply.data.sublist(5, 9);
|
|
|
|
|
|
final List<String> saveStrList = changeIntListToStringList(token);
|
2024-04-22 11:16:37 +08:00
|
|
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-04-16 17:44:38 +08:00
|
|
|
|
IoSenderManage.senderAddCardWithTimeCycleCoercionCommand(
|
2024-06-01 17:25:55 +08:00
|
|
|
|
keyID: state.keyId.value.toString(),
|
|
|
|
|
|
userID: await Storage.getUid(),
|
|
|
|
|
|
cardNo: int.parse(state.typeNumber.value),
|
|
|
|
|
|
useCountLimit: 0xffff,
|
|
|
|
|
|
operate: state.isDeletCard.value ? 2 : 1,
|
|
|
|
|
|
// 0:注册 1:修改 2:删除 3:删除全部
|
|
|
|
|
|
isAdmin: state.isAdministrator.value == true ? 1 : 0,
|
|
|
|
|
|
isForce: state.isStressCard.value == true ? 1 : 0,
|
|
|
|
|
|
// 是否是胁迫
|
|
|
|
|
|
isRound: state.keyType.value == 4 ? 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.starTime.value),
|
|
|
|
|
|
endTime: DateTool().dateToHNString(state.endTime.value),
|
|
|
|
|
|
needAuthor: 1,
|
|
|
|
|
|
signKey: signKeyDataList,
|
|
|
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
isBeforeAddUser: false);
|
2023-12-13 11:54:34 +08:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2024-04-11 17:06:06 +08:00
|
|
|
|
//失败
|
2024-07-27 18:18:54 +08:00
|
|
|
|
state.sureBtnState.value = 0;
|
2023-12-13 11:54:34 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-18 11:25:56 +08:00
|
|
|
|
// 添加卡片
|
2023-12-16 11:20:36 +08:00
|
|
|
|
Future<void> senderAddICCard() async {
|
2024-06-01 17:25:55 +08:00
|
|
|
|
if (state.sureBtnState.value == 1) {
|
2024-01-12 19:05:44 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
state.sureBtnState.value = 1;
|
|
|
|
|
|
|
|
|
|
|
|
showEasyLoading();
|
2024-06-01 17:25:55 +08:00
|
|
|
|
showBlueConnetctToastTimer(action: () {
|
2024-01-12 19:05:44 +08:00
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
|
});
|
2024-06-01 17:25:55 +08:00
|
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
|
|
|
|
|
(BluetoothConnectionState deviceConnectionState) async {
|
|
|
|
|
|
if (deviceConnectionState == BluetoothConnectionState.connected) {
|
|
|
|
|
|
final List<String>? signKey =
|
|
|
|
|
|
await Storage.getStringList(saveBlueSignKey);
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-06-01 17:25:55 +08:00
|
|
|
|
final List<String>? privateKey =
|
|
|
|
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
final List<int> getPrivateKeyList =
|
|
|
|
|
|
changeStringListToIntList(privateKey!);
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-04-16 17:44:38 +08:00
|
|
|
|
IoSenderManage.senderAddCardWithTimeCycleCoercionCommand(
|
2024-06-01 17:25:55 +08:00
|
|
|
|
keyID: state.keyId.value.toString(),
|
|
|
|
|
|
userID: await Storage.getUid(),
|
|
|
|
|
|
cardNo: int.parse(state.typeNumber.value),
|
|
|
|
|
|
useCountLimit: 0xffff,
|
|
|
|
|
|
operate: state.isDeletCard.value ? 2 : 1,
|
|
|
|
|
|
// 0:注册 1:修改 2:删除 3:删除全部
|
|
|
|
|
|
isAdmin: state.isAdministrator.value == true ? 1 : 0,
|
|
|
|
|
|
isForce: state.isStressCard.value == true ? 1 : 0,
|
|
|
|
|
|
// 是否是胁迫
|
|
|
|
|
|
isRound: state.keyType.value == 4 ? 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.starTime.value),
|
|
|
|
|
|
endTime: DateTool().dateToHNString(state.endTime.value),
|
|
|
|
|
|
needAuthor: 1,
|
|
|
|
|
|
signKey: signKeyDataList,
|
|
|
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
|
token: getTokenList,
|
|
|
|
|
|
isBeforeAddUser: false);
|
|
|
|
|
|
} else if (deviceConnectionState ==
|
|
|
|
|
|
BluetoothConnectionState.disconnected) {
|
2024-01-12 19:05:44 +08:00
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
state.sureBtnState.value = 0;
|
2024-06-01 17:25:55 +08:00
|
|
|
|
if (state.ifCurrentScreen.value == true) {
|
2024-01-12 19:05:44 +08:00
|
|
|
|
showBlueConnetctToast();
|
|
|
|
|
|
}
|
2023-12-13 11:54:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 编辑iC卡
|
2024-06-01 17:25:55 +08:00
|
|
|
|
Future<void> editICCardData() async {
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final LoginEntity entity = await ApiRepository.to.editICCardData(
|
2023-12-13 11:54:34 +08:00
|
|
|
|
cardId: state.keyId.value.toString(),
|
|
|
|
|
|
lockId: state.fingerprintItemData.value.lockId.toString(),
|
|
|
|
|
|
weekDay: state.weekDay.value,
|
2024-04-22 11:16:37 +08:00
|
|
|
|
startDate: state.startDate.value,
|
2024-04-11 17:06:06 +08:00
|
|
|
|
endDate: state.endDate.value,
|
2024-05-29 14:31:38 +08:00
|
|
|
|
isCoerced: state.isStressCard.value ? '1' : '2',
|
2023-12-13 11:54:34 +08:00
|
|
|
|
cardName: state.changeNameController.text,
|
2024-05-29 14:31:38 +08:00
|
|
|
|
changeType: '1',
|
2024-03-25 15:03:02 +08:00
|
|
|
|
startTime: int.parse(state.starTime.value),
|
|
|
|
|
|
endTime: int.parse(state.endTime.value),
|
2024-06-01 17:25:55 +08:00
|
|
|
|
cardType: state.keyType.value,
|
2024-05-04 16:53:27 +08:00
|
|
|
|
cardRight: state.isAdministrator.value ? 1 : 0,
|
2023-12-13 11:54:34 +08:00
|
|
|
|
);
|
2024-06-01 17:25:55 +08:00
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
2024-06-05 16:38:43 +08:00
|
|
|
|
state.fingerprintItemData.value.cardName = state.changeNameController.text;
|
|
|
|
|
|
state.fingerprintItemData.value.isCoerced = state.isStressCard.value ? 1 : 2;
|
|
|
|
|
|
state.fingerprintItemData.value.cardRight = state.isAdministrator.value ? 1 : 0;
|
2024-06-01 17:25:55 +08:00
|
|
|
|
showToast('修改成功'.tr, something: () {
|
2024-03-28 13:46:35 +08:00
|
|
|
|
eventBus.fire(OtherTypeRefreshListEvent());
|
2024-03-23 11:23:38 +08:00
|
|
|
|
});
|
2023-12-13 11:54:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除IC卡
|
2024-06-01 17:25:55 +08:00
|
|
|
|
Future<void> deletICCardData() async {
|
2024-05-29 14:31:38 +08:00
|
|
|
|
final LoginEntity entity = await ApiRepository.to.deletIcCardData(
|
2023-12-13 11:54:34 +08:00
|
|
|
|
cardId: state.fingerprintItemData.value.cardId.toString(),
|
|
|
|
|
|
lockId: state.fingerprintItemData.value.lockId.toString(),
|
2024-05-29 14:31:38 +08:00
|
|
|
|
type: '0',
|
2024-06-01 17:25:55 +08:00
|
|
|
|
deleteType: '1');
|
|
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
|
|
|
|
showToast('删除成功'.tr, something: () {
|
2024-05-29 14:31:38 +08:00
|
|
|
|
Get.back(result: 'addScuess');
|
2024-03-25 15:03:02 +08:00
|
|
|
|
});
|
2023-12-13 11:54:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-01 17:25:55 +08:00
|
|
|
|
String getKeyTypeShowDateTime() {
|
2024-03-25 15:03:02 +08:00
|
|
|
|
String useDateStr = '';
|
2024-06-01 17:25:55 +08:00
|
|
|
|
if (state.keyType.value == 1) {
|
2024-05-29 14:31:38 +08:00
|
|
|
|
useDateStr = '永久'.tr;
|
2024-06-01 17:25:55 +08:00
|
|
|
|
} else if (state.keyType.value == 2) {
|
|
|
|
|
|
useDateStr =
|
|
|
|
|
|
'${DateTool().dateToYMDHNString(state.startDate.value)}\n${DateTool().dateToYMDHNString(state.endDate.value)}';
|
|
|
|
|
|
} else if (state.keyType.value == 4) {
|
|
|
|
|
|
useDateStr =
|
|
|
|
|
|
'${DateTool().dateToYMDString(state.startDate.value)}\n${DateTool().dateToYMDString(state.endDate.value)}';
|
2024-03-28 13:46:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
return useDateStr;
|
2024-03-25 15:03:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-13 11:54:34 +08:00
|
|
|
|
@override
|
|
|
|
|
|
void onReady() {
|
|
|
|
|
|
super.onReady();
|
|
|
|
|
|
|
|
|
|
|
|
_initReplySubscription();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void onClose() {
|
|
|
|
|
|
super.onClose();
|
|
|
|
|
|
|
|
|
|
|
|
_replySubscription.cancel();
|
|
|
|
|
|
}
|
2024-06-01 17:25:55 +08:00
|
|
|
|
}
|