Files
tuiche/lib/controller/mh_controller/device_list_controller.dart
2025-10-28 14:51:28 +08:00

202 lines
6.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:ef/ef.dart';
import 'package:flutter/material.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/common/util/requestWithLog.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/model/api_response.dart';
part 'device_list_controller.g.dart';
@JsonSerializable()
class DeviceListModel {
//设备列表
List<dynamic> deviceList = [];
List<dynamic> vitalList = [];
@JsonKey(ignore: true)
String? keyword;
@JsonKey(ignore: true)
Color? color = Color(0xFFFFFFFF);
DeviceListModel();
static DeviceListModel fromJson(Map<String, dynamic> json) =>
_$DeviceListModelFromJson(json);
Map<String, dynamic> toJson() => _$DeviceListModelToJson(this);
}
class DeviceListController extends GetControllerEx<DeviceListModel> {
DeviceListController() {
attr = GetModel(DeviceListModel()).obs;
}
//床和床垫
getDeviceList() async {
try {
String search = (model.keyword != null && model.keyword!.isNotEmpty)
? "?key=${model.keyword}&ncs=1"
: "?ncs=1";
ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备列表请求失败".tr);
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.device_list;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}$search";
var response = await EasyDartModule.dio.get(queryUrl);
if (response != null) {
var responseData =
response.data is String ? jsonDecode(response.data) : response.data;
ApiResponse res =
ApiResponse.fromJson(responseData, (object) => object);
MyUtils.formatResponse(res, "设备列表请求成功".tr, "设备列表请求失败".tr);
if (res.code == HttpStatusCodes.ok) {
// bindDeviceNum.value = res.total!;
model.deviceList = res.data;
updateAll();
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
}
//体征传感器
getVitalList(String mac) async {
try {
ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备列表请求失败".tr);
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.device_list;
String queryUrl =
"${serviceAddress}${serviceName}${serviceApi}?bindType=1&mac=$mac";
var response = await EasyDartModule.dio.get(queryUrl);
if (response != null) {
var responseData =
response.data is String ? jsonDecode(response.data) : response.data;
ApiResponse res =
ApiResponse.fromJson(responseData, (object) => object);
MyUtils.formatResponse(res, "设备列表请求成功".tr, "设备列表请求失败".tr);
if (res.code == HttpStatusCodes.ok) {
// bindDeviceNum.value = res.total!;
model.vitalList = res.data;
updateAll();
}
} 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
}
//主动解绑设备
Future<String> unbindDevice(Map<dynamic, dynamic> device) async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.device_bind;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
String msg = '';
int type = device['bind_type'];
await requestWithLog(
logTitle: '解绑设备',
method: MyHttpMethod.delete,
queryUrl: queryUrl,
data: {"mac": device['mac']},
onSuccess: (res) {
if (type == 1) {
msg = '解绑成功'.tr;
} else {
msg = '删除成功'.tr;
}
//删除用户的自定义配置
delDeviceSleepHabit(device);
},
onFailure: (res) {
if (type == 1) {
msg = '解绑失败'.tr;
} else {
msg = '删除失败'.tr;
}
},
);
return msg;
}
unbindShareDevice(String id, String mac, BuildContext context) async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.device_bind;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
String msg = '';
await requestWithLog(
logTitle: '解绑设备',
method: MyHttpMethod.delete,
queryUrl: queryUrl,
data: {"mac": mac, "uid": id},
onSuccess: (res) {
TopSlideNotification.show(context,
text: res.msg!, textColor: Color(0XFF00C1AA));
Get.back();
},
onFailure: (err) {
TopSlideNotification.show(context,
text: err.msg!, textColor: Color(0xFFFF7159));
},
);
}
void delDeviceSleepHabit(Map device) {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.user_setting;
String type = "sleep_habit_${formatMacAddress(device['mac'])}";
String queryUrl =
"${serviceAddress}${serviceName}${serviceApi}?type=${type}";
requestWithLog(
logTitle: "删除设备自定义配置",
method: MyHttpMethod.delete,
queryUrl: queryUrl,
onSuccess: (res) {
print(res);
},
onFailure: (res) {
print(res);
},
);
}
String formatMacAddress(String input) {
if (input.isEmpty) return input;
// 1. 移除所有非16进制字符去掉冒号、空格、破折号等
String clean = input.replaceAll(RegExp(r'[^a-fA-F0-9]'), '');
// 2. 如果长度不足12左侧补0MAC地址长度固定12
clean = clean.padLeft(12, '0');
// 3. 按2个字符一组拼接冒号
List<String> parts = [];
for (int i = 0; i < 12; i += 2) {
parts.add(clean.substring(i, i + 2).toUpperCase());
}
// 4. 返回格式化字符串
return parts.join(':');
}
}