2023-09-07 18:36:16 +08:00
|
|
|
|
2023-10-07 18:55:59 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
2024-03-18 16:16:51 +08:00
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
2024-04-16 18:33:24 +08:00
|
|
|
import 'package:get/get.dart';
|
2024-08-21 14:12:15 +08:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// 配置锁的常开模式设置
|
2024-08-21 14:12:15 +08:00
|
|
|
Future<void> configPassageMode() async{
|
2024-01-11 15:14:02 +08:00
|
|
|
if(state.weekDays.value.isEmpty){
|
2024-08-21 14:12:15 +08:00
|
|
|
showToast('请选择常开日期'.tr);
|
2023-11-03 13:58:41 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 18:36:16 +08:00
|
|
|
if(state.endTimeMinute.value < state.beginTimeMinute.value){
|
2024-08-21 14:12:15 +08:00
|
|
|
showToast('结束时间不能小于开始时间哦'.tr);
|
2023-09-07 18:36:16 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
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,
|
2023-11-03 13:58:41 +08:00
|
|
|
};
|
2024-01-11 15:14:02 +08:00
|
|
|
passageModeConfig.add(map);
|
2023-11-03 13:58:41 +08:00
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
final LoginEntity entity = await ApiRepository.to.setNormallyModeData(
|
2023-11-03 13:58:41 +08:00
|
|
|
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){
|
2024-08-21 14:12:15 +08:00
|
|
|
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() {
|
2024-08-21 14:12:15 +08:00
|
|
|
_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 {
|
2024-08-21 14:12:15 +08:00
|
|
|
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
|
|
|
//成功
|
2024-01-09 18:02:02 +08:00
|
|
|
state.sureBtnState.value = 0;
|
2024-01-18 11:25:56 +08:00
|
|
|
cancelBlueConnetctToastTimer();
|
2024-01-09 18:02:02 +08:00
|
|
|
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 {
|
2024-01-09 18:02:02 +08:00
|
|
|
if(state.sureBtnState.value == 1){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
state.sureBtnState.value = 1;
|
|
|
|
|
|
2024-01-02 18:03:50 +08:00
|
|
|
showEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
showBlueConnetctToastTimer(action: (){
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
});
|
2024-05-21 15:33:06 +08:00
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async {
|
2024-03-18 16:16:51 +08:00
|
|
|
if (connectionState == BluetoothConnectionState.connected) {
|
2024-08-21 14:12:15 +08:00
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
2023-10-07 18:55:59 +08:00
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
2023-10-07 18:55:59 +08:00
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
|
|
|
final List<int> getPublicKeyList = changeStringListToIntList(publicKey!);
|
2023-10-07 18:55:59 +08:00
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
String weekStr = '00000000';
|
2024-01-09 18:02:02 +08:00
|
|
|
for (var day in state.weekDays.value) {
|
2024-08-21 14:12:15 +08:00
|
|
|
final int index = day % 7; // 将周日的索引转换为 0
|
2024-01-09 18:02:02 +08:00
|
|
|
weekStr = '${weekStr.substring(0, index)}1${weekStr.substring(index + 1)}';
|
|
|
|
|
}
|
2024-04-19 18:13:34 +08:00
|
|
|
// 倒序 weekStr
|
|
|
|
|
weekStr = weekStr.split('').reversed.join('');
|
2024-08-21 14:12:15 +08:00
|
|
|
final int number = int.parse(weekStr, radix: 2);
|
2024-01-09 18:02:02 +08:00
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
final List<int> list = <int>[];
|
2023-10-07 18:55:59 +08:00
|
|
|
list.add(state.isOpenNormallyOpenMode.value == true ? 1:0);
|
2024-01-09 18:02:02 +08:00
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
final int bieginTime = state.beginTimeMinute.value;
|
|
|
|
|
final double bieginDouble = bieginTime / 256;
|
|
|
|
|
final int biegin1 = bieginDouble.toInt();
|
|
|
|
|
final int biegin2 = bieginTime % 256;
|
2024-01-09 18:02:02 +08:00
|
|
|
list.add(biegin1);
|
|
|
|
|
list.add(biegin2);
|
|
|
|
|
// list.add(state.beginTimeMinute.value);
|
|
|
|
|
|
2024-08-21 14:12:15 +08:00
|
|
|
final int endTime = state.endTimeMinute.value;
|
|
|
|
|
final double endDouble = endTime / 256;
|
|
|
|
|
final int end1 = endDouble.toInt();
|
|
|
|
|
final int end2 = endTime % 256;
|
2024-01-09 18:02:02 +08:00
|
|
|
list.add(end1);
|
|
|
|
|
list.add(end2);
|
|
|
|
|
|
2023-10-07 18:55:59 +08:00
|
|
|
list.add(state.isAllDay.value == 1 ? 1:0);
|
2024-01-09 18:02:02 +08:00
|
|
|
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);
|
2024-03-18 16:16:51 +08:00
|
|
|
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
2024-01-02 18:03:50 +08:00
|
|
|
dismissEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
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
|
|
|
}
|