2023-09-07 18:36:16 +08:00
|
|
|
import 'dart:async';
|
2023-12-11 13:44:15 +08:00
|
|
|
import 'dart:io';
|
2023-09-07 18:36:16 +08:00
|
|
|
|
2024-03-18 16:16:51 +08:00
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
2024-04-15 16:30:00 +08:00
|
|
|
import 'package:get/get.dart';
|
2023-09-07 18:36:16 +08:00
|
|
|
import 'package:network_info_plus/network_info_plus.dart';
|
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2024-08-19 11:01:37 +08:00
|
|
|
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
|
|
|
|
import 'package:star_lock/main/lockDetail/lockSet/configuringWifi/configuringWifi/configuringWifiEntity.dart';
|
2024-12-23 15:19:57 +08:00
|
|
|
import 'package:star_lock/talk/startChart/entity/star_chart_register_node_entity.dart';
|
2023-09-07 18:36:16 +08:00
|
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
|
|
2024-03-09 17:17:38 +08:00
|
|
|
import '../../../../../blue/blue_manage.dart';
|
|
|
|
|
import '../../../../../blue/io_protocol/io_configuringWifi.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';
|
|
|
|
|
import '../../../../../network/api_repository.dart';
|
2024-04-22 15:38:19 +08:00
|
|
|
import '../../../../../tools/eventBusEventManage.dart';
|
2024-03-09 17:17:38 +08:00
|
|
|
import '../../../../../tools/storage.dart';
|
2023-09-07 18:36:16 +08:00
|
|
|
import 'configuringWifi_state.dart';
|
|
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
class ConfiguringWifiLogic extends BaseGetXController {
|
2023-09-07 18:36:16 +08:00
|
|
|
final ConfiguringWifiState state = ConfiguringWifiState();
|
|
|
|
|
|
|
|
|
|
Future<void> getWifiLockServiceIpAndPort() async {
|
2024-12-23 15:19:57 +08:00
|
|
|
final ConfiguringWifiEntity entity =
|
|
|
|
|
await ApiRepository.to.getWifiLockServiceIpAndPort();
|
|
|
|
|
if (entity.errorCode! == 0) {
|
2023-09-07 18:36:16 +08:00
|
|
|
state.configuringWifiEntity.value = entity;
|
2024-04-22 15:38:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-18 10:38:13 +08:00
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
Future<void> updateNetworkInfo() async {
|
2024-08-19 11:01:37 +08:00
|
|
|
final LoginEntity entity = await ApiRepository.to.updateNetworkInfo(
|
2024-04-22 15:38:19 +08:00
|
|
|
lockId: state.lockSetInfoData.value.lockId!,
|
2024-12-23 15:19:57 +08:00
|
|
|
network: state.wifiNameController.text,
|
2024-04-22 15:38:19 +08:00
|
|
|
);
|
2024-12-23 15:19:57 +08:00
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
|
|
|
showToast('配网成功'.tr, something: () {
|
|
|
|
|
eventBus
|
|
|
|
|
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
2024-04-22 15:38:19 +08:00
|
|
|
Get.close(2);
|
|
|
|
|
});
|
2023-09-07 18:36:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 监听设备返回的数据
|
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
2024-12-23 15:19:57 +08:00
|
|
|
|
2023-09-07 18:36:16 +08:00
|
|
|
void _initReplySubscription() {
|
2024-12-23 15:19:57 +08:00
|
|
|
_replySubscription =
|
|
|
|
|
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
|
2023-11-18 10:38:13 +08:00
|
|
|
// WIFI配网结果
|
2024-12-23 15:19:57 +08:00
|
|
|
if (reply is SenderConfiguringWifiReply) {
|
2023-09-07 18:36:16 +08:00
|
|
|
_replySenderConfiguringWifi(reply);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-18 10:38:13 +08:00
|
|
|
// WIFI配网结果
|
2023-09-07 18:36:16 +08:00
|
|
|
Future<void> _replySenderConfiguringWifi(Reply reply) async {
|
2024-08-19 11:01:37 +08:00
|
|
|
final int status = reply.data[5];
|
2023-09-07 18:36:16 +08:00
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
switch (status) {
|
2023-09-07 18:36:16 +08:00
|
|
|
case 0x00:
|
2024-04-16 17:44:38 +08:00
|
|
|
//成功
|
2024-01-09 18:02:02 +08:00
|
|
|
state.sureBtnState.value = 0;
|
2024-01-18 11:25:56 +08:00
|
|
|
cancelBlueConnetctToastTimer();
|
2024-01-09 18:02:02 +08:00
|
|
|
dismissEasyLoading();
|
2024-04-22 15:38:19 +08:00
|
|
|
updateNetworkInfo();
|
2023-09-07 18:36:16 +08:00
|
|
|
break;
|
|
|
|
|
case 0x06:
|
2024-04-16 17:44:38 +08:00
|
|
|
//无权限
|
2024-12-23 15:19:57 +08:00
|
|
|
final List<String>? privateKey =
|
|
|
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList =
|
|
|
|
|
changeStringListToIntList(privateKey!);
|
2023-11-18 10:38:13 +08:00
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
final List<String>? publicKey =
|
|
|
|
|
await Storage.getStringList(saveBluePublicKey);
|
|
|
|
|
final List<int> publicKeyDataList =
|
|
|
|
|
changeStringListToIntList(publicKey!);
|
2023-11-18 10:38:13 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<int> tokenData = reply.data.sublist(7, 10);
|
|
|
|
|
final List<String> saveStrList = changeIntListToStringList(tokenData);
|
2023-11-18 10:38:13 +08:00
|
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<int> serversList = <int>[];
|
2024-12-23 15:19:57 +08:00
|
|
|
for (int i = 0;
|
|
|
|
|
i < state.configuringWifiEntity.value.data!.serviceList!.length;
|
|
|
|
|
i++) {
|
|
|
|
|
final ServiceList item =
|
|
|
|
|
state.configuringWifiEntity.value.data!.serviceList![i];
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<String> itemList = item.serviceIp!.split('.');
|
|
|
|
|
for (String element in itemList) {
|
2023-11-18 10:38:13 +08:00
|
|
|
serversList.add(int.parse(element));
|
2024-04-16 17:44:38 +08:00
|
|
|
}
|
2023-11-18 10:38:13 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final double typeDouble = int.parse(item.port!) / 256;
|
|
|
|
|
final int type1 = typeDouble.toInt();
|
|
|
|
|
final int type2 = int.parse(item.port!) % 256;
|
2023-11-18 10:38:13 +08:00
|
|
|
serversList.add(type1);
|
|
|
|
|
serversList.add(type2);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 09:14:29 +08:00
|
|
|
// 判断是否登录账户
|
|
|
|
|
final loginData = await Storage.getLoginData();
|
|
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
// 获取app用户的peerId
|
2025-01-08 09:14:29 +08:00
|
|
|
String appPeerId = loginData?.starchart?.starchartId ?? '';
|
2024-12-23 15:19:57 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<String> uidList = <String>[Storage.getUid().toString()];
|
2023-11-18 10:38:13 +08:00
|
|
|
IoSenderManage.senderConfiguringWifiCommand(
|
|
|
|
|
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
|
2023-09-07 18:36:16 +08:00
|
|
|
userID: await Storage.getUid(),
|
2023-11-18 10:38:13 +08:00
|
|
|
ssid: state.wifiNameController.text,
|
|
|
|
|
password: state.wifiPWDController.text,
|
|
|
|
|
numberOfServers: state.configuringWifiEntity.value.data!.serviceNum,
|
|
|
|
|
listOfServers: serversList,
|
2024-01-16 15:13:43 +08:00
|
|
|
numberOfPhone: uidList.length,
|
|
|
|
|
listOfPhone: uidList,
|
2023-09-07 18:36:16 +08:00
|
|
|
token: tokenData,
|
|
|
|
|
needAuthor: 1,
|
2023-11-18 10:38:13 +08:00
|
|
|
publicKey: publicKeyDataList,
|
2023-09-07 18:36:16 +08:00
|
|
|
privateKey: getPrivateKeyList,
|
2024-12-23 15:19:57 +08:00
|
|
|
peerId: appPeerId,
|
2023-09-07 18:36:16 +08:00
|
|
|
);
|
|
|
|
|
|
2024-01-03 15:33:53 +08:00
|
|
|
break;
|
|
|
|
|
case 0xff:
|
|
|
|
|
//成功
|
2024-08-21 14:12:15 +08:00
|
|
|
showToast('配网失败'.tr);
|
2023-09-07 18:36:16 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
2024-04-16 17:44:38 +08:00
|
|
|
//失败
|
2023-09-07 18:36:16 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 点击配置wifi
|
|
|
|
|
Future<void> senderConfiguringWifiAction() async {
|
2024-12-23 15:19:57 +08:00
|
|
|
if (state.wifiNameController.text.isEmpty) {
|
2024-08-21 14:12:15 +08:00
|
|
|
showToast('请输入wifi名称'.tr);
|
2024-04-22 15:38:19 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
if (state.sureBtnState.value == 1) {
|
2024-01-09 18:02:02 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
state.sureBtnState.value = 1;
|
|
|
|
|
|
2024-01-02 18:03:50 +08:00
|
|
|
showEasyLoading();
|
2024-12-23 15:19:57 +08:00
|
|
|
showBlueConnetctToastTimer(action: () {
|
2024-01-09 18:02:02 +08:00
|
|
|
dismissEasyLoading();
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
});
|
2024-12-23 15:19:57 +08:00
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
|
|
|
|
(BluetoothConnectionState connectionState) async {
|
|
|
|
|
if (connectionState == BluetoothConnectionState.connected) {
|
|
|
|
|
final List<String>? privateKey =
|
|
|
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList =
|
|
|
|
|
changeStringListToIntList(privateKey!);
|
2023-09-07 18:36:16 +08:00
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
final List<String>? publicKey =
|
|
|
|
|
await Storage.getStringList(saveBluePublicKey);
|
|
|
|
|
final List<int> publicKeyDataList =
|
|
|
|
|
changeStringListToIntList(publicKey!);
|
2023-09-07 18:36:16 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
2023-09-07 18:36:16 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<int> serversList = <int>[];
|
2024-12-23 15:19:57 +08:00
|
|
|
for (int i = 0;
|
|
|
|
|
i < state.configuringWifiEntity.value.data!.serviceList!.length;
|
|
|
|
|
i++) {
|
|
|
|
|
final ServiceList item =
|
|
|
|
|
state.configuringWifiEntity.value.data!.serviceList![i];
|
|
|
|
|
if (item.serviceIp!.contains('192')) {
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<String> itemList = item.serviceIp!.split('.');
|
|
|
|
|
for (String element in itemList) {
|
2023-12-11 13:44:15 +08:00
|
|
|
serversList.add(int.parse(element));
|
|
|
|
|
}
|
2024-12-23 15:19:57 +08:00
|
|
|
} else {
|
|
|
|
|
final List<InternetAddress> addresses =
|
|
|
|
|
await InternetAddress.lookup(item.serviceIp!);
|
2024-08-19 11:01:37 +08:00
|
|
|
final List<String> itemList = addresses.first.address.split('.');
|
|
|
|
|
for (String element in itemList) {
|
2023-12-11 13:44:15 +08:00
|
|
|
serversList.add(int.parse(element));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-03 15:33:53 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final double typeDouble = int.parse(item.port!) / 256;
|
|
|
|
|
final int type1 = typeDouble.toInt();
|
|
|
|
|
final int type2 = int.parse(item.port!) % 256;
|
2024-01-03 15:33:53 +08:00
|
|
|
serversList.add(type1);
|
|
|
|
|
serversList.add(type2);
|
2023-12-11 13:44:15 +08:00
|
|
|
}
|
2023-11-18 10:38:13 +08:00
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final String? uidStr = await Storage.getUid();
|
|
|
|
|
final List<String> uidList = <String>[uidStr.toString()];
|
2024-12-23 15:19:57 +08:00
|
|
|
|
2025-01-08 09:14:29 +08:00
|
|
|
// 判断是否登录账户
|
|
|
|
|
final loginData = await Storage.getLoginData();
|
|
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
// 获取app用户的peerId
|
2025-01-08 09:14:29 +08:00
|
|
|
String appPeerId = loginData?.starchart?.starchartId ?? '';
|
2023-09-07 18:36:16 +08:00
|
|
|
IoSenderManage.senderConfiguringWifiCommand(
|
2023-11-18 10:38:13 +08:00
|
|
|
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
|
2023-09-07 18:36:16 +08:00
|
|
|
userID: await Storage.getUid(),
|
|
|
|
|
ssid: state.wifiNameController.text,
|
|
|
|
|
password: state.wifiPWDController.text,
|
2024-12-23 15:19:57 +08:00
|
|
|
peerId: appPeerId,
|
2023-09-07 18:36:16 +08:00
|
|
|
numberOfServers: state.configuringWifiEntity.value.data!.serviceNum,
|
2023-11-18 10:38:13 +08:00
|
|
|
listOfServers: serversList,
|
2024-01-16 15:13:43 +08:00
|
|
|
numberOfPhone: uidList.length,
|
|
|
|
|
listOfPhone: uidList,
|
2023-09-07 18:36:16 +08:00
|
|
|
token: getTokenList,
|
|
|
|
|
needAuthor: 1,
|
|
|
|
|
publicKey: publicKeyDataList,
|
|
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
);
|
2024-03-18 16:16:51 +08:00
|
|
|
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
2024-01-03 15:33:53 +08:00
|
|
|
dismissEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
state.sureBtnState.value = 0;
|
2024-12-23 15:19:57 +08:00
|
|
|
if (state.ifCurrentScreen.value == true) {
|
2024-01-09 18:02:02 +08:00
|
|
|
showBlueConnetctToast();
|
|
|
|
|
}
|
2023-09-07 18:36:16 +08:00
|
|
|
}
|
2024-01-02 18:03:50 +08:00
|
|
|
});
|
2023-09-07 18:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:01:37 +08:00
|
|
|
final NetworkInfo _networkInfo = NetworkInfo();
|
2024-12-23 15:19:57 +08:00
|
|
|
|
2023-09-07 18:36:16 +08:00
|
|
|
Future<String> getWifiName() async {
|
2024-08-19 11:01:37 +08:00
|
|
|
String ssid = '';
|
2023-09-07 18:36:16 +08:00
|
|
|
ssid = (await _networkInfo.getWifiName())!;
|
|
|
|
|
ssid = ssid ?? '';
|
|
|
|
|
ssid = ssid.replaceAll(r'"', '');
|
|
|
|
|
return ssid ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///定位权限
|
|
|
|
|
Future<bool> checkLocationPermission() async {
|
2024-08-19 11:01:37 +08:00
|
|
|
final PermissionStatus value = await locationPermission();
|
2024-12-23 15:19:57 +08:00
|
|
|
final bool allow = value != PermissionStatus.permanentlyDenied &&
|
|
|
|
|
value != PermissionStatus.denied;
|
2023-09-07 18:36:16 +08:00
|
|
|
return allow;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
Future<PermissionStatus> locationPermission() async =>
|
|
|
|
|
Permission.location.request();
|
2023-09-07 18:36:16 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onReady() {
|
|
|
|
|
super.onReady();
|
|
|
|
|
|
2024-12-23 15:19:57 +08:00
|
|
|
if (state.wifiName.value.isEmpty) {
|
2024-08-19 11:01:37 +08:00
|
|
|
getWifiName().then((String value) {
|
2024-03-09 17:17:38 +08:00
|
|
|
state.wifiNameController.text = value;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-09-07 18:36:16 +08:00
|
|
|
|
|
|
|
|
getWifiLockServiceIpAndPort();
|
|
|
|
|
_initReplySubscription();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onInit() {
|
|
|
|
|
super.onInit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onClose() {
|
2024-01-09 18:02:02 +08:00
|
|
|
_replySubscription.cancel();
|
|
|
|
|
super.onClose();
|
2023-09-07 18:36:16 +08:00
|
|
|
}
|
2024-01-23 17:36:02 +08:00
|
|
|
}
|