216 lines
6.7 KiB
Dart
216 lines
6.7 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||
import 'package:star_lock/tools/baseGetXController.dart';
|
||
|
||
import '../../../../blue/blue_manage.dart';
|
||
import '../../../../blue/io_protocol/io_readSupportFunctionsWithParameters.dart';
|
||
import '../../../../blue/io_protocol/io_setSupportFunctionsWithParameters.dart';
|
||
import '../../../../blue/io_reply.dart';
|
||
import '../../../../blue/io_tool/io_manager.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 '../../../../tools/toast.dart';
|
||
import 'lockSoundSet_state.dart';
|
||
|
||
class LockSoundSetLogic extends BaseGetXController {
|
||
LockSoundSetState state = LockSoundSetState();
|
||
|
||
// 配置锁的常开模式设置 -> 锁声音设置
|
||
void _setLockSetGeneralSetting() async{
|
||
int isOn;
|
||
int type;
|
||
if(state.isOpenLockSound.value == false){
|
||
// 音量关了的时候
|
||
isOn = 2; type = 6;
|
||
}else{
|
||
// 音量开了的时候
|
||
isOn = state.lockSoundLevel.value; type = 8;
|
||
}
|
||
var entity = await ApiRepository.to.setLockSetGeneralSettingData(
|
||
lockId: state.getKeyInfosData.value.lockId.toString(),
|
||
changeType:"1",
|
||
isOn:isOn.toString(), // 1-开启、2-关闭; 设置音量:1到5;
|
||
type:type.toString(), // 6-锁声音开关 8-音量、
|
||
);
|
||
|
||
if(entity.errorCode!.codeIsSuccessful){
|
||
eventBus.fire(RefreshLockListInfoDataEvent());
|
||
state.getKeyInfosData.value.lockSound = state.isOpenLockSound.value == true ? 1 :2;
|
||
state.getKeyInfosData.value.volume = state.lockSoundLevel.value;
|
||
eventBus.fire(PassCurrentLockInformationEvent(state.getKeyInfosData.value));
|
||
Toast.show(msg: "操作成功");
|
||
}
|
||
}
|
||
|
||
// 获取解析后的数据
|
||
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:
|
||
//成功
|
||
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;
|
||
}
|
||
}
|
||
|
||
// 设置自动落锁数据解析
|
||
Future<void> _replySetSupportFunctionsWithParameters(Reply reply) async {
|
||
int status = reply.data[2];
|
||
switch(status){
|
||
case 0x00:
|
||
//成功
|
||
print("${reply.commandType}数据解析成功");
|
||
_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;
|
||
}
|
||
}
|
||
|
||
// 读取支持功能-带参数
|
||
Future<void> _readSupportFunctionsWithParameters() async {
|
||
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, 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.getKeyInfosData.value.keyId.toString(),
|
||
userID: await Storage.getUid(),
|
||
featureBit: 33,
|
||
token: getTokenList,
|
||
needAuthor: 1,
|
||
publicKey: getPublicKeyList,
|
||
privateKey: getPrivateKeyList
|
||
);
|
||
}
|
||
}, isShowLoading: true);
|
||
}
|
||
|
||
// 设置支持功能(带参数)
|
||
Future<void> sendLockSound() async {
|
||
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, 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!);
|
||
|
||
int type;
|
||
if(state.isOpenLockSound.value == false){
|
||
// 音量关了的时候
|
||
type = 0;
|
||
}else{
|
||
// 音量开了的时候
|
||
type = state.lockSoundLevel.value;
|
||
}
|
||
IoSenderManage.setSupportFunctionsWithParametersCommand(
|
||
keyID: state.getKeyInfosData.value.keyId.toString(),
|
||
userID: await Storage.getUid(),
|
||
featureBit: 33,
|
||
featureParaLength: 1,
|
||
featureData: [type],
|
||
token: getTokenList,
|
||
needAuthor: 1,
|
||
publicKey: getPublicKeyList,
|
||
privateKey: getPrivateKeyList);
|
||
}
|
||
});
|
||
}
|
||
|
||
@override
|
||
void onReady() {
|
||
// TODO: implement onReady
|
||
super.onReady();
|
||
print("onReady()");
|
||
|
||
_initReplySubscription();
|
||
}
|
||
|
||
@override
|
||
void onInit() {
|
||
// TODO: implement onInit
|
||
super.onInit();
|
||
print("onInit()");
|
||
|
||
_readSupportFunctionsWithParameters();
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
// TODO: implement onClose
|
||
super.onClose();
|
||
_replySubscription.cancel();
|
||
}
|
||
|
||
} |