191 lines
5.4 KiB
Dart
191 lines
5.4 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_automaticPadlock.dart';
|
|
import '../../../../blue/io_protocol/io_getLockStatu.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 'automaticBlocking_state.dart';
|
|
|
|
class AutomaticBlockingLogic extends BaseGetXController{
|
|
final AutomaticBlockingState state = AutomaticBlockingState();
|
|
|
|
void setAutoUnLock() async{
|
|
String autoTime;
|
|
if(state.isOpen.value == false){
|
|
autoTime = "-1";
|
|
}else{
|
|
autoTime = state.autoLockTime.value;
|
|
}
|
|
|
|
var entity = await ApiRepository.to.setAutoUnlock(
|
|
lockId: state.getKeyInfosData.value.lockId.toString(),
|
|
autoLockTime:autoTime,
|
|
type: "1",
|
|
);
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
state.getKeyInfosData.value.autoLockTime = int.parse(autoTime);
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
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 AutomaticPadlockReply) {
|
|
_replyAutomaticPadlock(reply);
|
|
}
|
|
|
|
// 获取锁状态
|
|
if(reply is GetLockStatuReply) {
|
|
_replyGetLockStatus(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 获取锁状态数据解析
|
|
Future<void> _replyGetLockStatus(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
|
|
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> _replyAutomaticPadlock(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
// _getLockStatus();
|
|
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> _getLockStatus() 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!);
|
|
IoSenderManage.senderGetLockStatu(
|
|
lockID:BlueManage().connectDeviceName,
|
|
userID:await Storage.getUid(),
|
|
privateKey:getPrivateKeyList,
|
|
);
|
|
}
|
|
}, isShowLoading: false);
|
|
}
|
|
|
|
// 自动落锁
|
|
Future<void> sendAutoLock() 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 signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> getSignKeyList = changeStringListToIntList(signKey!);
|
|
|
|
print("automaticBlockingState.isOpen.value:${state.isOpen.value}");
|
|
IoSenderManage.senderAutomaticPadlockCommand(
|
|
lockID:BlueManage().connectDeviceName,
|
|
userID:await Storage.getUid(),
|
|
autoLockFlag:state.isOpen.value ? 1 : 0,
|
|
token:getTokenList,
|
|
needAuthor:1,
|
|
signKey:getSignKeyList,
|
|
privateKey:getPrivateKeyList,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
_getLockStatus();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
}
|