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
|
|
|
|
|
|
|
|
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
|
|
|
|
import 'package:network_info_plus/network_info_plus.dart';
|
|
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
import '../../../../tools/storage.dart';
|
|
|
|
|
|
import 'configuringWifi_state.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class ConfiguringWifiLogic extends BaseGetXController{
|
|
|
|
|
|
final ConfiguringWifiState state = ConfiguringWifiState();
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> getWifiLockServiceIpAndPort() async {
|
|
|
|
|
|
var entity = await ApiRepository.to.getWifiLockServiceIpAndPort();
|
|
|
|
|
|
if(entity.errorCode! == 0){
|
|
|
|
|
|
state.configuringWifiEntity.value = entity;
|
2023-11-18 10:38:13 +08:00
|
|
|
|
|
|
|
|
|
|
// var serversList = <int>[];
|
|
|
|
|
|
// for(int i = 0; i<state.configuringWifiEntity.value.data!.serviceList!.length; i++){
|
|
|
|
|
|
// var item = state.configuringWifiEntity.value.data!.serviceList![i];
|
|
|
|
|
|
// var itemList = item.serviceIp!.split(".");
|
|
|
|
|
|
// itemList.forEach((element) {
|
|
|
|
|
|
// serversList.add(int.parse(element));
|
|
|
|
|
|
// });
|
|
|
|
|
|
// // serversList.add(int.parse(item.port!));
|
|
|
|
|
|
//
|
|
|
|
|
|
// double typeDouble = int.parse(item.port!) / 256;
|
|
|
|
|
|
// int type1 = typeDouble.toInt();
|
|
|
|
|
|
// int type2 = int.parse(item.port!) % 256;
|
|
|
|
|
|
// serversList.add(type1);
|
|
|
|
|
|
// serversList.add(type2);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// print("serversListserversList:$serversList");
|
2023-09-07 18:36:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 监听设备返回的数据
|
|
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
|
|
|
|
void _initReplySubscription() {
|
|
|
|
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) async {
|
2023-11-18 10:38:13 +08:00
|
|
|
|
// WIFI配网结果
|
2023-09-07 18:36:16 +08:00
|
|
|
|
if(reply is SenderConfiguringWifiReply) {
|
|
|
|
|
|
_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 {
|
2023-12-11 13:44:15 +08:00
|
|
|
|
int status = reply.data[5];
|
2023-09-07 18:36:16 +08:00
|
|
|
|
print("status:$status");
|
|
|
|
|
|
|
|
|
|
|
|
switch(status){
|
|
|
|
|
|
case 0x00:
|
|
|
|
|
|
//成功
|
|
|
|
|
|
print("${reply.commandType}数据解析成功");
|
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-01-16 16:52:05 +08:00
|
|
|
|
showToast("配网成功");
|
2023-09-07 18:36:16 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 0x06:
|
|
|
|
|
|
//无权限
|
|
|
|
|
|
print("${reply.commandType}需要鉴权");
|
|
|
|
|
|
|
2023-11-18 10:38:13 +08:00
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
|
|
|
|
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
|
|
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
|
|
|
|
|
|
|
|
var tokenData = reply.data.sublist(7, 10);
|
|
|
|
|
|
var saveStrList = changeIntListToStringList(tokenData);
|
|
|
|
|
|
print("openDoorToken:$tokenData");
|
|
|
|
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
|
|
|
|
|
|
|
|
var serversList = <int>[];
|
|
|
|
|
|
for(int i = 0; i<state.configuringWifiEntity.value.data!.serviceList!.length; i++){
|
|
|
|
|
|
var item = state.configuringWifiEntity.value.data!.serviceList![i];
|
|
|
|
|
|
var itemList = item.serviceIp!.split(".");
|
|
|
|
|
|
itemList.forEach((element) {
|
|
|
|
|
|
serversList.add(int.parse(element));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
double typeDouble = int.parse(item.port!) / 256;
|
|
|
|
|
|
int type1 = typeDouble.toInt();
|
|
|
|
|
|
int type2 = int.parse(item.port!) % 256;
|
|
|
|
|
|
serversList.add(type1);
|
|
|
|
|
|
serversList.add(type2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-16 15:13:43 +08:00
|
|
|
|
var uidList = [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,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x07:
|
|
|
|
|
|
//无权限
|
|
|
|
|
|
print("${reply.commandType}用户无权限");
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x09:
|
|
|
|
|
|
// 权限校验错误
|
|
|
|
|
|
print("${reply.commandType}校验错误");
|
|
|
|
|
|
|
2024-01-03 15:33:53 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 0xff:
|
|
|
|
|
|
//成功
|
2024-01-16 16:52:05 +08:00
|
|
|
|
showToast("配网失败");
|
2023-09-07 18:36:16 +08:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
//失败
|
|
|
|
|
|
print("${reply.commandType}失败");
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 点击配置wifi
|
|
|
|
|
|
Future<void> senderConfiguringWifiAction() async {
|
2024-01-09 18:02:02 +08:00
|
|
|
|
if(state.sureBtnState.value == 1){
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
state.sureBtnState.value = 1;
|
|
|
|
|
|
|
2024-01-02 18:03:50 +08:00
|
|
|
|
showEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
|
showBlueConnetctToastTimer(action: (){
|
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
|
});
|
2024-01-04 18:27:50 +08:00
|
|
|
|
BlueManage().bludSendData(BlueManage().connectDeviceName, (DeviceConnectionState connectionState) async {
|
2023-09-07 18:36:16 +08:00
|
|
|
|
if (connectionState == 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 = changeStringListToIntList(token!);
|
|
|
|
|
|
print("openDoorTokenPubToken:$getTokenList");
|
|
|
|
|
|
|
2023-11-01 17:28:59 +08:00
|
|
|
|
// print("millisecondsSinceEpoch/1000:${DateTime.now().millisecondsSinceEpoch~/1000}");
|
2023-11-18 10:38:13 +08:00
|
|
|
|
var serversList = <int>[];
|
|
|
|
|
|
for(int i = 0; i<state.configuringWifiEntity.value.data!.serviceList!.length; i++){
|
|
|
|
|
|
var item = state.configuringWifiEntity.value.data!.serviceList![i];
|
2023-12-11 13:44:15 +08:00
|
|
|
|
if(item.serviceIp!.contains("192")){
|
|
|
|
|
|
var itemList = item.serviceIp!.split(".");
|
|
|
|
|
|
for (var element in itemList) {
|
|
|
|
|
|
serversList.add(int.parse(element));
|
|
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
|
|
List<InternetAddress> addresses = await InternetAddress.lookup(item.serviceIp!);
|
|
|
|
|
|
var itemList = addresses.first.address.split(".");
|
|
|
|
|
|
for (var element in itemList) {
|
|
|
|
|
|
serversList.add(int.parse(element));
|
|
|
|
|
|
}
|
2024-01-19 11:09:46 +08:00
|
|
|
|
print('Resolved google.com to address: ${addresses.first.address} serversList:$serversList');
|
2023-12-11 13:44:15 +08:00
|
|
|
|
}
|
2024-01-03 15:33:53 +08:00
|
|
|
|
|
|
|
|
|
|
double typeDouble = int.parse(item.port!) / 256;
|
|
|
|
|
|
int type1 = typeDouble.toInt();
|
|
|
|
|
|
int type2 = int.parse(item.port!) % 256;
|
|
|
|
|
|
serversList.add(type1);
|
|
|
|
|
|
serversList.add(type2);
|
2023-12-11 13:44:15 +08:00
|
|
|
|
}
|
2023-11-18 10:38:13 +08:00
|
|
|
|
|
2024-01-19 11:09:46 +08:00
|
|
|
|
var uidStr = await Storage.getUid();
|
|
|
|
|
|
var uidList = [uidStr.toString()];
|
|
|
|
|
|
// Get.log("uidList:$uidList await Storage.getUid()${await Storage.getUid()}");
|
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,
|
|
|
|
|
|
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-01-03 15:33:53 +08:00
|
|
|
|
} else if (connectionState == DeviceConnectionState.disconnected) {
|
|
|
|
|
|
dismissEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
|
if(state.ifCurrentScreen.value == true){
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final _networkInfo = NetworkInfo();
|
|
|
|
|
|
Future<String> getWifiName() async {
|
|
|
|
|
|
// if(!(await checkLocationPermission())) {
|
|
|
|
|
|
// print("未获取定位权限");
|
|
|
|
|
|
// return '';
|
|
|
|
|
|
// }
|
|
|
|
|
|
var ssid = '';
|
|
|
|
|
|
ssid = (await _networkInfo.getWifiName())!;
|
|
|
|
|
|
ssid = ssid ?? '';
|
|
|
|
|
|
ssid = ssid.replaceAll(r'"', '');
|
|
|
|
|
|
print("ssid:$ssid");
|
|
|
|
|
|
return ssid ?? '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///定位权限
|
|
|
|
|
|
Future<bool> checkLocationPermission() async {
|
|
|
|
|
|
PermissionStatus value = await locationPermission();
|
|
|
|
|
|
bool allow = value != PermissionStatus.permanentlyDenied && value != PermissionStatus.denied;
|
|
|
|
|
|
return allow;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<PermissionStatus> locationPermission() async => Permission.location.request();
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void onReady() {
|
|
|
|
|
|
// TODO: implement onReady
|
|
|
|
|
|
super.onReady();
|
|
|
|
|
|
|
|
|
|
|
|
getWifiName().then((value) {
|
|
|
|
|
|
print("wifiName:$value");
|
|
|
|
|
|
state.wifiNameController.text = value;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
getWifiLockServiceIpAndPort();
|
|
|
|
|
|
_initReplySubscription();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void onInit() {
|
|
|
|
|
|
// TODO: implement onInit
|
|
|
|
|
|
super.onInit();
|
2024-01-16 15:13:43 +08:00
|
|
|
|
|
2023-09-07 18:36:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void onClose() {
|
|
|
|
|
|
// TODO: implement 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
|
|
|
|
}
|