223 lines
7.1 KiB
Dart
223 lines
7.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
import '../../../../appRouters.dart';
|
|
import '../../../../app_settings/app_settings.dart';
|
|
import '../../../../blue/blue_manage.dart';
|
|
import '../../../../blue/io_gateway/io_gateway_getStatus.dart';
|
|
import '../../../../blue/io_reply.dart';
|
|
import '../../../../blue/io_tool/io_tool.dart';
|
|
import '../../../../blue/io_tool/manager_event_bus.dart';
|
|
import '../../../../blue/io_type.dart';
|
|
import '../../../../blue/sender_manage.dart';
|
|
import '../../../../tools/storage.dart';
|
|
import '../../../../widget/permission/permission_dialog.dart';
|
|
import 'getGatewayInfo_model.dart';
|
|
import 'selectGatewayList_state.dart';
|
|
|
|
class SelectGatewayListLogic extends BaseGetXController {
|
|
SelectGatewayListState state = SelectGatewayListState();
|
|
|
|
// 监听设备返回的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
|
|
void _initReplySubscription() {
|
|
_replySubscription =
|
|
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
|
|
// 获取网关状态
|
|
if (reply is GatewayGetStatusReply) {
|
|
_replyGatewayGetStatusReply(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _replyGatewayGetStatusReply(Reply reply) async {
|
|
final int status = reply.data[2];
|
|
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
// state.sureBtnState.value = 0;
|
|
cancelBlueConnetctToastTimer();
|
|
dismissEasyLoading();
|
|
|
|
final GetGatewayInfoModel gatewayModel = GetGatewayInfoModel();
|
|
gatewayModel.gatewayName =
|
|
state.currentDevice!.advertisementData.advName;
|
|
|
|
// 网关MAC地址
|
|
int index = 3;
|
|
final List<int> macList = reply.data.sublist(index, index + 20);
|
|
final String macStr = utf8String(macList);
|
|
// lockInfo['mac'] = macStr;
|
|
gatewayModel.mac = macStr;
|
|
index = index + 20;
|
|
// AppLog.log('网关MAC地址 macList:$macList macStr:$macStr');
|
|
|
|
// 网关序列号
|
|
final List<int> serialNum = reply.data.sublist(index, index + 20);
|
|
final String serialNumStr = utf8String(serialNum);
|
|
// lockInfo['serialNum'] = serialNumStr;
|
|
gatewayModel.serialNum = serialNumStr;
|
|
index = index + 20;
|
|
// AppLog.log('网关序列号 serialNum:$serialNum serialNumStr:$serialNumStr');
|
|
|
|
// 网关版本
|
|
final List<int> gatewayVersion = reply.data.sublist(index, index + 20);
|
|
final String gatewayVersionStr = utf8String(gatewayVersion);
|
|
// lockInfo['gatewayVersion'] = gatewayVersionStr;
|
|
gatewayModel.gatewayVersion = gatewayVersionStr;
|
|
index = index + 20;
|
|
// AppLog.log('软件版本 gatewayVersion:$gatewayVersion gatewayVersionStr:$gatewayVersionStr');
|
|
|
|
// wifiMac地址
|
|
final List<int> wifiMac = reply.data.sublist(index, index + 20);
|
|
final String wifiMacStr = utf8String(wifiMac);
|
|
// lockInfo['wifiMac'] = wifiMacStr;
|
|
gatewayModel.wifiMac = wifiMacStr;
|
|
index = index + 20;
|
|
AppLog.log('wifiMac地址 wifiMac:$wifiMac wifiMacStr:$wifiMacStr');
|
|
// gatewayModel.wifiMac = '00:00:00:00:00:00';
|
|
|
|
Get.toNamed(Routers.gatewayGetWifiListPage, arguments: {
|
|
'gatewayModel': gatewayModel,
|
|
});
|
|
break;
|
|
default:
|
|
//失败
|
|
dismissEasyLoading();
|
|
showToast('配网失败'.tr);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void startScanBlueList() {
|
|
state.devices.clear();
|
|
BlueManage().startScan(2000, DeviceType.gateway, (List<ScanResult> list) {
|
|
// AppLog.log('ScanResultList:$list');
|
|
for (int i = 0; i < list.length; i++) {
|
|
final ScanResult device = list[i];
|
|
// if ((device.advertisementData.serviceUuids.isNotEmpty
|
|
// ? device.advertisementData.serviceUuids[0]
|
|
// : '')
|
|
// .toString()[31] !=
|
|
// '1') {
|
|
if (!state.devices.contains(device)) {
|
|
state.devices.add(device);
|
|
} else {
|
|
// 包含的情况下替换
|
|
state.devices[state.devices.indexOf(device)] = device;
|
|
}
|
|
// AppLog.log('device:${list[i]}');
|
|
// }
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> stopScanBlueList() async {
|
|
// BlueManage().disconnect();
|
|
state.devices.clear();
|
|
await BlueManage().stopScan();
|
|
}
|
|
|
|
void blueDisconnect() {
|
|
BlueManage().disconnect();
|
|
}
|
|
|
|
Future<void> getNearByLimits() async {
|
|
if (!Platform.isIOS) {
|
|
final bool bluetoothRequest = await PermissionDialog.requestBluetooth();
|
|
final bool locationRequest =
|
|
await PermissionDialog.request(Permission.location);
|
|
AppLog.log('locationRequest:$locationRequest');
|
|
if (!bluetoothRequest || !locationRequest) {
|
|
return;
|
|
}
|
|
}
|
|
startScanBlueList();
|
|
}
|
|
|
|
// 获取网关状态
|
|
Future<void> senderGatewayGetStatusAction() async {
|
|
showEasyLoading();
|
|
showBlueConnetctToastTimer(action: () {
|
|
dismissEasyLoading();
|
|
});
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
|
(BluetoothConnectionState connectionState) async {
|
|
if (connectionState == BluetoothConnectionState.connected) {
|
|
IoSenderManage.gatewayGetStatusCommand(
|
|
lockID: BlueManage().connectDeviceName,
|
|
userID: await Storage.getUid(),
|
|
);
|
|
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
|
dismissEasyLoading();
|
|
cancelBlueConnetctToastTimer();
|
|
if (state.ifCurrentScreen.value == true) {
|
|
showBlueConnetctToast();
|
|
}
|
|
}
|
|
}, isAddEquipment: true);
|
|
}
|
|
|
|
// 点击连接设备
|
|
void connect(ScanResult device) {
|
|
showEasyLoading();
|
|
showBlueConnetctToastTimer(action: () {
|
|
dismissEasyLoading();
|
|
});
|
|
BlueManage().blueSendData(device.advertisementData.advName,
|
|
(BluetoothConnectionState bluetoothConnectionState) async {
|
|
// AppLog.log('点击要添加的设备了');
|
|
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
|
dismissEasyLoading();
|
|
|
|
// 没有网关调试,做假数据调试
|
|
// final GetGatewayInfoModel gatewayModel = GetGatewayInfoModel();
|
|
// gatewayModel.mac = 'C1:1E:0D:E0:0C:A9';
|
|
// gatewayModel.serialNum = DateTime.now().millisecondsSinceEpoch.toString();
|
|
// gatewayModel.gatewayVersion = '1.0.1';
|
|
// gatewayModel.wifiMac = 'C1:1E:0D:E0:0C:A9';
|
|
// Get.toNamed(Routers.gatewayGetWifiListPage, arguments: {
|
|
// 'gatewayModel':gatewayModel,
|
|
// });
|
|
|
|
senderGatewayGetStatusAction();
|
|
} else {
|
|
dismissEasyLoading();
|
|
}
|
|
}, isAddEquipment: true);
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
super.onReady();
|
|
|
|
AppLog.log('扫描网关列表 onReadyonReady被调用了');
|
|
getNearByLimits();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_replySubscription.cancel();
|
|
super.dispose();
|
|
}
|
|
@override
|
|
void onClose() {
|
|
_replySubscription.cancel();
|
|
super.onClose();
|
|
}
|
|
}
|