198 lines
5.9 KiB
Dart
198 lines
5.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
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:star_lock/app_settings/app_settings.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
import '../../../../blue/blue_manage.dart';
|
|
import '../../../../blue/io_gateway/io_gateway_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 '../../../../login/login/entity/LoginEntity.dart';
|
|
import '../../../../network/api_repository.dart';
|
|
import 'gatewayConfigurationWifi_state.dart';
|
|
import 'getGatewayConfiguration_entity.dart';
|
|
|
|
class GatewayConfigurationWifiLogic extends BaseGetXController {
|
|
final GatewayConfigurationWifiState state = GatewayConfigurationWifiState();
|
|
|
|
Future<void> gatewayDistributionNetwork() async {
|
|
final LoginEntity entity = await ApiRepository.to
|
|
.gatewayDistributionNetwork(
|
|
gatewayName: state.gatewayNameTF.text,
|
|
gatewayMac: state.gatewayModel.mac,
|
|
serialNumber: state.gatewayModel.serialNum,
|
|
gatewayType: 2,
|
|
networkName: state.wifiNameTF.text,
|
|
networkMac: state.gatewayModel.wifiMac,
|
|
version: state.gatewayModel.gatewayVersion,
|
|
gatewayJson: jsonDecode(state.gatewayJson));
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
showToast('配网成功'.tr, something: () {
|
|
// eventBus.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
|
Get.close(4);
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> getGatewayConfiguration() async {
|
|
final GetGatewayConfigurationEntity entity =
|
|
await ApiRepository.to.getGatewayConfiguration();
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.getGatewayConfigurationStr = entity.data ?? '';
|
|
// AppLog.log('state.getGatewayConfigurationStr:${state.getGatewayConfigurationStr}');
|
|
}
|
|
}
|
|
|
|
// 监听设备返回的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription =
|
|
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
|
|
// WIFI配网
|
|
// if(reply is GatewayConfiguringWifiReply) {
|
|
// _replySenderConfiguringWifi(reply);
|
|
// }
|
|
|
|
// WIFI配网结果
|
|
if (reply is GatewayConfiguringWifiResultReply) {
|
|
_replySenderConfiguringWifiResult(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
// WIFI配网
|
|
// Future<void> _replySenderConfiguringWifi(Reply reply) async {
|
|
// final int status = reply.data[2];
|
|
//
|
|
// switch(status){
|
|
// case 0x00:
|
|
// //成功
|
|
// state.sureBtnState.value = 0;
|
|
// cancelBlueConnetctToastTimer();
|
|
// dismissEasyLoading();
|
|
// gatewayDistributionNetwork();
|
|
// break;
|
|
// default:
|
|
// //失败
|
|
// dismissEasyLoading();
|
|
// break;
|
|
// }
|
|
// }
|
|
|
|
// WIFI配网结果
|
|
Future<void> _replySenderConfiguringWifiResult(Reply reply) async {
|
|
final int status = reply.data[2];
|
|
state.sureBtnState.value = 0;
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
cancelBlueConnetctToastTimer();
|
|
dismissEasyLoading();
|
|
final int secretKeyJsonLength = (reply.data[3] << 8) + reply.data[4];
|
|
final List<int> secretKeyList =
|
|
reply.data.sublist(5, 5 + secretKeyJsonLength);
|
|
state.gatewayJson = utf8String(secretKeyList);
|
|
|
|
gatewayDistributionNetwork();
|
|
break;
|
|
default:
|
|
//失败
|
|
dismissEasyLoading();
|
|
cancelBlueConnetctToastTimer();
|
|
showToast('配网失败'.tr);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 点击配置wifi
|
|
Future<void> senderConfiguringWifiAction() async {
|
|
AppLog.log(
|
|
'state.getGatewayConfigurationStr:${state.getGatewayConfigurationStr}');
|
|
if (state.wifiNameTF.text.isEmpty) {
|
|
showToast('请输入wifi名称'.tr);
|
|
return;
|
|
}
|
|
|
|
if (state.wifiPasswardTF.text.isEmpty) {
|
|
showToast('请输入WiFi密码'.tr);
|
|
return;
|
|
}
|
|
|
|
if (state.gatewayNameTF.text.isEmpty) {
|
|
showToast('请输入网关名称'.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) {
|
|
IoSenderManage.gatewayConfiguringWifiCommand(
|
|
ssid: state.wifiNameTF.text,
|
|
password: state.wifiPasswardTF.text,
|
|
gatewayConfigurationStr: state.getGatewayConfigurationStr,
|
|
);
|
|
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
|
dismissEasyLoading();
|
|
cancelBlueConnetctToastTimer();
|
|
state.sureBtnState.value = 0;
|
|
if (state.ifCurrentScreen.value == true) {
|
|
showBlueConnetctToast();
|
|
}
|
|
}
|
|
}, isAddEquipment: true);
|
|
}
|
|
|
|
final NetworkInfo _networkInfo = NetworkInfo();
|
|
Future<String> getWifiName() async {
|
|
String ssid = '';
|
|
ssid = (await _networkInfo.getWifiName())!;
|
|
ssid = ssid ?? '';
|
|
ssid = ssid.replaceAll(r'"', '');
|
|
return ssid ?? '';
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
super.onReady();
|
|
|
|
if (state.wifiNameTF.text.isEmpty) {
|
|
getWifiName().then((String value) {
|
|
state.wifiNameTF.text = value;
|
|
// update();
|
|
// AppLog.log('wifiNameTF:${state.wifiNameTF.text} value:$value');
|
|
});
|
|
}
|
|
|
|
getGatewayConfiguration();
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
_replySubscription.cancel();
|
|
super.onClose();
|
|
}
|
|
}
|