app-starlock/lib/main/lockDetail/lockSet/normallyOpenMode/normallyOpenMode_logic.dart

208 lines
6.6 KiB
Dart
Raw Normal View History

2023-09-07 18:36:16 +08:00
2023-10-07 18:55:59 +08:00
import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
2024-04-16 18:33:24 +08:00
import 'package:get/get.dart';
import 'package:star_lock/login/login/entity/LoginEntity.dart';
2023-09-07 18:36:16 +08:00
import 'package:star_lock/tools/baseGetXController.dart';
2023-10-07 18:55:59 +08:00
import '../../../../blue/blue_manage.dart';
import '../../../../blue/io_protocol/io_setSupportFunctionsWithParameters.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-10-07 18:55:59 +08:00
import '../../../../tools/storage.dart';
2023-09-07 18:36:16 +08:00
import 'normallyOpenMode_state.dart';
class NormallyOpenModeLogic extends BaseGetXController{
NormallyOpenModeState state = NormallyOpenModeState();
// 配置锁的常开模式设置
Future<void> configPassageMode() async{
2024-01-11 15:14:02 +08:00
if(state.weekDays.value.isEmpty){
showToast('请选择常开日期'.tr);
return;
}
2023-09-07 18:36:16 +08:00
if(state.endTimeMinute.value < state.beginTimeMinute.value){
showToast('结束时间不能小于开始时间哦'.tr);
2023-09-07 18:36:16 +08:00
return;
}
final List passageModeConfig = [];
final Map<String, Object> map = <String, Object>{
'isAllDay':state.isAllDay.value,
'weekDays':state.weekDays.value,
'startDate':state.beginTimeMinute.value,
'endDate':state.endTimeMinute.value,
};
2024-01-11 15:14:02 +08:00
passageModeConfig.add(map);
final LoginEntity entity = await ApiRepository.to.setNormallyModeData(
lockId: state.lockSetInfoData.value.lockId!,
passageMode:state.isOpenNormallyOpenMode.value == true ? 1:0,
2024-01-11 15:14:02 +08:00
passageModeConfig: passageModeConfig,
2023-09-07 18:36:16 +08:00
);
if(entity.errorCode!.codeIsSuccessful){
showToast('操作成功'.tr, something: (){
2024-01-23 18:37:03 +08:00
eventBus.fire(RefreshLockListInfoDataEvent());
2023-09-07 18:36:16 +08:00
2024-01-23 18:37:03 +08:00
state.lockSetInfoData.value.lockSettingInfo!.passageMode = state.isOpenNormallyOpenMode.value == true ? 1:0;
eventBus.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
eventBus.fire(LockSetChangeSetRefreshLockDetailWithType(2, state.lockSetInfoData.value.lockSettingInfo!.passageMode!.toString()));
});
2023-09-07 18:36:16 +08:00
}
}
2023-10-07 18:55:59 +08:00
// 获取解析后的数据
late StreamSubscription<Reply> _replySubscription;
void _initReplySubscription() {
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
2023-10-07 18:55:59 +08:00
if(reply is SetSupportFunctionsWithParametersReply) {
_replySetSupportFunctionsWithParameters(reply);
}
// 读取支持功能(带参数)
2024-01-19 18:15:42 +08:00
// if(reply is ReadSupportFunctionsWithParametersReply) {
// _readSupportFunctionsWithParametersReply(reply);
// }
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.isOpenNormallyOpenMode.value = reply.data[4] == 0 ? false : true;
//
// break;
// case 0x06:
// //无权限
// break;
// default:
// //失败
// break;
// }
// }
2023-10-07 18:55:59 +08:00
// 设置自动落锁数据解析
Future<void> _replySetSupportFunctionsWithParameters(Reply reply) async {
final int status = reply.data[2];
2023-10-07 18:55:59 +08:00
switch(status){
case 0x00:
2024-04-16 18:33:24 +08:00
//成功
state.sureBtnState.value = 0;
cancelBlueConnetctToastTimer();
dismissEasyLoading();
2023-10-07 18:55:59 +08:00
configPassageMode();
break;
case 0x06:
2024-04-16 18:33:24 +08:00
//无权限
2023-10-07 18:55:59 +08:00
break;
default:
break;
}
}
// 设置支持功能(带参数)
Future<void> sendAutoLock() async {
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-10-07 18:55:59 +08:00
final List<String>? token = await Storage.getStringList(saveBlueToken);
final List<int> getTokenList = changeStringListToIntList(token!);
2023-10-07 18:55:59 +08:00
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> getPublicKeyList = changeStringListToIntList(publicKey!);
2023-10-07 18:55:59 +08:00
String weekStr = '00000000';
for (var day in state.weekDays.value) {
final int index = day % 7; // 将周日的索引转换为 0
weekStr = '${weekStr.substring(0, index)}1${weekStr.substring(index + 1)}';
}
// 倒序 weekStr
weekStr = weekStr.split('').reversed.join('');
final int number = int.parse(weekStr, radix: 2);
final List<int> list = <int>[];
2023-10-07 18:55:59 +08:00
list.add(state.isOpenNormallyOpenMode.value == true ? 1:0);
final int bieginTime = state.beginTimeMinute.value;
final double bieginDouble = bieginTime / 256;
final int biegin1 = bieginDouble.toInt();
final int biegin2 = bieginTime % 256;
list.add(biegin1);
list.add(biegin2);
// list.add(state.beginTimeMinute.value);
final int endTime = state.endTimeMinute.value;
final double endDouble = endTime / 256;
final int end1 = endDouble.toInt();
final int end2 = endTime % 256;
list.add(end1);
list.add(end2);
2023-10-07 18:55:59 +08:00
list.add(state.isAllDay.value == 1 ? 1:0);
list.add(number);
2023-10-07 18:55:59 +08:00
list.add(0);
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: 50,
featureParaLength: 8,
featureData: list,
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-10-07 18:55:59 +08:00
}
});
}
2023-09-07 18:36:16 +08:00
@override
void onReady() {
super.onReady();
2023-10-07 18:55:59 +08:00
_initReplySubscription();
2023-09-07 18:36:16 +08:00
}
@override
void onInit() {
super.onInit();
2023-10-07 18:55:59 +08:00
2024-01-19 18:15:42 +08:00
// _readSupportFunctionsWithParameters();
2023-09-07 18:36:16 +08:00
}
@override
void onClose() {
2023-10-07 18:55:59 +08:00
super.onClose();
_replySubscription.cancel();
2023-09-07 18:36:16 +08:00
}
2024-01-23 17:36:02 +08:00
}