234 lines
7.4 KiB
Dart
Raw Normal View History

2023-10-07 18:55:59 +08:00
import 'dart:async';
import 'package:flutter_reactive_ble/flutter_reactive_ble.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 'lockSoundSet_state.dart';
class LockSoundSetLogic extends BaseGetXController {
LockSoundSetState state = LockSoundSetState();
// 配置锁的常开模式设置 -> 锁声音设置
2023-10-07 18:55:59 +08:00
void _setLockSetGeneralSetting() async{
2023-11-01 17:28:59 +08:00
var entity = await ApiRepository.to.setLockSound(
lockId: state.lockSetInfoData.value.lockId!,
lockSound:state.isOpenLockSound.value == true ? 1 : 0,
lockSoundVolume:state.lockSoundLevel.value
2023-09-07 18:36:16 +08:00
);
2023-10-07 18:55:59 +08:00
2023-09-07 18:36:16 +08:00
if(entity.errorCode!.codeIsSuccessful){
2024-01-02 18:03:50 +08:00
// eventBus.fire(RefreshLockListInfoDataEvent());
2024-01-11 15:14:02 +08:00
state.lockSetInfoData.value.lockSettingInfo!.lockSound = state.isOpenLockSound.value == true ? 1 :0;
2023-11-01 17:28:59 +08:00
state.lockSetInfoData.value.lockSettingInfo!.lockSoundVolume = state.lockSoundLevel.value;
eventBus.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
2024-01-23 18:37:03 +08:00
ifCanNext();
2024-01-16 16:52:05 +08:00
showToast("操作成功");
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) {
2023-11-01 17:28:59 +08:00
// 设置支持功能解析(带参数)
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:
// //成功
// print("${reply.commandType}数据解析成功");
// // state.autoLockTime.value = reply.data[7].toString();
//
// break;
// case 0x06:
// //无权限
// print("${reply.commandType}需要鉴权");
//
// break;
// case 0x07:
// //无权限
// print("${reply.commandType}用户无权限");
//
// break;
// case 0x09:
// // 权限校验错误
// print("${reply.commandType}权限校验错误");
//
// break;
// default:
// //失败
// print("${reply.commandType}失败");
//
// break;
// }
// }
2023-10-07 18:55:59 +08:00
2023-11-01 17:28:59 +08:00
// 设置支持功能解析
2023-10-07 18:55:59 +08:00
Future<void> _replySetSupportFunctionsWithParameters(Reply reply) async {
int status = reply.data[2];
switch(status){
case 0x00:
//成功
print("${reply.commandType}数据解析成功");
state.sureBtnState.value = 0;
cancelBlueConnetctToastTimer();
dismissEasyLoading();
2023-10-07 18:55:59 +08:00
_setLockSetGeneralSetting();
break;
case 0x06:
//无权限
print("${reply.commandType}需要鉴权");
break;
case 0x07:
//无权限
print("${reply.commandType}用户无权限");
break;
case 0x09:
// 权限校验错误
print("${reply.commandType}权限校验错误");
break;
default:
//失败
print("${reply.commandType}失败");
break;
}
}
// 读取支持功能-带参数
2024-01-19 18:15:42 +08:00
// Future<void> _readSupportFunctionsWithParameters() async {
// BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async {
// if (connectionState == DeviceConnectionState.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!);
//
// IoSenderManage.readSupportFunctionsWithParametersCommand(
// keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
// userID: await Storage.getUid(),
// featureBit: 33,
// token: getTokenList,
// needAuthor: 1,
// publicKey: getPublicKeyList,
// privateKey: getPrivateKeyList
// );
// }
// });
// }
2023-10-07 18:55:59 +08:00
// 设置支持功能(带参数)
Future<void> sendLockSound() 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().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async {
2023-10-07 18:55:59 +08:00
if (connectionState == DeviceConnectionState.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!);
int type;
if(state.isOpenLockSound.value == false){
// 音量关了的时候
type = 0;
}else{
// 音量开了的时候
type = state.lockSoundLevel.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: 33,
featureParaLength: 1,
featureData: [type],
token: getTokenList,
needAuthor: 1,
publicKey: getPublicKeyList,
privateKey: getPrivateKeyList);
2024-01-02 18:03:50 +08:00
} else if (connectionState == DeviceConnectionState.disconnected) {
dismissEasyLoading();
cancelBlueConnetctToastTimer();
state.sureBtnState.value = 0;
if(state.ifCurrentScreen.value == true){
showBlueConnetctToast();
}
2023-10-07 18:55:59 +08:00
}
});
}
2024-01-23 18:37:03 +08:00
void ifCanNext() {
if((state.lockSoundLevel.value != state.lockSetInfoData.value.lockSettingInfo!.lockSoundVolume!) || (state.isOpenLockSound.value != (state.lockSetInfoData.value.lockSettingInfo!.lockSound == 1 ? true : false))){
state.canNext.value = true;
}else{
state.canNext.value = false;
}
}
2023-10-07 18:55:59 +08:00
@override
void onReady() {
// TODO: implement onReady
super.onReady();
print("onReady()");
_initReplySubscription();
}
@override
void onInit() {
// TODO: implement onInit
super.onInit();
print("onInit()");
2024-01-19 18:15:42 +08:00
// _readSupportFunctionsWithParameters();
2023-10-07 18:55:59 +08:00
}
@override
void onClose() {
// TODO: implement onClose
super.onClose();
_replySubscription.cancel();
}
2024-01-23 17:36:02 +08:00
}