2024-07-23 11:16:18 +08:00

226 lines
8.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:star_lock/tools/baseGetXController.dart';
import '../../../../blue/blue_manage.dart';
import '../../../../blue/io_protocol/io_addRemoteControlWithTimeCycleCoercion.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 '../../../../login/login/entity/LoginEntity.dart';
import '../../../../network/api_repository.dart';
import '../../../../tools/dateTool.dart';
import '../../../../tools/eventBusEventManage.dart';
import '../../../../tools/pickers/time_picker/time_utils.dart';
import '../../../../tools/storage.dart';
import 'remoteControlDetail_state.dart';
class RemoteControlDetailLogic extends BaseGetXController {
RemoteControlDetailState state = RemoteControlDetailState();
List<dynamic> get weekDayStr {
return state.weekDay.map((e) => TimeUtils.translateWeekday(e)).toList();
}
// 监听设备返回的数据
late StreamSubscription<Reply> _replySubscription;
void _initReplySubscription() {
_replySubscription =
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
// 添加卡片开始(重置锁里面所有卡)
if ((reply is SenderAddRemoteControlWithTimeCycleCoercionReply) && (state.ifCurrentScreen.value == true)) {
_replyAddRemoteControlBegin(reply);
}
});
}
// 添加卡片开始(此处用作删除卡片)
Future<void> _replyAddRemoteControlBegin(Reply reply) async {
final int status = reply.data[2];
switch (status) {
case 0x00:
//成功
cancelBlueConnetctToastTimer();
dismissEasyLoading();
if (state.isDeletRemoteControl.value == true) {
deletRemoteControlData();
} else {
editRemoteControlData();
}
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.senderAddRemoteControlWithTimeCycleCoercionCommand(
keyID: state.keyId.value.toString(),
userID: await Storage.getUid(),
remoteControlNo: int.parse(state.typeNumber.value),
useCountLimit: 0xffff,
operate: state.isDeletRemoteControl.value ? 2 : 1,
// 0:注册 1修改 2:删除 3删除全部
// isAdmin: state.isAdministrator.value == true ? 1 : 0,
// isForce: state.isStressCard.value == true ? 1 : 0, // 是否是胁迫
isAdmin: 0,
isForce: 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);
break;
default:
//失败
break;
}
}
// 添加卡片
Future<void> senderAddICCard() async {
if (state.sureBtnState.value == 1) {
return;
}
state.sureBtnState.value = 1;
showEasyLoading();
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
state.sureBtnState.value = 0;
});
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState deviceConnectionState) async {
if (deviceConnectionState == BluetoothConnectionState.connected) {
final List<String>? signKey =
await Storage.getStringList(saveBlueSignKey);
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
final List<String>? privateKey =
await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList =
changeStringListToIntList(privateKey!);
final List<String>? token = await Storage.getStringList(saveBlueToken);
final List<int> getTokenList = changeStringListToIntList(token!);
IoSenderManage.senderAddRemoteControlWithTimeCycleCoercionCommand(
keyID: state.keyId.value.toString(),
userID: await Storage.getUid(),
remoteControlNo: int.parse(state.typeNumber.value),
useCountLimit: 0xffff,
operate: state.isDeletRemoteControl.value ? 2 : 1,
// 0:注册 1修改 2:删除 3删除全部
// isAdmin: state.isAdministrator.value == true ? 1 : 0,
// isForce: state.isStressCard.value == true ? 1 : 0, // 是否是胁迫
isAdmin: 0,
isForce: 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) {
dismissEasyLoading();
cancelBlueConnetctToastTimer();
state.sureBtnState.value = 0;
if (state.ifCurrentScreen.value == true) {
showBlueConnetctToast();
}
}
});
}
// 编辑遥控
Future<void> editRemoteControlData() async {
final LoginEntity entity = await ApiRepository.to.editRemoteControlData(
lockId: state.fingerprintItemData.value.lockId ?? 0,
remoteId: state.keyId.value,
startDate: int.parse(state.startDate.value),
endDate: int.parse(state.endDate.value),
startTime: int.parse(state.starTime.value),
endTime: int.parse(state.endTime.value),
remoteType: state.keyType.value,
weekDay: state.weekDay.value,
remoteName: state.changeNameController.text,
addType: 1,
isCoerced: 2,
remoteRight: 2,
);
if (entity.errorCode!.codeIsSuccessful) {
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;
showToast('修改成功'.tr, something: () {
eventBus.fire(OtherTypeRefreshListEvent());
});
}
}
// 删除遥控
Future<void> deletRemoteControlData() async {
final LoginEntity entity = await ApiRepository.to.deletRemoteControlData(
remoteId: state.fingerprintItemData.value.remoteId ?? 0,
);
if (entity.errorCode!.codeIsSuccessful) {
showToast('删除成功'.tr, something: () {
Get.back(result: 'addScuess');
});
}
}
String getKeyTypeShowDateTime() {
String useDateStr = '';
if (state.keyType.value == 1) {
useDateStr = '永久'.tr;
} 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)}';
}
return useDateStr;
}
@override
void onReady() {
super.onReady();
_initReplySubscription();
}
@override
void onClose() {
super.onClose();
_replySubscription.cancel();
}
}