245 lines
8.5 KiB
Dart
Executable File

import 'dart:async';
import 'dart:io';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:star_lock/login/login/entity/LoginEntity.dart';
import 'package:star_lock/main/lockDetail/lockSet/configuringWifi/configuringWifi/configuringWifiEntity.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/eventBusEventManage.dart';
import '../../../../../tools/storage.dart';
import 'configuringWifi_state.dart';
class ConfiguringWifiLogic extends BaseGetXController{
final ConfiguringWifiState state = ConfiguringWifiState();
Future<void> getWifiLockServiceIpAndPort() async {
final ConfiguringWifiEntity entity = await ApiRepository.to.getWifiLockServiceIpAndPort();
if(entity.errorCode! == 0){
state.configuringWifiEntity.value = entity;
}
}
Future<void> updateNetworkInfo() async{
final LoginEntity entity = await ApiRepository.to.updateNetworkInfo(
lockId: state.lockSetInfoData.value.lockId!,
network:state.wifiNameController.text,
);
if(entity.errorCode!.codeIsSuccessful){
showToast('配网成功'.tr, something:(){
eventBus.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
Get.close(2);
});
}
}
// 监听设备返回的数据
late StreamSubscription<Reply> _replySubscription;
void _initReplySubscription() {
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
// WIFI配网结果
if(reply is SenderConfiguringWifiReply) {
_replySenderConfiguringWifi(reply);
}
});
}
// WIFI配网结果
Future<void> _replySenderConfiguringWifi(Reply reply) async {
final int status = reply.data[5];
switch(status){
case 0x00:
//成功
state.sureBtnState.value = 0;
cancelBlueConnetctToastTimer();
dismissEasyLoading();
updateNetworkInfo();
break;
case 0x06:
//无权限
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
final List<int> tokenData = reply.data.sublist(7, 10);
final List<String> saveStrList = changeIntListToStringList(tokenData);
Storage.setStringList(saveBlueToken, saveStrList);
final List<int> serversList = <int>[];
for(int i = 0; i<state.configuringWifiEntity.value.data!.serviceList!.length; i++){
final ServiceList item = state.configuringWifiEntity.value.data!.serviceList![i];
final List<String> itemList = item.serviceIp!.split('.');
for (String element in itemList) {
serversList.add(int.parse(element));
}
final double typeDouble = int.parse(item.port!) / 256;
final int type1 = typeDouble.toInt();
final int type2 = int.parse(item.port!) % 256;
serversList.add(type1);
serversList.add(type2);
}
final List<String> uidList = <String>[Storage.getUid().toString()];
IoSenderManage.senderConfiguringWifiCommand(
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
userID: await Storage.getUid(),
ssid: state.wifiNameController.text,
password: state.wifiPWDController.text,
numberOfServers: state.configuringWifiEntity.value.data!.serviceNum,
listOfServers: serversList,
numberOfPhone: uidList.length,
listOfPhone: uidList,
token: tokenData,
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
);
break;
case 0xff:
//成功
showToast('配网失败'.tr);
break;
default:
//失败
break;
}
}
// 点击配置wifi
Future<void> senderConfiguringWifiAction() async {
if(state.wifiNameController.text.isEmpty){
showToast('请输入wifi名称'.tr);
return;
}
if(state.sureBtnState.value == 1){
return;
}
state.sureBtnState.value = 1;
showEasyLoading();
showBlueConnetctToastTimer(action: (){
dismissEasyLoading();
state.sureBtnState.value = 0;
});
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!);
final List<String>? publicKey = await Storage.getStringList(saveBluePublicKey);
final List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
final List<String>? token = await Storage.getStringList(saveBlueToken);
final List<int> getTokenList = changeStringListToIntList(token!);
final List<int> serversList = <int>[];
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')){
final List<String> itemList = item.serviceIp!.split('.');
for (String element in itemList) {
serversList.add(int.parse(element));
}
}else{
final List<InternetAddress> addresses = await InternetAddress.lookup(item.serviceIp!);
final List<String> itemList = addresses.first.address.split('.');
for (String element in itemList) {
serversList.add(int.parse(element));
}
}
final double typeDouble = int.parse(item.port!) / 256;
final int type1 = typeDouble.toInt();
final int type2 = int.parse(item.port!) % 256;
serversList.add(type1);
serversList.add(type2);
}
final String? uidStr = await Storage.getUid();
final List<String> uidList = <String>[uidStr.toString()];
IoSenderManage.senderConfiguringWifiCommand(
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
userID: await Storage.getUid(),
ssid: state.wifiNameController.text,
password: state.wifiPWDController.text,
numberOfServers: state.configuringWifiEntity.value.data!.serviceNum,
listOfServers: serversList,
numberOfPhone: uidList.length,
listOfPhone: uidList,
token: getTokenList,
needAuthor: 1,
publicKey: publicKeyDataList,
privateKey: getPrivateKeyList,
);
} else if (connectionState == BluetoothConnectionState.disconnected) {
dismissEasyLoading();
cancelBlueConnetctToastTimer();
state.sureBtnState.value = 0;
if(state.ifCurrentScreen.value == true){
showBlueConnetctToast();
}
}
});
}
final NetworkInfo _networkInfo = NetworkInfo();
Future<String> getWifiName() async {
String ssid = '';
ssid = (await _networkInfo.getWifiName())!;
ssid = ssid ?? '';
ssid = ssid.replaceAll(r'"', '');
return ssid ?? '';
}
///定位权限
Future<bool> checkLocationPermission() async {
final PermissionStatus value = await locationPermission();
final bool allow = value != PermissionStatus.permanentlyDenied && value != PermissionStatus.denied;
return allow;
}
Future<PermissionStatus> locationPermission() async => Permission.location.request();
@override
void onReady() {
super.onReady();
if(state.wifiName.value.isEmpty){
getWifiName().then((String value) {
state.wifiNameController.text = value;
});
}
getWifiLockServiceIpAndPort();
_initReplySubscription();
}
@override
void onInit() {
super.onInit();
}
@override
void onClose() {
_replySubscription.cancel();
super.onClose();
}
}