更新登录对接
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:easydevice/src/ble_device.dart';
|
||||
import 'package:ef/ef.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||||
import 'package:vbvs_app/common/color/app_uri_status.dart';
|
||||
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/model/BleDeviceData.dart';
|
||||
import 'package:vbvs_app/model/api_response.dart';
|
||||
|
||||
part 'blueteeth_bind_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
|
||||
@@ -8,7 +19,8 @@ class BlueteethBindModel {
|
||||
int? read = 1; //是否不再提示教程 0 不再提示 1 需要提示
|
||||
double? singal = -70; //扫描信号强度
|
||||
|
||||
List? devicelist = []; //蓝牙扫描到的设备数据列表
|
||||
List<BleDeviceData>? devicelist = []; //蓝牙扫描到的设备数据列表
|
||||
List<BleDeviceData>? betDevicelist = []; //请求的
|
||||
List? blelist = []; //蓝牙扫描到的设备数据列表
|
||||
List? wifiList = [];
|
||||
|
||||
@@ -46,21 +58,122 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
attr = GetModel(BlueteethBindModel()).obs;
|
||||
}
|
||||
|
||||
void updateDeviceStatus() {
|
||||
// try {
|
||||
Timer? _statusTimer;
|
||||
|
||||
// } catch (e) {
|
||||
// print(e);
|
||||
// EasyDartModule.logger.info("向后端请求设备绑定状态报错了:$e");
|
||||
// } finally {
|
||||
// EasyDartModule.logger.info("向后端请求设备绑定状态");
|
||||
// }
|
||||
BLEDevice? currentDevice;
|
||||
|
||||
// 启动每10秒获取设备状态
|
||||
void startStatusPolling() {
|
||||
if (_statusTimer == null) {
|
||||
_statusTimer = Timer.periodic(Duration(seconds: 10), (timer) {
|
||||
updateDeviceStatus();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future bindDevice(d) async {
|
||||
print("绑定参数:$d");
|
||||
await Future.delayed(Duration(seconds: 1));
|
||||
// return ApiService.request
|
||||
// .post("/api/device/info/bind", data: formdata.FormData.fromMap(d));
|
||||
// 停止轮询
|
||||
void stopStatusPolling() {
|
||||
_statusTimer?.cancel();
|
||||
_statusTimer = null;
|
||||
}
|
||||
|
||||
// 你的已有方法
|
||||
Future<void> updateDeviceStatus() async {
|
||||
try {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.get_bluetooth_device_status;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
|
||||
if (model.devicelist != null && model.devicelist!.isNotEmpty) {
|
||||
final macParams = model.devicelist!
|
||||
.map((device) => "mac=${Uri.encodeQueryComponent(device.mac!)}")
|
||||
.join("&");
|
||||
|
||||
if (queryUrl.contains('?')) {
|
||||
queryUrl += '&$macParams';
|
||||
} else {
|
||||
queryUrl += '?$macParams';
|
||||
}
|
||||
|
||||
var response = await EasyDartModule.dio.get(queryUrl);
|
||||
|
||||
if (response.data['data'] != null && response.data['data'] is List) {
|
||||
List<dynamic> responseList = response.data['data'];
|
||||
|
||||
Map<String, BleDeviceData> deviceMap = {
|
||||
for (var d in model.devicelist!)
|
||||
if (d.mac != null) d.mac!: d
|
||||
};
|
||||
|
||||
List<String> slaveMacsToRemove = [];
|
||||
|
||||
for (var item in responseList) {
|
||||
String mac = item['mac'];
|
||||
String? bindMac = item['bindMac'];
|
||||
bool? bind = item['bind'];
|
||||
|
||||
if (deviceMap.containsKey(mac)) {
|
||||
BleDeviceData currentDevice = deviceMap[mac]!;
|
||||
currentDevice.bind = bind;
|
||||
if (bindMac != null && deviceMap.containsKey(bindMac)) {
|
||||
BleDeviceData masterDevice = deviceMap[bindMac]!;
|
||||
masterDevice.slave = currentDevice;
|
||||
slaveMacsToRemove.add(mac);
|
||||
}
|
||||
}
|
||||
}
|
||||
model.devicelist!
|
||||
.removeWhere((device) => slaveMacsToRemove.contains(device.mac));
|
||||
}
|
||||
print("获取设备状态成功");
|
||||
updateAll();
|
||||
}
|
||||
} catch (e) {
|
||||
print("获取设备状态异常: $e");
|
||||
EasyDartModule.logger.info("获取设备状态异常: $e");
|
||||
DailyLogUtils.writeLog("获取设备状态异常: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<ApiResponse> bindDeviceAndMAC(BleDeviceData d) async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "蓝牙绑定.绑定失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_bind;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
var data = {
|
||||
"deviceType": 1,
|
||||
"mac": d.mac,
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||||
if (response != null) {
|
||||
var responseData =
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
MyUtils.formatResponse(apiResponse, "蓝牙绑定.绑定成功".tr, "蓝牙绑定.绑定成功".tr);
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
return apiResponse;
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
||||
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
||||
}
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
stopStatusPolling(); // 控制器销毁时停止轮询
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
bindDevice(Map<String, dynamic> map) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user