fix:梳理配网逻辑

This commit is contained in:
liyi 2025-04-26 15:15:27 +08:00
parent 1784f75c47
commit 2a89d0110c
5 changed files with 626 additions and 255 deletions

View File

@ -37,15 +37,24 @@ import 'configuringWifi_state.dart';
class ConfiguringWifiLogic extends BaseGetXController {
final ConfiguringWifiState state = ConfiguringWifiState();
final int _configurationTimeout = 60; //
/// WiFi锁服务IP和端口
Future<void> getWifiLockServiceIpAndPort() async {
final ConfiguringWifiEntity entity =
await ApiRepository.to.getWifiLockServiceIpAndPort();
if (entity.errorCode! == 0) {
state.configuringWifiEntity.value = entity;
try {
final ConfiguringWifiEntity entity =
await ApiRepository.to.getWifiLockServiceIpAndPort();
if (entity.errorCode! == 0) {
state.configuringWifiEntity.value = entity;
} else {
AppLog.log('获取WiFi锁服务IP和端口失败${entity.errorCode}');
}
} catch (e) {
AppLog.log('获取WiFi锁服务IP和端口异常$e');
}
}
///
void updateNetworkInfo({
required String peerId,
required String wifiName,
@ -53,24 +62,36 @@ class ConfiguringWifiLogic extends BaseGetXController {
required String deviceMac,
required String networkMac,
}) async {
final LoginEntity entity = await ApiRepository.to.settingDeviceNetwork(
deviceType: 2,
deviceMac: deviceMac,
wifiName: wifiName,
networkMac: networkMac,
secretKey: secretKey,
peerId: peerId,
);
if (entity.errorCode!.codeIsSuccessful) {
// peerID
StartChartManage().lockNetworkInfo = DeviceNetworkInfo(
try {
final LoginEntity entity = await ApiRepository.to.settingDeviceNetwork(
deviceType: 2,
deviceMac: deviceMac,
wifiName: wifiName,
networkMac: networkMac,
secretKey: secretKey,
peerId: peerId,
);
await _getUploadLockSet();
if (entity.errorCode!.codeIsSuccessful) {
// peerID
StartChartManage().lockNetworkInfo = DeviceNetworkInfo(
wifiName: wifiName,
networkMac: networkMac,
secretKey: secretKey,
peerId: peerId,
);
await _getUploadLockSet();
} else {
dismissEasyLoading();
showToast('网络配置失败,请重试'.tr);
state.sureBtnState.value = 0;
}
} catch (e) {
dismissEasyLoading();
showToast('网络配置异常:${e.toString()}'.tr);
state.sureBtnState.value = 0;
AppLog.log('网络配置异常:$e');
}
}
@ -84,81 +105,138 @@ class ConfiguringWifiLogic extends BaseGetXController {
if (reply is GatewayConfiguringWifiResultReply) {
_replySenderConfiguringWifiResult(reply);
}
// wifi配网命令应答结果
if (reply is GatewayConfiguringWifiReply) {
_replySenderConfiguringWifiResult(reply);
_replySenderConfiguringWifi(reply);
}
if (reply is GatewayGetStatusReply) {
_replyGatewayGetStatusReply(reply);
}
// if (reply is GatewayGetStatusReply) {
// _replyStatusInfo(reply);
// }
//
if (reply is UpdataLockSetReply) {
_replyUpdataLockSetReply(reply);
}
AppLog.log('蓝牙回调处理完毕${EasyLoading.isShow}');
});
}
// WIFI配网结果
Future<void> _replySenderConfiguringWifiResult(Reply reply) async {
// WIFI配网操作结果处理
Future<void> _replySenderConfiguringWifi(Reply reply) async {
final int status = reply.data[2];
// state.sureBtnState.value = 0;
// loading超时定时器
state.loadingTimer?.cancel();
state.loadingTimer = null;
switch (status) {
case 0x00:
await Storage.removeLockNetWorkInfoCache();
final int secretKeyJsonLength = (reply.data[4] << 8) + reply.data[3];
final List<int> secretKeyList =
reply.data.sublist(5, 5 + secretKeyJsonLength);
String result = utf8String(secretKeyList);
// JSON Map
Map<String, dynamic> jsonMap = json.decode(result);
// peerId
String? peerId = jsonMap['peerId'];
String? wifiName = jsonMap['wifiName'];
String? secretKey = jsonMap['secretKey'];
String? deviceMac = jsonMap['deviceMac'];
String? networkMac = jsonMap['networkMac'];
/// ,peerId
StartChartManage().lockPeerId = peerId ?? '';
state.isLoading.value = false;
//
await Storage.saveLockNetWorkInfo(jsonMap);
//
updateNetworkInfo(
peerId: peerId ?? '',
wifiName: wifiName ?? '',
secretKey: secretKey ?? '',
deviceMac: deviceMac ?? '',
networkMac: networkMac ?? '');
AppLog.log('wifi配网命令回复结果:成功');
break;
default:
//
dismissEasyLoading(); // loading
cancelBlueConnetctToastTimer();
if (state.loadingTimer != null) {
state.loadingTimer!.cancel();
state.loadingTimer = null;
}
showToast('配网失败'.tr);
state.isLoading.value = false;
break;
}
}
// JSON
// WIFI配网结果处理
Future<void> _replySenderConfiguringWifiResult(Reply reply) async {
final int status = reply.data[2];
//
cancelBlueConnetctToastTimer();
switch (status) {
case 0x00:
// - loading
await Storage.removeLockNetWorkInfoCache();
try {
final int secretKeyJsonLength = (reply.data[4] << 8) + reply.data[3];
final List<int> secretKeyList =
reply.data.sublist(5, 5 + secretKeyJsonLength);
String result = utf8String(secretKeyList);
AppLog.log('解析配网信息: $result');
// JSON Map
Map<String, dynamic> jsonMap = json.decode(result);
//
String? peerId = jsonMap['peerId'];
String? wifiName = jsonMap['wifiName'];
String? secretKey = jsonMap['secretKey'];
String? deviceMac = jsonMap['deviceMac'];
String? networkMac = jsonMap['networkMac'];
//
if (peerId == null ||
peerId.isEmpty ||
secretKey == null ||
secretKey.isEmpty) {
throw Exception('Missing required network information');
}
/// ,peerId
StartChartManage().lockPeerId = peerId;
//
await Storage.saveLockNetWorkInfo(jsonMap);
// - : sureBtnState updateNetworkInfo
updateNetworkInfo(
peerId: peerId,
wifiName: wifiName ?? '',
secretKey: secretKey,
deviceMac: deviceMac ?? '',
networkMac: networkMac ?? '');
} catch (e) {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('解析配网信息失败,请重试'.tr);
state.sureBtnState.value = 0; //
AppLog.log('解析配网信息失败: $e');
return; // return阻止后续流程
}
break;
case 0x01:
// WiFi密码错误
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('WiFi密码错误请重新输入'.tr);
state.sureBtnState.value = 0; //
break;
case 0x02:
// WiFi
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('找不到该WiFi网络请确认WiFi名称正确'.tr);
state.sureBtnState.value = 0; //
break;
case 0x03:
//
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('连接WiFi超时请确保网络信号良好'.tr);
state.sureBtnState.value = 0; //
break;
default:
//
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('配网失败 (错误码: $status),请重试'.tr);
state.sureBtnState.value = 0; //
break;
}
}
// JSON
String prettyPrintJson(String jsonString) {
var jsonObject = json.decode(jsonString);
return JsonEncoder.withIndent(' ').convert(jsonObject);
@ -168,63 +246,80 @@ class ConfiguringWifiLogic extends BaseGetXController {
Future<void> senderConfiguringWifiAction() async {
AppLog.log('开始配网${EasyLoading.isShow}');
if (state.isLoading.isTrue) {
if (state.sureBtnState.value == 1) {
AppLog.log('正在配网中请勿重复点击');
return;
}
if (state.wifiNameController.text.isEmpty) {
showToast('请输入wifi名称'.tr);
//
try {
final GetGatewayConfigurationEntity entity = await ApiRepository.to
.getGatewayConfigurationNotLoading(timeout: _configurationTimeout);
if (entity.errorCode!.codeIsSuccessful) {
state.getGatewayConfigurationStr = entity.data ?? '';
} else {
// showToast('获取网关配置失败,请重试'.tr);
AppLog.log('获取网关配置失败,请重试');
return;
}
//
final loginData = await Storage.getLoginData();
if (loginData == null) {
AppLog.log('未检测到登录信息,请重新登录'.tr);
return;
}
// app用户的peerId
String appPeerId = loginData.starchart?.starchartId ?? '';
if (appPeerId.isEmpty) {
AppLog.log('用户ID获取失败请重新登录'.tr);
return;
}
//
if (state.getGatewayConfigurationStr.isNotEmpty) {
// JSON Map
Map<String, dynamic> jsonMap =
json.decode(state.getGatewayConfigurationStr);
//
jsonMap.remove("starCloudUrl");
jsonMap.remove("starLockPeerId");
//
jsonMap['userPeerld'] = appPeerId;
// Map JSON
state.getGatewayConfigurationStr =
json.encode(jsonMap).replaceAll(',', ',\n');
//
state.getGatewayConfigurationStr =
prettyPrintJson(state.getGatewayConfigurationStr);
} else {
//
state.getGatewayConfigurationStr = "{\"userPeerld\": \"$appPeerId\"}";
}
} catch (e) {
AppLog.log('网关配置准备失败:${e.toString()}'.tr);
return;
}
if (state.wifiPWDController.text.isEmpty) {
showToast('请输入WiFi密码'.tr);
return;
}
// if (state.sureBtnState.value == 1) {
// return;
// }
// state.sureBtnState.value = 1;
// sureBtnState状态
state.sureBtnState.value = 1;
final GetGatewayConfigurationEntity entity =
await ApiRepository.to.getGatewayConfigurationNotLoading(timeout: 60);
if (entity.errorCode!.codeIsSuccessful) {
state.getGatewayConfigurationStr = entity.data ?? '';
// loading
if (!EasyLoading.isShow) {
showEasyLoading();
}
//
final loginData = await Storage.getLoginData();
// app用户的peerId
String appPeerId = loginData?.starchart?.starchartId ?? '';
//
if (state.getGatewayConfigurationStr.isNotEmpty) {
// JSON Map
Map<String, dynamic> jsonMap =
json.decode(state.getGatewayConfigurationStr);
//
jsonMap.remove("starCloudUrl");
jsonMap.remove("starLockPeerId");
//
jsonMap['userPeerld'] = appPeerId;
// Map JSON
state.getGatewayConfigurationStr =
json.encode(jsonMap).replaceAll(',', ',\n');
//
state.getGatewayConfigurationStr =
prettyPrintJson(state.getGatewayConfigurationStr);
} else {
//
state.getGatewayConfigurationStr = "{\"userPeerld\": \"$appPeerId\"}";
}
showEasyLoading();
//
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
state.isLoading.value = false;
if (EasyLoading.isShow) {
dismissEasyLoading();
}
state.sureBtnState.value = 0; //
});
//
@ -232,16 +327,27 @@ class ConfiguringWifiLogic extends BaseGetXController {
BlueManage().connectDeviceName,
(BluetoothConnectionState connectionState) async {
if (connectionState == BluetoothConnectionState.connected) {
IoSenderManage.gatewayConfiguringWifiCommand(
ssid: state.wifiNameController.text,
password: state.wifiPWDController.text,
gatewayConfigurationStr: state.getGatewayConfigurationStr,
);
try {
IoSenderManage.gatewayConfiguringWifiCommand(
ssid: state.wifiNameController.text,
password: state.wifiPWDController.text,
gatewayConfigurationStr: state.getGatewayConfigurationStr,
);
// sureBtnState状态
} catch (e) {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
cancelBlueConnetctToastTimer();
state.sureBtnState.value = 0; //
showToast('发送配网指令失败:${e.toString()}'.tr);
}
} else if (connectionState == BluetoothConnectionState.disconnected) {
dismissEasyLoading();
if (EasyLoading.isShow) {
dismissEasyLoading();
}
cancelBlueConnetctToastTimer();
state.isLoading.value = false;
// state.sureBtnState.value = 0;
state.sureBtnState.value = 0; //
if (state.ifCurrentScreen.value == true) {
showBlueConnetctToast();
}
@ -249,7 +355,6 @@ class ConfiguringWifiLogic extends BaseGetXController {
},
isAddEquipment: false,
);
state.isLoading.value = true;
}
//
@ -278,11 +383,15 @@ class ConfiguringWifiLogic extends BaseGetXController {
final NetworkInfo _networkInfo = NetworkInfo();
Future<String> getWifiName() async {
String ssid = '';
ssid = (await _networkInfo.getWifiName())!;
ssid = ssid ?? '';
ssid = ssid.replaceAll(r'"', '');
return ssid ?? '';
try {
String? ssid = await _networkInfo.getWifiName();
ssid = ssid ?? '';
ssid = ssid.replaceAll(r'"', '');
return ssid;
} catch (e) {
AppLog.log('获取WiFi名称失败: $e');
return '';
}
}
///
@ -308,17 +417,12 @@ class ConfiguringWifiLogic extends BaseGetXController {
getWifiLockServiceIpAndPort();
_initReplySubscription();
// getDevicesStatusAction();
}
@override
void onInit() {
super.onInit();
}
@override
void onClose() {
_replySubscription.cancel();
cancelBlueConnetctToastTimer(); //
super.onClose();
}
@ -330,8 +434,6 @@ class ConfiguringWifiLogic extends BaseGetXController {
switch (status) {
case 0x00:
//
// state.sureBtnState.value = 0;
final GetGatewayInfoModel gatewayModel = GetGatewayInfoModel();
// MAC地址
int index = 3;
@ -372,70 +474,108 @@ class ConfiguringWifiLogic extends BaseGetXController {
default:
//
dismissEasyLoading();
showToast('配网失败'.tr);
if (state.loadingTimer != null) {
state.loadingTimer!.cancel();
state.loadingTimer = null;
}
showToast('获取设备状态失败'.tr);
break;
}
}
//
Future<void> _getUploadLockSet() async {
showEasyLoading();
// loading状态loading
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
if (EasyLoading.isShow) {
dismissEasyLoading();
}
state.sureBtnState.value = 0;
});
final List<String>? token = await Storage.getStringList(saveBlueToken);
final List<int> getTokenList = changeStringListToIntList(token!);
await _uploadLockSet(getTokenList);
try {
final List<String>? token = await Storage.getStringList(saveBlueToken);
if (token == null || token.isEmpty) {
throw Exception('Token is empty');
}
final List<int> getTokenList = changeStringListToIntList(token);
await _uploadLockSet(getTokenList);
} catch (e) {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
cancelBlueConnetctToastTimer();
showToast('获取设置失败:${e.toString()}'.tr);
state.sureBtnState.value = 0;
}
}
//
Future<void> _uploadLockSet(List<int> token) async {
final List<String>? privateKey =
await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
try {
final List<String>? privateKey =
await Storage.getStringList(saveBluePrivateKey);
if (privateKey == null || privateKey.isEmpty) {
throw Exception('Private key is empty');
}
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey);
final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
final List<String>? signKey =
await Storage.getStringList(saveBlueSignKey);
if (signKey == null || signKey.isEmpty) {
throw Exception('Sign key is empty');
}
final List<int> signKeyDataList = changeStringListToIntList(signKey);
IoSenderManage.updataLockSetCommand(
lockID: BlueManage().connectDeviceName,
userID: await Storage.getUid(),
token: token,
needAuthor: 1,
signKey: signKeyDataList,
privateKey: getPrivateKeyList);
IoSenderManage.updataLockSetCommand(
lockID: BlueManage().connectDeviceName,
userID: await Storage.getUid(),
token: token,
needAuthor: 1,
signKey: signKeyDataList,
privateKey: getPrivateKeyList);
} catch (e) {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
cancelBlueConnetctToastTimer();
showToast('上传设置失败:${e.toString()}'.tr);
state.sureBtnState.value = 0;
}
}
//
Future<void> _replyUpdataLockSetReply(Reply reply) async {
final int status = reply.data[2];
dismissEasyLoading(); // loading
// loading状态直到整个过程完成
cancelBlueConnetctToastTimer();
switch (status) {
case 0x00:
await _lockDataUpload(
uploadType: 1,
recordType: 0,
records: reply.data.sublist(7, reply.data.length));
break;
case 0x06:
//
final List<int> token = reply.data.sublist(3, 7);
final List<String> saveStrList = changeIntListToStringList(token);
Storage.setStringList(saveBlueToken, saveStrList);
_uploadLockSet(token);
//token
try {
final List<int> token = reply.data.sublist(3, 7);
final List<String> saveStrList = changeIntListToStringList(token);
await Storage.setStringList(saveBlueToken, saveStrList);
_uploadLockSet(token);
} catch (e) {
if (EasyLoading.isShow) {
dismissEasyLoading(); // loading
}
showToast('获取设置权限失败:${e.toString()}'.tr);
state.sureBtnState.value = 0; //
}
break;
default:
dismissEasyLoading();
cancelBlueConnetctToastTimer();
if (EasyLoading.isShow) {
dismissEasyLoading(); // loading
}
showToast('获取锁设置失败 (错误码: $status)'.tr);
state.sureBtnState.value = 0; //
break;
}
}
@ -445,25 +585,43 @@ class ConfiguringWifiLogic extends BaseGetXController {
{required int uploadType,
required int recordType,
required List records}) async {
final LoginEntity entity = await ApiRepository.to.lockDataUpload(
lockId: state.lockBasicInfo.value.lockId ?? -1,
uploadType: uploadType,
recordType: recordType,
records: records,
isUnShowLoading: true);
if (entity.errorCode!.codeIsSuccessful) {
showToast('配网成功'.tr, something: () {
state.isLoading.value = false;
if (state.pageName.value == 'lockSet') {
Get.close(2);
} else {
Get.offAllNamed(Routers.starLockMain);
}
try {
final LoginEntity entity = await ApiRepository.to.lockDataUpload(
lockId: state.lockBasicInfo.value.lockId ?? -1,
uploadType: uploadType,
recordType: recordType,
records: records,
isUnShowLoading: true);
eventBus
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
eventBus.fire(SuccessfulDistributionNetwork());
});
if (entity.errorCode!.codeIsSuccessful) {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('配网成功'.tr, something: () {
state.sureBtnState.value = 0; //
if (state.pageName.value == 'lockSet') {
Get.close(2);
} else {
Get.offAllNamed(Routers.starLockMain);
}
eventBus.fire(
PassCurrentLockInformationEvent(state.lockSetInfoData.value));
eventBus.fire(SuccessfulDistributionNetwork());
});
} else {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('数据上传失败:${entity.errorCode}'.tr);
state.sureBtnState.value = 0; //
}
} catch (e) {
if (EasyLoading.isShow) {
dismissEasyLoading();
}
showToast('数据上传异常:${e.toString()}'.tr);
state.sureBtnState.value = 0; //
}
}
}

View File

@ -25,6 +25,9 @@ class _ConfiguringWifiPageState extends State<ConfiguringWifiPage>
final ConfiguringWifiLogic logic = Get.put(ConfiguringWifiLogic());
final ConfiguringWifiState state = Get.find<ConfiguringWifiLogic>().state;
//
final RxBool _obscureText = true.obs;
@override
Widget build(BuildContext context) {
return Scaffold(
@ -39,19 +42,36 @@ class _ConfiguringWifiPageState extends State<ConfiguringWifiPage>
'WiFi名称'.tr, '请输入WiFi名字'.tr, state.wifiNameController),
Container(
width: 1.sw, height: 1.h, color: AppColors.mainBackgroundColor),
configuringWifiTFWidget(
configuringWifiPasswordTFWidget(
'WiFi密码'.tr, '请输入WiFi密码'.tr, state.wifiPWDController),
SizedBox(
height: 50.h,
),
Obx(
() => SubmitBtn(
btnName: '确定'.tr,
isDisabled: state.isLoading.isFalse,
onClick: state.isLoading.isTrue
btnName: state.sureBtnState.value == 1 ? '配置中...'.tr : '确定'.tr,
// sureBtnState为1时按钮不可用
isDisabled: state.sureBtnState.value == 1,
onClick: state.sureBtnState.value == 1
? null
: () {
FocusScope.of(context).requestFocus(FocusNode());
//
if (state.wifiNameController.text.isEmpty) {
logic.showToast('请输入WiFi名称'.tr);
return;
}
if (state.wifiPWDController.text.isEmpty) {
logic.showToast('请输入WiFi密码'.tr);
return;
}
// WiFi名称是否包含5G关键字
if (state.wifiNameController.text
.toLowerCase()
.contains('5g')) {
logic.showToast('请确保使用2.4GHz WiFi网络'.tr);
return;
}
logic.senderConfiguringWifiAction();
},
),
@ -86,7 +106,22 @@ class _ConfiguringWifiPageState extends State<ConfiguringWifiPage>
);
}
//
Widget configuringWifiPasswordTFWidget(
String titleStr, String rightTitle, TextEditingController controller) {
return Column(
children: <Widget>[
Container(height: 10.h),
CommonItem(
leftTitel: titleStr,
rightTitle: '',
isHaveRightWidget: true,
rightWidget: getPasswordTFWidget(rightTitle, controller)),
Container(height: 10.h),
],
);
}
//
Widget getTFWidget(String tfStr, TextEditingController controller) {
return Container(
height: 65.h,
@ -95,18 +130,14 @@ class _ConfiguringWifiPageState extends State<ConfiguringWifiPage>
children: <Widget>[
Expanded(
child: TextField(
//
maxLines: 1,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.deny('\n'),
// LengthLimitingTextInputFormatter(30),
],
controller: controller,
autofocus: false,
textAlign: TextAlign.end,
decoration: InputDecoration(
//
// contentPadding: const EdgeInsets.only(top: 12.0, bottom: 8.0),
hintText: tfStr,
hintStyle: TextStyle(fontSize: 22.sp),
focusedBorder: const OutlineInputBorder(
@ -135,6 +166,61 @@ class _ConfiguringWifiPageState extends State<ConfiguringWifiPage>
);
}
//
Widget getPasswordTFWidget(String tfStr, TextEditingController controller) {
return Container(
height: 65.h,
width: 300.w,
child: Row(
children: <Widget>[
Expanded(
child: Obx(
() => TextField(
maxLines: 1,
obscureText: _obscureText.value,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.deny('\n'),
],
controller: controller,
autofocus: false,
textAlign: TextAlign.end,
decoration: InputDecoration(
hintText: tfStr,
hintStyle: TextStyle(fontSize: 22.sp),
focusedBorder: const OutlineInputBorder(
borderSide:
BorderSide(width: 0, color: Colors.transparent)),
disabledBorder: const OutlineInputBorder(
borderSide:
BorderSide(width: 0, color: Colors.transparent)),
enabledBorder: const OutlineInputBorder(
borderSide:
BorderSide(width: 0, color: Colors.transparent)),
border: const OutlineInputBorder(
borderSide:
BorderSide(width: 0, color: Colors.transparent)),
contentPadding: const EdgeInsets.symmetric(vertical: 0),
),
style: TextStyle(
fontSize: 22.sp, textBaseline: TextBaseline.alphabetic),
),
),
),
IconButton(
icon: Icon(
_obscureText.value ? Icons.visibility_off : Icons.visibility,
color: Colors.grey,
size: 24.sp,
),
onPressed: () {
_obscureText.value = !_obscureText.value;
},
),
],
),
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();

View File

@ -1,5 +1,3 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:network_info_plus/network_info_plus.dart';
@ -33,5 +31,4 @@ class ConfiguringWifiState {
String getGatewayConfigurationStr = '';
RxBool isLoading = false.obs;
Timer? loadingTimer;
}

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:star_lock/app_settings/app_settings.dart';
import 'package:star_lock/blue/io_gateway/io_gateway_getWifiList.dart';
import 'package:star_lock/blue/io_protocol/io_getWifiList.dart';
import 'package:star_lock/talk/starChart/star_chart_manage.dart';
@ -20,7 +21,9 @@ class WifiListLogic extends BaseGetXController {
//
late StreamSubscription<Reply> _replySubscription;
Timer? _connectionTimer;
///
void _initReplySubscription() {
_replySubscription =
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
@ -31,78 +34,130 @@ class WifiListLogic extends BaseGetXController {
if (reply is GatewayGetWifiListReply) {
_replyGetWifiListParameters(reply);
}
}, onError: (error) {
// CRC校验失败等错误
AppLog.log('WiFi列表获取过程中发生错误: $error');
// loading状态
dismissEasyLoading();
cancelBlueConnetctToastTimer();
//
state.sureBtnState.value = 0;
// CRC校验失败
if (error.toString().contains('CRC')) {
showToast('数据校验失败,请重新扫描'.tr);
} else {
showToast('扫描WiFi失败请重试'.tr);
}
});
}
// wifi列表数据解析
/// wifi列表数据解析
Future<void> _replySendGetWifiParameters(Reply reply) async {
final int status = reply.data[2];
switch (status) {
case 0x00:
//
showEasyLoading();
// - loading框UI中已经有进度指示器
cancelBlueConnetctToastTimer();
Future.delayed(5.seconds, dismissEasyLoading);
break;
case 0x06:
//
dismissEasyLoading();
AppLog.log('需要设备鉴权,请重试'.tr);
state.sureBtnState.value = 0;
break;
default:
//
dismissEasyLoading();
AppLog.log('获取WiFi列表失败错误码$status'.tr);
state.sureBtnState.value = 0;
break;
}
}
// WiFi数据解析
/// WiFi数据解析
Future<void> _replyGetWifiListParameters(Reply reply) async {
final int status = reply.data[2];
switch (status) {
case 0x00:
//
// showEasyLoading();
dismissEasyLoading();
state.sureBtnState.value = 0;
if (reply.data[3] > 0) {
reply.data.removeRange(0, 4);
// 33
// 33
final List<List<int>> getList = splitList(reply.data, 33);
final List<Map<String, String>> uploadList = <Map<String, String>>[];
for (int i = 0; i < getList.length; i++) {
final List<int> indexList = getList[i];
final Map<String, String> indexMap = <String, String>{};
final List<int> wifiName = indexList.sublist(0, 32);
indexMap['wifiName'] = utf8String(wifiName);
final String wifiNameStr = utf8String(wifiName).trim();
// WiFi名称
if (wifiNameStr.isEmpty) {
continue;
}
indexMap['wifiName'] = wifiNameStr;
indexMap['rssi'] = (indexList.last - 255).toString();
uploadList.add(indexMap);
state.wifiNameDataList.value = uploadList;
}
// WiFi列表 ()
uploadList.sort((a, b) =>
int.parse(b['rssi']!).compareTo(int.parse(a['rssi']!)));
state.wifiNameDataList.value = uploadList;
if (uploadList.isEmpty) {
showToast('未检测到可用的WiFi网络'.tr);
}
} else {
// WiFi列表为空的情况
state.wifiNameDataList.clear();
showToast('未检测到可用的WiFi网络'.tr);
}
break;
default:
//
dismissEasyLoading();
showToast('解析WiFi列表失败错误码$status'.tr);
state.sureBtnState.value = 0;
break;
}
}
// wifi列表
/// WiFi列表
Future<void> senderGetWifiListWifiAction() async {
if (state.sureBtnState.value == 1) {
return;
}
state.sureBtnState.value = 1;
state.wifiNameDataList.clear(); //
showEasyLoading();
// loading框UI中已经有进度指示器
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
state.sureBtnState.value = 0;
});
BlueManage().blueSendData(BlueManage().connectDeviceName,
(BluetoothConnectionState connectionState) async {
if (connectionState == BluetoothConnectionState.connected) {
IoSenderManage.gatewayGetWifiCommand(
userID: await Storage.getUid(),
);
try {
IoSenderManage.gatewayGetWifiCommand(
userID: await Storage.getUid(),
);
} catch (e) {
state.sureBtnState.value = 0;
cancelBlueConnetctToastTimer();
showToast('发送获取WiFi列表请求失败${e.toString()}'.tr);
}
} else if (connectionState == BluetoothConnectionState.disconnected) {
dismissEasyLoading();
state.sureBtnState.value = 0;
cancelBlueConnetctToastTimer();
if (state.ifCurrentScreen.value == true) {
@ -113,22 +168,26 @@ class WifiListLogic extends BaseGetXController {
}
@override
void onReady() {
void onReady() async {
super.onReady();
_initReplySubscription();
await senderGetWifiListWifiAction();
}
@override
void onInit() {
super.onInit();
senderGetWifiListWifiAction();
//
state.ifCurrentScreen.value = true;
}
@override
void onClose() {
super.onClose();
//
_replySubscription.cancel();
_connectionTimer?.cancel();
cancelBlueConnetctToastTimer();
state.ifCurrentScreen.value = false;
super.onClose();
}
}

View File

@ -23,6 +23,32 @@ class _WifiListPageState extends State<WifiListPage> {
final WifiListLogic logic = Get.put(WifiListLogic());
final WifiListState state = Get.find<WifiListLogic>().state;
/// WiFi信号强度图标
IconData _getWifiSignalIcon(String rssi) {
final int rssiValue = int.parse(rssi);
if (rssiValue >= -50) {
return Icons.signal_wifi_4_bar;
} else if (rssiValue >= -70) {
return Icons.network_wifi;
} else if (rssiValue >= -80) {
return Icons.network_wifi_2_bar_rounded;
} else {
return Icons.signal_wifi_0_bar;
}
}
@override
void initState() {
super.initState();
state.ifCurrentScreen.value = true;
}
@override
void dispose() {
state.ifCurrentScreen.value = false;
super.dispose();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
@ -38,13 +64,15 @@ class _WifiListPageState extends State<WifiListPage> {
barTitle: 'WIFI列表'.tr,
haveBack: state.pageName.value == 'lockSet',
actionsList: <Widget>[
TextButton(
child: Text(
'刷新'.tr,
style: TextStyle(color: Colors.white, fontSize: 24.sp),
),
onPressed: logic.senderGetWifiListWifiAction,
),
Obx(() => TextButton(
child: Text(
'刷新'.tr,
style: TextStyle(color: Colors.white, fontSize: 24.sp),
),
onPressed: state.sureBtnState.value == 0
? logic.senderGetWifiListWifiAction
: null,
)),
],
backgroundColor: AppColors.mainColor,
),
@ -52,26 +80,56 @@ class _WifiListPageState extends State<WifiListPage> {
children: <Widget>[
Expanded(
child: Obx(() => state.wifiNameDataList.value.isNotEmpty
? ListView.builder(
itemCount: state.wifiNameDataList.value.length,
itemBuilder: (BuildContext c, int index) {
Map wifiNameStr = state.wifiNameDataList.value[index];
return _messageListItem(
wifiNameStr['wifiName'], wifiNameStr['rssi'], () {
Get.toNamed(Routers.configuringWifiPage,
arguments: {
'lockSetInfoData':
state.lockSetInfoData.value,
'wifiName': wifiNameStr['wifiName'],
'pageName': state.pageName.value,
});
});
})
: NoData(
noDataHeight: 1.sh -
ScreenUtil().statusBarHeight -
ScreenUtil().bottomBarHeight -
64.h)),
? RefreshIndicator(
onRefresh: () async {
if (state.sureBtnState.value == 0) {
await logic.senderGetWifiListWifiAction();
}
},
child: ListView.builder(
itemCount: state.wifiNameDataList.value.length,
itemBuilder: (BuildContext c, int index) {
Map wifiNameStr =
state.wifiNameDataList.value[index];
return _messageListItem(
wifiNameStr['wifiName'], wifiNameStr['rssi'],
() {
Get.toNamed(Routers.configuringWifiPage,
arguments: {
'lockSetInfoData':
state.lockSetInfoData.value,
'wifiName': wifiNameStr['wifiName'],
'pageName': state.pageName.value,
});
});
}),
)
: Obx(() => state.sureBtnState.value == 1
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 50.w,
height: 50.w,
child: CircularProgressIndicator(
strokeWidth: 4.w,
valueColor: AlwaysStoppedAnimation<Color>(
AppColors.mainColor,
),
backgroundColor: Colors.grey[200],
),
),
SizedBox(height: 20.h),
Text('正在扫描WiFi网络...\n请确保设备处于正常状态'.tr,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24.sp,
color: AppColors.blackColor))
],
),
)
: NoData())),
),
state.pageName.value == 'saveLock'
? SubmitBtn(
@ -140,24 +198,37 @@ class _WifiListPageState extends State<WifiListPage> {
height: 79.h,
width: 1.sw - 20.w * 2,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
flex: 4,
child: Text(
'$wifiName(${rssi}db)',
wifiName,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 22.sp, color: AppColors.blackColor),
fontSize: 24.sp, color: AppColors.blackColor),
),
),
// Text(
// rssi,
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
// style: TextStyle(
// fontSize: 22.sp, color: AppColors.blackColor),
// )
Flexible(
flex: 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Icon(
_getWifiSignalIcon(rssi),
color: AppColors.mainColor,
size: 24.sp,
),
SizedBox(width: 8.w),
Text(
'$rssi dB',
style:
TextStyle(fontSize: 18.sp, color: Colors.black),
),
],
),
)
],
),
),