优化蓝牙逻辑,添加开锁成功弹窗

This commit is contained in:
魏少阳 2024-01-05 13:56:34 +08:00
parent 5f1e594de3
commit 264bab98ae
7 changed files with 211 additions and 87 deletions

View File

@ -90,7 +90,7 @@ class BlueManage {
}); });
} }
/// ///
Future<void> bludSendData(String deviceName, ConnectStateCallBack stateCallBack) async { Future<void> bludSendData(String deviceName, ConnectStateCallBack stateCallBack) async {
if(deviceConnectionState != DeviceConnectionState.connected){ if(deviceConnectionState != DeviceConnectionState.connected){
_connect(deviceName, (state){ _connect(deviceName, (state){

View File

@ -257,7 +257,9 @@ class LockSetLogic extends BaseGetXController {
// //
Future<void> deletUserAction() async { Future<void> deletUserAction() async {
showEasyLoading(); showEasyLoading();
showBlueConnetctToastTimer(); showBlueConnetctToastTimer((){
});
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async { BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async {
cancelBlueConnetctToastTimer(); cancelBlueConnetctToastTimer();
if (connectionState == DeviceConnectionState.connected) { if (connectionState == DeviceConnectionState.connected) {
@ -293,7 +295,9 @@ class LockSetLogic extends BaseGetXController {
// //
Future<void> factoryDataResetAction() async { Future<void> factoryDataResetAction() async {
showEasyLoading(); showEasyLoading();
showBlueConnetctToastTimer(); showBlueConnetctToastTimer((){
});
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async { BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async {
cancelBlueConnetctToastTimer(); cancelBlueConnetctToastTimer();
if (connectionState == DeviceConnectionState.connected) { if (connectionState == DeviceConnectionState.connected) {
@ -351,7 +355,9 @@ class LockSetLogic extends BaseGetXController {
// () // ()
Future<void> sendBurglarAlarm(int type) async { Future<void> sendBurglarAlarm(int type) async {
showEasyLoading(); showEasyLoading();
showBlueConnetctToastTimer(); showBlueConnetctToastTimer((){
});
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async { BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async {
cancelBlueConnetctToastTimer(); cancelBlueConnetctToastTimer();
if (connectionState == DeviceConnectionState.connected) { if (connectionState == DeviceConnectionState.connected) {

View File

@ -4,12 +4,16 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:star_lock/blue/io_protocol/io_editUser.dart'; import 'package:star_lock/blue/io_protocol/io_editUser.dart';
import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart'; import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart';
import 'package:star_lock/blue/io_type.dart'; import 'package:star_lock/blue/io_type.dart';
import 'package:star_lock/tools/toast.dart'; import 'package:star_lock/tools/toast.dart';
import '../../../app_settings/app_colors.dart';
import '../../../blue/blue_manage.dart'; import '../../../blue/blue_manage.dart';
import '../../../blue/io_protocol/io_addUser.dart'; import '../../../blue/io_protocol/io_addUser.dart';
import '../../../blue/io_protocol/io_getStarLockStatusInfo.dart'; import '../../../blue/io_protocol/io_getStarLockStatusInfo.dart';
@ -72,16 +76,17 @@ class LockDetailLogic extends BaseGetXController {
case 0x00: case 0x00:
// //
print("${reply.commandType}数据解析成功"); print("${reply.commandType}数据解析成功");
_showFullScreenOverlay(Get.context!);
// //
int power = reply.data[7]; int power = reply.data[7];
state.electricQuantity.value = power; state.electricQuantity.value = power;
cancelBlueConnetctToastTimer();
getLockRecordLastUploadDataTime(); getLockRecordLastUploadDataTime();
state.openLockBtnState.value = 0; state.openLockBtnState.value = 0;
state.animationController.reset(); // state.animationController.reset();
state.animationController.forward(); // state.animationController.forward();
// state.animationController.isCompleted;
break; break;
case 0x06: case 0x06:
// //
@ -343,15 +348,66 @@ class LockDetailLogic extends BaseGetXController {
} }
} }
// ()
Future<void> addUserConnectBlue() async {
showBlueConnetctToastTimer(() {
state.openLockBtnState.value = 0;
// state.animationController.reset();
// state.animationController.forward();
});
BlueManage().bludSendData(state.keyInfos.value.bluetooth!.bluetoothDeviceName!, (DeviceConnectionState deviceConnectionState) async {
if (deviceConnectionState == DeviceConnectionState.connected) {
//
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
var token = await Storage.getStringList(saveBlueToken);
List<int> getTokenList = [0, 0, 0, 0];
if (token != null) {
getTokenList = changeStringListToIntList(token);
}
print("BlueManage().connectDeviceName:${BlueManage().connectDeviceName} authUserID:${state.senderUserId.toString()} keyID:${state.keyInfos.value.keyId.toString()} userID:${await Storage.getUid()}");
IoSenderManage.senderAddUser(
lockID: BlueManage().connectDeviceName,
authUserID: state.senderUserId.toString(),
keyID: state.keyInfos.value.keyId.toString(),
userID: await Storage.getUid(),
openMode: 1,
keyType: 0,
startDate: DateTime.now().millisecondsSinceEpoch,
expireDate: 0x11223344,
role: 0,
password: "123456",
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
token: getTokenList);
}else if (deviceConnectionState == DeviceConnectionState.disconnected){
cancelBlueConnetctToastTimer();
if(state.ifCurrentScreen.value == true){
showBlueConnetctToast();
}
state.openLockBtnState.value = 0;
// state.animationController.reset();
// state.animationController.forward();
}
});
}
// //
Future<void> openDoorAction() async { Future<void> openDoorAction() async {
showBlueConnetctToastTimer(); showBlueConnetctToastTimer(() {
state.openLockBtnState.value = 0;
// state.animationController.reset();
// state.animationController.forward();
});
BlueManage().bludSendData(state.keyInfos.value.bluetooth!.bluetoothDeviceName!, (DeviceConnectionState deviceConnectionState) async { BlueManage().bludSendData(state.keyInfos.value.bluetooth!.bluetoothDeviceName!, (DeviceConnectionState deviceConnectionState) async {
cancelBlueConnetctToastTimer();
if (deviceConnectionState == DeviceConnectionState.connected){ if (deviceConnectionState == DeviceConnectionState.connected){
state.connectState.value = 1;
state.openLockBtnState.value = 0;
var privateKey = await Storage.getStringList(saveBluePrivateKey); var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!); List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
@ -374,14 +430,14 @@ class LockDetailLogic extends BaseGetXController {
privateKey: getPrivateKeyList, privateKey: getPrivateKeyList,
); );
}else if (deviceConnectionState == DeviceConnectionState.disconnected){ }else if (deviceConnectionState == DeviceConnectionState.disconnected){
// if(state.lockState.value != 2){ cancelBlueConnetctToastTimer();
// // 2 toast if(state.ifCurrentScreen.value == true){
showBlueConnetctToast(); showBlueConnetctToast();
// } }
state.connectState.value = 0;
state.openLockBtnState.value = 0; state.openLockBtnState.value = 0;
state.animationController.reset(); // state.animationController.reset();
state.animationController.forward(); // state.animationController.forward();
} }
}); });
} }
@ -445,7 +501,6 @@ class LockDetailLogic extends BaseGetXController {
// }, isShowLoading: false); // }, isShowLoading: false);
// } // }
// () // ()
Future<void> senderReferEventRecordTime(int time) async { Future<void> senderReferEventRecordTime(int time) async {
BlueManage().bludSendData(BlueManage().connectDeviceName, BlueManage().bludSendData(BlueManage().connectDeviceName,
@ -475,54 +530,13 @@ class LockDetailLogic extends BaseGetXController {
}); });
} }
// () //
Future<void> addUserConnectBlue() async {
//
BlueManage().bludSendData(BlueManage().connectDeviceName,
(DeviceConnectionState deviceConnectionState) async {
if (deviceConnectionState == DeviceConnectionState.connected) {
//
var privateKey = await Storage.getStringList(saveBluePrivateKey);
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
var publicKey = await Storage.getStringList(saveBluePublicKey);
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
var token = await Storage.getStringList(saveBlueToken);
List<int> getTokenList = [0, 0, 0, 0];
if (token != null) {
getTokenList = changeStringListToIntList(token);
}
print(
"BlueManage().connectDeviceName:${BlueManage().connectDeviceName} authUserID:${state.senderUserId.toString()} keyID:${state.keyInfos.value.keyId.toString()} userID:${await Storage.getUid()}");
IoSenderManage.senderAddUser(
lockID: BlueManage().connectDeviceName,
authUserID: state.senderUserId.toString(),
keyID: state.keyInfos.value.keyId.toString(),
userID: await Storage.getUid(),
openMode: 1,
keyType: 0,
startDate: DateTime.now().millisecondsSinceEpoch,
expireDate: 0x11223344,
role: 0,
password: "123456",
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
token: getTokenList);
}
});
}
//
startOpenLock() { startOpenLock() {
// if(state.lockState.value == 4){ if(state.openLockBtnState.value == 1){
// // 4 return;
// state.lockState.value = 3; }
// }else{ state.iSClosedUnlockSuccessfulPopup.value = false;
// state.openLockBtnState.value = 1;
state.openLockBtnState.value = 1;
// }
state.animationController.forward(); state.animationController.forward();
if (state.lockUserNo == 0) { if (state.lockUserNo == 0) {
@ -609,9 +623,7 @@ class LockDetailLogic extends BaseGetXController {
_lockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceEvent; _lockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceEvent;
void _initLockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceAction() { void _initLockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceAction() {
// eventBus // eventBus
_lockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceEvent = eventBus _lockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceEvent = eventBus.on<LockSetChangeSetRefreshLockDetailWithType>().listen((event) {
.on<LockSetChangeSetRefreshLockDetailWithType>()
.listen((event) {
if (event.type == 0) { if (event.type == 0) {
// 0 // 0
state.isAttendance.value = event.setResult; state.isAttendance.value = event.setResult;
@ -624,6 +636,90 @@ class LockDetailLogic extends BaseGetXController {
}); });
} }
late StreamSubscription<List<DiscoveredDevice>>_scanListDiscoveredDeviceSubscription;
void _scanListDiscoveredDeviceSubscriptionAction() {
_scanListDiscoveredDeviceSubscription = EventBusManager().eventBus!.on<List<DiscoveredDevice>>().listen((List<DiscoveredDevice> list) {
final knownDeviceIndex = list.indexWhere((d) => d.name == state.keyInfos.value.bluetooth!.bluetoothDeviceName!);
if (knownDeviceIndex >= 0) {
//
state.currentDeviceUUid.value = (list[knownDeviceIndex].serviceUuids.isNotEmpty ? list[knownDeviceIndex].serviceUuids[0] : "").toString();
}
});
}
//
void _showFullScreenOverlay(BuildContext context) {
Future.delayed(const Duration(seconds: 3), () {
if(state.iSClosedUnlockSuccessfulPopup.value == true){
return;
}
state.iSClosedUnlockSuccessfulPopup.value = true;
Get.back();
});
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
if(state.iSClosedUnlockSuccessfulPopup.value == true){
return;
}
state.iSClosedUnlockSuccessfulPopup.value = true;
Get.back();
},
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.black.withOpacity(0.2),
child: _unlockSuccessWidget(),
),
);
},
);
}
Widget _unlockSuccessWidget() {
return Stack(
alignment: Alignment.center,
children: [
Image.asset(
'images/main/unlocked_bg.png',
width: 358.w,
height: 348.h,
),
Positioned(
top: ScreenUtil().screenHeight / 2,
child: Column(
children: [
Text(
state.keyInfos.value.lockAlias!,
style: TextStyle(
color: AppColors.placeholderTextColor, fontSize: 24.sp),
),
SizedBox(
height: 10.h,
),
Text(
getCurrentFormattedTime(),
style: TextStyle(
color: AppColors.darkGrayTextColor, fontSize: 24.sp),
)
],
))
],
);
}
String getCurrentFormattedTime() {
//
DateTime now = DateTime.now();
//
String formattedTime = DateFormat('MM/dd HH:mm').format(now);
return formattedTime;
}
@override @override
void onReady() { void onReady() {
// TODO: implement onReady // TODO: implement onReady
@ -632,6 +728,10 @@ class LockDetailLogic extends BaseGetXController {
_initReplySubscription(); _initReplySubscription();
_initLockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceAction(); _initLockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceAction();
_scanListDiscoveredDeviceSubscriptionAction();
Future.delayed(const Duration(seconds: 1)).then((value) {
});
} }
@override @override
@ -652,5 +752,6 @@ class LockDetailLogic extends BaseGetXController {
_replySubscription.cancel(); _replySubscription.cancel();
_lockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceEvent!.cancel(); _lockSetOpenOrCloseCheckInRefreshLockDetailWithAttendanceEvent!.cancel();
_scanListDiscoveredDeviceSubscription.cancel();
} }
} }

View File

@ -121,7 +121,8 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
child: Obx(() => Stack( child: Obx(() => Stack(
children: [ children: [
Image.asset( Image.asset(
state.connectState.value == 0 ? 'images/main/icon_main_openLockBtn_grey.png' : 'images/main/icon_main_openLockBtn_center.png', // state.connectState.value == 0 ? 'images/main/icon_main_openLockBtn_grey.png' : 'images/main/icon_main_openLockBtn_center.png',
'images/main/icon_main_openLockBtn_center.png',
width: 330.w, width: 330.w,
height: 330.w, height: 330.w,
), ),
@ -136,7 +137,7 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
], ],
)), )),
onTap: () { onTap: () {
Get.log("点击开锁"); // Get.log("点击开锁");
logic.startOpenLock(); logic.startOpenLock();
}, },
onLongPressStart: (details) { onLongPressStart: (details) {
@ -504,14 +505,14 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
height: MediaQuery.of(context).size.height, height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width, width: MediaQuery.of(context).size.width,
color: Colors.black.withOpacity(0.5), color: Colors.black.withOpacity(0.5),
child: unlockSuccessWidget(), child: _unlockSuccessWidget(),
), ),
); );
}, },
); );
} }
Widget unlockSuccessWidget() { Widget _unlockSuccessWidget() {
return Stack( return Stack(
alignment: Alignment.center, alignment: Alignment.center,
children: [ children: [
@ -546,10 +547,8 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
String getCurrentFormattedTime() { String getCurrentFormattedTime() {
// //
DateTime now = DateTime.now(); DateTime now = DateTime.now();
// //
String formattedTime = DateFormat('MM/dd HH:mm').format(now); String formattedTime = DateFormat('MM/dd HH:mm').format(now);
return formattedTime; return formattedTime;
} }
@ -559,15 +558,18 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
super.didChangeDependencies(); super.didChangeDependencies();
/// ///
// AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!); AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!);
} }
@override @override
void dispose() { void dispose() {
// TODO: implement dispose // TODO: implement dispose
/// ///
print("LockDetailPage===dispose");
AppRouteObserver().routeObserver.unsubscribe(this); AppRouteObserver().routeObserver.unsubscribe(this);
state.animationController.dispose(); // state.animationController.reset();
// state.animationController.forward();
// state.animationController.dispose();
super.dispose(); super.dispose();
BlueManage().disconnect(BlueManage().connectDeviceMacAddress); BlueManage().disconnect(BlueManage().connectDeviceMacAddress);
} }
@ -577,6 +579,7 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
void didPush() { void didPush() {
super.didPush(); super.didPush();
print("LockDetailPage===didPush"); print("LockDetailPage===didPush");
state.ifCurrentScreen.value = true;
} }
/// ///
@ -584,13 +587,18 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
void didPop() { void didPop() {
super.didPop(); super.didPop();
print("LockDetailPage===didPop"); print("LockDetailPage===didPop");
state.ifCurrentScreen.value = false;
logic.cancelBlueConnetctToastTimer(); logic.cancelBlueConnetctToastTimer();
state.animationController.reset();
state.animationController.forward();
state.animationController.dispose();
} }
/// ///
@override @override
void didPopNext() { void didPopNext() {
super.didPopNext(); super.didPopNext();
state.ifCurrentScreen.value = true;
print("LockDetailPage===didPopNext"); print("LockDetailPage===didPopNext");
} }
@ -599,7 +607,10 @@ class _LockDetailPageState extends State<LockDetailPage> with TickerProviderStat
void didPushNext() { void didPushNext() {
super.didPushNext(); super.didPushNext();
print("LockDetailPage===didPushNext"); print("LockDetailPage===didPushNext");
state.ifCurrentScreen.value = false;
logic.cancelBlueConnetctToastTimer(); logic.cancelBlueConnetctToastTimer();
state.animationController.reset();
state.animationController.forward();
} }
} }

View File

@ -26,9 +26,12 @@ class LockDetailState {
var currentDeviceUUid = "".obs;// uuid var currentDeviceUUid = "".obs;// uuid
var ifCurrentScreen = true.obs; // ,
var iSClosedUnlockSuccessfulPopup = false.obs; //
// //
late AnimationController animationController; late AnimationController animationController;
// var lockState = 0.obs;// 0 1() 2 3 4 5 // var lockState = 0.obs;// 0 1() 2 3 4 5
var openLockBtnState = 0.obs;// 0() 1() var openLockBtnState = 0.obs;// 0() 1()
var connectState = 0.obs;// 0 1 // var connectState = 0.obs;// 0 1
} }

View File

@ -45,10 +45,6 @@ class BaseGetXController extends GetxController{
Get.log('onClose -----> $runtimeType'); Get.log('onClose -----> $runtimeType');
} }
// operationFailedCallBack(e){
// showOperationFailed();
// }
Future delay({Duration? duration,Function? something}) => Future.delayed(duration ?? 500. milliseconds,something as FutureOr Function()?); Future delay({Duration? duration,Function? something}) => Future.delayed(duration ?? 500. milliseconds,something as FutureOr Function()?);
void showEasyLoading() => EasyLoading.show(); void showEasyLoading() => EasyLoading.show();
@ -57,12 +53,17 @@ class BaseGetXController extends GetxController{
Timer? _timer; Timer? _timer;
// CancelableOperation? _operation; // CancelableOperation? _operation;
void showBlueConnetctToastTimer() { void showBlueConnetctToastTimer(Function? something) {
if(_timer != null && _timer!.isActive){ if(_timer != null){
_timer!.cancel(); _timer!.cancel();
_timer = null;
} }
_timer = Timer.periodic(15.seconds, (timer) { _timer = Timer.periodic(15.seconds, (timer) {
cancelBlueConnetctToastTimer();
showBlueConnetctToast(); showBlueConnetctToast();
if(something != null) {
something();
}
}); });
// _operation = CancelableOperation.fromFuture( // _operation = CancelableOperation.fromFuture(
// Future.delayed(const Duration(seconds: 15), () { // Future.delayed(const Duration(seconds: 15), () {
@ -74,7 +75,10 @@ class BaseGetXController extends GetxController{
void cancelBlueConnetctToastTimer() { void cancelBlueConnetctToastTimer() {
Get.log('cancelBlueConnetctToastTimer'); Get.log('cancelBlueConnetctToastTimer');
if(_timer != null && _timer!.isActive) _timer!.cancel(); if(_timer != null){
_timer!.cancel();
_timer = null;
}
// _operation?.cancel(); // _operation?.cancel();
} }
@ -136,7 +140,6 @@ class BaseGetXController extends GetxController{
} }
///Extension_Int ///Extension_Int
extension Extension_Int on int { extension Extension_Int on int {
bool get codeIsSuccessful => this == 0; bool get codeIsSuccessful => this == 0;

View File

@ -128,7 +128,7 @@ dependencies:
# ffmpeg_kit_flutter: 5.1.0-LTS # ffmpeg_kit_flutter: 5.1.0-LTS
fast_gbk: ^1.0.0 fast_gbk: ^1.0.0
flutter_pcm_sound: ^1.1.0 flutter_pcm_sound: ^1.1.0
intl: ^0.18.1 intl: ^0.18.0
# flutter_audio_capture: <1.1.5 # flutter_audio_capture: <1.1.5
flutter_voice_processor: ^1.1.0 flutter_voice_processor: ^1.1.0