196 lines
6.1 KiB
Dart
196 lines
6.1 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsWithParameters.dart';
|
|
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';
|
|
import '../../../../network/api_repository.dart';
|
|
import '../../../../tools/eventBusEventManage.dart';
|
|
import '../../../../tools/storage.dart';
|
|
import 'automaticBlocking_state.dart';
|
|
|
|
class AutomaticBlockingLogic extends BaseGetXController{
|
|
final AutomaticBlockingState state = AutomaticBlockingState();
|
|
|
|
void setAutoUnLock() async{
|
|
String autoTime;
|
|
if(state.isOpen.value == false){
|
|
autoTime = "0";
|
|
}else{
|
|
if(state.isCustomLockTime.value == true){
|
|
autoTime = state.timeController.text;
|
|
if(int.parse(autoTime) >= 1000){
|
|
showToast("请输入小于1000的数字".tr);
|
|
return;
|
|
}
|
|
}else{
|
|
autoTime = state.autoLockTime.value;
|
|
}
|
|
}
|
|
|
|
var entity = await ApiRepository.to.setAutoUnlock(
|
|
lockId: state.lockSetInfoData.value.lockId!,
|
|
autoLock:state.isOpen.value == true ? 1 : 0,
|
|
autoLockSecond: int.parse(autoTime),
|
|
);
|
|
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));
|
|
Get.back();
|
|
});
|
|
}
|
|
}
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if(reply is SetSupportFunctionsWithParametersReply) {
|
|
_replySetSupportFunctionsWithParameters(reply);
|
|
}
|
|
|
|
// 读取支持功能(带参数)
|
|
// if(reply is ReadSupportFunctionsWithParametersReply) {
|
|
// _readSupportFunctionsWithParametersReply(reply);
|
|
// }
|
|
});
|
|
}
|
|
|
|
// 读取支持功能带参数数据解析
|
|
// 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;
|
|
// }
|
|
// }
|
|
|
|
// 设置自动落锁数据解析
|
|
Future<void> _replySetSupportFunctionsWithParameters(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
state.sureBtnState.value = 0;
|
|
cancelBlueConnetctToastTimer();
|
|
dismissEasyLoading();
|
|
setAutoUnLock();
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
break;
|
|
default:
|
|
state.sureBtnState.value = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 设置支持功能(带参数)
|
|
Future<void> sendAutoLock() async {
|
|
if(state.sureBtnState.value == 1){
|
|
return;
|
|
}
|
|
state.sureBtnState.value = 1;
|
|
|
|
showEasyLoading();
|
|
showBlueConnetctToastTimer(action: (){
|
|
dismissEasyLoading();
|
|
state.sureBtnState.value = 0;
|
|
});
|
|
BlueManage().bludSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async {
|
|
if (connectionState == BluetoothConnectionState.connected) {
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> getPublicKeyList = changeStringListToIntList(publicKey!);
|
|
|
|
String autoTime;
|
|
if(state.isOpen.value == false){
|
|
autoTime = "0";
|
|
}else{
|
|
if(state.isCustomLockTime.value == true){
|
|
autoTime = state.timeController.text;
|
|
}else{
|
|
autoTime = state.autoLockTime.value;
|
|
}
|
|
}
|
|
IoSenderManage.setSupportFunctionsWithParametersCommand(
|
|
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
|
|
userID: await Storage.getUid(),
|
|
featureBit: 29,
|
|
featureParaLength: 2,
|
|
featureData: [int.parse(autoTime)],
|
|
token: getTokenList,
|
|
needAuthor: 1,
|
|
publicKey: getPublicKeyList,
|
|
privateKey: getPrivateKeyList);
|
|
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
|
dismissEasyLoading();
|
|
cancelBlueConnetctToastTimer();
|
|
state.sureBtnState.value = 0;
|
|
if(state.ifCurrentScreen.value == true){
|
|
showBlueConnetctToast();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void ifCanNext() {
|
|
if(((state.isOpen.value != (state.lockSetInfoData.value.lockSettingInfo!.autoLock! == 1 ? true : false)) &&
|
|
(state.autoLockTime.value != state.lockSetInfoData.value.lockSettingInfo!.autoLockSecond!.toString()) &&
|
|
(state.isJustForShow.value == false)) || ((state.isOpen.value = true) && (state.lockSetInfoData.value.lockSettingInfo!.autoLock! == 1) &&
|
|
(state.autoLockTime.value != state.lockSetInfoData.value.lockSettingInfo!.autoLockSecond!.toString()) &&
|
|
(state.isJustForShow.value == false))){
|
|
state.canNext.value = true;
|
|
}else{
|
|
state.canNext.value = false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
|
|
// _readSupportFunctionsWithParameters();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
}
|