216 lines
6.8 KiB
Dart
Raw Permalink Normal View History

2023-09-04 15:00:42 +08:00
import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
2024-01-23 18:37:03 +08:00
import 'package:get/get.dart';
2023-10-07 18:55:59 +08:00
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsWithParameters.dart';
import 'package:star_lock/login/login/entity/LoginEntity.dart';
2023-09-04 15:00:42 +08:00
import 'package:star_lock/tools/baseGetXController.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';
2023-09-07 18:36:16 +08:00
import '../../../../network/api_repository.dart';
import '../../../../tools/eventBusEventManage.dart';
2023-09-04 15:00:42 +08:00
import '../../../../tools/storage.dart';
import 'automaticBlocking_state.dart';
class AutomaticBlockingLogic extends BaseGetXController {
2023-09-07 18:36:16 +08:00
final AutomaticBlockingState state = AutomaticBlockingState();
Future<void> setAutoUnLock() async {
2023-11-27 09:57:53 +08:00
String autoTime;
if (state.isOpen.value == false) {
autoTime = '0';
} else {
if (state.isCustomLockTime.value == true) {
2023-11-27 09:57:53 +08:00
autoTime = state.timeController.text;
if (int.parse(autoTime) > 60) {
2024-07-25 11:36:54 +08:00
showToast('请输入小于或等于60的整数'.tr);
return;
}
} else {
2023-11-27 09:57:53 +08:00
autoTime = state.autoLockTime.value;
}
}
final LoginEntity entity = await ApiRepository.to.setAutoUnlock(
lockId: state.lockSetInfoData.value.lockId!,
2024-07-25 11:36:54 +08:00
autoLock: state.isOpen.value == true ? (int.parse(state.autoLockTime.value) > 0 ? 1 : 0) : 0,
autoLockSecond: int.parse(autoTime),
2023-09-07 18:36:16 +08:00
);
if (entity.errorCode!.codeIsSuccessful) {
state.autoLockTime.value = autoTime;
state.lockSetInfoData.value.lockSettingInfo!.autoLockSecond =
int.parse(state.autoLockTime.value);
showToast('操作成功'.tr, something: () {
eventBus.fire(RefreshLockListInfoDataEvent());
eventBus
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
2024-01-23 18:37:03 +08:00
Get.back();
}, maskType: EasyLoadingMaskType.clear);
2023-09-07 18:36:16 +08:00
}
}
2023-09-04 15:00:42 +08:00
// 获取解析后的数据
late StreamSubscription<Reply> _replySubscription;
2023-09-04 15:00:42 +08:00
void _initReplySubscription() {
_replySubscription =
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
if (reply is SetSupportFunctionsWithParametersReply) {
2023-10-07 18:55:59 +08:00
_replySetSupportFunctionsWithParameters(reply);
2023-09-04 15:00:42 +08:00
}
2023-10-07 18:55:59 +08:00
// 读取支持功能(带参数)
2024-01-19 18:15:42 +08:00
// if(reply is ReadSupportFunctionsWithParametersReply) {
// _readSupportFunctionsWithParametersReply(reply);
// }
2023-09-04 15:00:42 +08:00
});
}
2023-10-07 18:55:59 +08:00
// 读取支持功能带参数数据解析
2024-01-19 18:15:42 +08:00
// Future<void> _readSupportFunctionsWithParametersReply(Reply reply) async {
// int status = reply.data[2];
// switch(status){
// case 0x00:
// //成功
// state.autoLockTime.value = reply.data[7].toString();
// break;
// case 0x06:
// //无权限
//
// break;
// default:
// //失败
// break;
// }
// }
2023-09-04 15:00:42 +08:00
2023-09-07 18:36:16 +08:00
// 设置自动落锁数据解析
2023-10-07 18:55:59 +08:00
Future<void> _replySetSupportFunctionsWithParameters(Reply reply) async {
final int status = reply.data[2];
switch (status) {
2023-09-04 15:00:42 +08:00
case 0x00:
//成功
state.sureBtnState.value = 0;
cancelBlueConnetctToastTimer();
2024-01-09 18:48:08 +08:00
dismissEasyLoading();
setAutoUnLock();
2023-09-04 15:00:42 +08:00
break;
case 0x06:
//无权限
2023-09-04 15:00:42 +08:00
break;
default:
2024-04-30 14:46:06 +08:00
state.sureBtnState.value = 0;
2023-09-04 15:00:42 +08:00
break;
}
}
2023-10-07 18:55:59 +08:00
// 设置支持功能(带参数)
2023-09-07 18:36:16 +08:00
Future<void> sendAutoLock() async {
final String autoTime = state.timeController.text;
if (int.parse(autoTime) > 60) {
showToast('请输入小于或等于60的数字'.tr);
return;
}
if (state.sureBtnState.value == 1) {
return;
}
state.sureBtnState.value = 1;
2024-01-02 18:03:50 +08:00
showEasyLoading();
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
state.sureBtnState.value = 0;
});
BlueManage().blueSendData(BlueManage().connectDeviceName,
(BluetoothConnectionState connectionState) async {
if (connectionState == BluetoothConnectionState.connected) {
final List<String>? privateKey =
await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList =
changeStringListToIntList(privateKey!);
2023-09-04 15:00:42 +08:00
final List<String>? token = await Storage.getStringList(saveBlueToken);
final List<int> getTokenList = changeStringListToIntList(token!);
2023-09-04 15:00:42 +08:00
final List<String>? publicKey =
await Storage.getStringList(saveBluePublicKey);
final List<int> getPublicKeyList =
changeStringListToIntList(publicKey!);
2024-01-23 17:36:02 +08:00
2023-10-07 18:55:59 +08:00
String autoTime;
if (state.isOpen.value == false) {
autoTime = '0';
} else {
if (state.isCustomLockTime.value == true) {
2023-10-07 18:55:59 +08:00
autoTime = state.timeController.text;
} else {
2023-10-07 18:55:59 +08:00
autoTime = state.autoLockTime.value;
}
}
IoSenderManage.setSupportFunctionsWithParametersCommand(
2023-11-01 17:28:59 +08:00
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
2023-10-07 18:55:59 +08:00
userID: await Storage.getUid(),
featureBit: 29,
featureParaLength: 2,
featureData: <int>[int.parse(autoTime)],
2023-10-07 18:55:59 +08:00
token: getTokenList,
needAuthor: 1,
publicKey: getPublicKeyList,
privateKey: getPrivateKeyList);
} else if (connectionState == BluetoothConnectionState.disconnected) {
2024-01-02 18:03:50 +08:00
dismissEasyLoading();
cancelBlueConnetctToastTimer();
state.sureBtnState.value = 0;
if (state.ifCurrentScreen.value == true) {
showBlueConnetctToast();
}
2023-09-04 15:00:42 +08:00
}
});
}
2024-01-23 18:37:03 +08:00
void ifCanNext() {
final int autoLock = state.lockSetInfoData.value.lockSettingInfo!.autoLock!;
final String autoLockSecond =
state.lockSetInfoData.value.lockSettingInfo!.autoLockSecond!.toString();
final bool openLock = autoLock == 1;
final bool isNotJustForShow = state.isJustForShow.value == false;
final bool isEditAutoLockSecond =
state.autoLockTime.value != autoLockSecond;
if (!isNotJustForShow) {
//普通用户
state.canNext.value = false;
return;
}
if (state.isOpen.value != openLock || isEditAutoLockSecond) {
2024-01-23 18:37:03 +08:00
state.canNext.value = true;
} else {
2024-01-23 18:37:03 +08:00
state.canNext.value = false;
}
if (!openLock && (state.autoLockTime.value.isEmpty || state.autoLockTime.value == '0')) {
state.autoLockTime.value = '5';
}
2024-01-23 18:37:03 +08:00
}
2023-09-04 15:00:42 +08:00
@override
void onReady() {
super.onReady();
_initReplySubscription();
Storage.getAutomaticLockOffTime()
.then((String value) => state.timeController.text = value);
2023-09-04 15:00:42 +08:00
}
@override
void onClose() {
super.onClose();
_replySubscription.cancel();
}
}