更新界面布局

This commit is contained in:
wyf
2025-06-06 15:53:22 +08:00
parent 45b0d8ccb9
commit c9d7fceb98
8 changed files with 323 additions and 194 deletions

View File

@@ -136,7 +136,128 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
_statusTimer = null;
}
Future<ApiResponse> updateDeviceStatus() async {
// Future<ApiResponse> 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';
// }
// String? language = "";
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// if (language != null && language.isNotEmpty) {
// if (queryUrl.contains("?")) {
// queryUrl += "&lang=$language";
// } else {
// queryUrl += "?lang=$language";
// }
// }
// var response = await EasyDartModule.dio.get(queryUrl);
// var responseData =
// response.data is String ? jsonDecode(response.data) : response.data;
// ApiResponse res =
// ApiResponse.fromJson(responseData, (object) => object);
// if (res.code != HttpStatusCodes.ok) return res;
// if (response.data['data'] != null && response.data['data'] is List) {
// List<dynamic> responseList = response.data['data'];
// // 构建 mac -> 设备的映射
// Map<String, BleDeviceData> deviceMap = {
// for (var d in model.devicelist!)
// if (d.mac != null) d.mac!.toLowerCase(): d,
// };
// // 用于记录已经设置过主从关系的 mac避免重复
// Set<String> processedMacs = {};
// for (var item in responseList) {
// String mac = item['mac'].toLowerCase();
// String? bindMac = item['bindMac']?.toLowerCase();
// bool? bind = item['bind'];
// if (!deviceMap.containsKey(mac)) continue;
// BleDeviceData currentDevice = deviceMap[mac]!;
// currentDevice.bind = bind;
// if (bindMac != null && deviceMap.containsKey(bindMac)) {
// final isMutualBind = responseList.any((e) =>
// e['mac']?.toString().toLowerCase() == bindMac &&
// e['bindMac']?.toString().toLowerCase() == mac);
// if (isMutualBind) {
// if (processedMacs.contains(mac) ||
// processedMacs.contains(bindMac)) {
// continue;
// }
// final masterMac = (mac.compareTo(bindMac) < 0) ? mac : bindMac;
// final slaveMac = (mac.compareTo(bindMac) < 0) ? bindMac : mac;
// if (deviceMap.containsKey(masterMac) &&
// deviceMap.containsKey(slaveMac)) {
// deviceMap[masterMac]!.slave = deviceMap[slaveMac];
// processedMacs.add(masterMac);
// processedMacs.add(slaveMac);
// }
// } else {
// if (!processedMacs.contains(mac)) {
// BleDeviceData masterDevice = deviceMap[bindMac]!;
// masterDevice.slave = currentDevice;
// processedMacs.add(mac);
// processedMacs.add(bindMac);
// }
// }
// }
// }
// // 获取所有被作为 slave 的 mac用于排除掉
// final Set<String> allSlaveMacs = {
// for (var d in deviceMap.values)
// if (d.slave?.mac != null) d.slave!.mac!.toLowerCase()
// };
// // 构造最终列表,只保留主设备和未被作为 slave 的独立设备
// final List<BleDeviceData> finalList = deviceMap.values.where((d) {
// final mac = d.mac?.toLowerCase();
// if (mac == null) return false;
// if (d.slave != null) return true; // 主设备
// return !allSlaveMacs.contains(mac); // 不是别人 slave 的独立设备
// }).toList();
// model.betDevicelist = finalList;
// } else {
// model.betDevicelist = [];
// }
// updateAll();
// return res;
// }
// } catch (e) {
// print("获取设备状态异常: $e");
// EasyDartModule.logger.info("获取设备状态异常: $e");
// DailyLogUtils.writeLog("获取设备状态异常: $e");
// return ApiResponse(code: -1, msg: "请求失败".tr);
// }
// return ApiResponse(code: -1, msg: "未知错误".tr);
// }
Future<ApiResponse> updateDeviceStatus() async {
try {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
@@ -174,72 +295,32 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
if (response.data['data'] != null && response.data['data'] is List) {
List<dynamic> responseList = response.data['data'];
// 构建 mac -> 设备的映射
Map<String, BleDeviceData> deviceMap = {
for (var d in model.devicelist!)
if (d.mac != null) d.mac!.toLowerCase(): d,
if (d.mac != null) d.mac!: d
};
// 用于记录已经设置过主从关系的 mac避免重复
Set<String> processedMacs = {};
List<String> slaveMacsToRemove = [];
for (var item in responseList) {
String mac = item['mac'].toLowerCase();
String? bindMac = item['bindMac']?.toLowerCase();
String mac = item['mac'];
String? bindMac = item['bindMac'];
bool? bind = item['bind'];
if (!deviceMap.containsKey(mac)) continue;
BleDeviceData currentDevice = deviceMap[mac]!;
currentDevice.bind = bind;
if (bindMac != null && deviceMap.containsKey(bindMac)) {
final isMutualBind = responseList.any((e) =>
e['mac']?.toString().toLowerCase() == bindMac &&
e['bindMac']?.toString().toLowerCase() == mac);
if (isMutualBind) {
if (processedMacs.contains(mac) ||
processedMacs.contains(bindMac)) {
continue;
}
final masterMac = (mac.compareTo(bindMac) < 0) ? mac : bindMac;
final slaveMac = (mac.compareTo(bindMac) < 0) ? bindMac : mac;
if (deviceMap.containsKey(masterMac) &&
deviceMap.containsKey(slaveMac)) {
deviceMap[masterMac]!.slave = deviceMap[slaveMac];
processedMacs.add(masterMac);
processedMacs.add(slaveMac);
}
} else {
if (!processedMacs.contains(mac)) {
BleDeviceData masterDevice = deviceMap[bindMac]!;
masterDevice.slave = currentDevice;
processedMacs.add(mac);
processedMacs.add(bindMac);
}
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);
}
}
}
// 获取所有被作为 slave 的 mac用于排除掉
final Set<String> allSlaveMacs = {
for (var d in deviceMap.values)
if (d.slave?.mac != null) d.slave!.mac!.toLowerCase()
};
// 构造最终列表,只保留主设备和未被作为 slave 的独立设备
final List<BleDeviceData> finalList = deviceMap.values.where((d) {
final mac = d.mac?.toLowerCase();
if (mac == null) return false;
if (d.slave != null) return true; // 主设备
return !allSlaveMacs.contains(mac); // 不是别人 slave 的独立设备
}).toList();
model.betDevicelist = finalList;
model.devicelist!
.removeWhere((device) => slaveMacsToRemove.contains(device.mac));
model.betDevicelist = model.devicelist!;
} else {
model.betDevicelist = [];
}

View File

@@ -2,8 +2,8 @@ import 'dart:async';
import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:ef/ef.dart';
// import 'package:geocoding/geocoding.dart';
// import 'package:geolocator/geolocator.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/common/util/CommonVariables.dart';
import 'package:vbvs_app/controller/setting/language/language_controller.dart';
@@ -37,148 +37,148 @@ class WeatherModelController extends GetControllerEx<WeatherModel> {
LanguageController languageController = Get.find();
WeatherModelController() {
attr = GetModel(WeatherModel()).obs;
// weatherFactory = WeatherFactory(CommonVariables.weather_apiKey,
// language: Language.CHINESE_SIMPLIFIED);
weatherFactory = WeatherFactory(CommonVariables.weather_apiKey,
language: Language.CHINESE_SIMPLIFIED);
}
// Timer? _weatherTimer;
// Timer? _locationTimer;
Timer? _weatherTimer;
Timer? _locationTimer;
// late WeatherFactory weatherFactory;
late WeatherFactory weatherFactory;
// @override
// Future<void> onInit() async {
// super.onInit();
// await _getCurrentLocation();
// _weatherTimer = Timer.periodic(Duration(seconds: 5), (timer) {
// _getCurrentWeather(); // 每 5 秒更新一次天气
// });
@override
Future<void> onInit() async {
super.onInit();
await _getCurrentLocation();
_weatherTimer = Timer.periodic(Duration(seconds: 5), (timer) {
_getCurrentWeather(); // 每 5 秒更新一次天气
});
// _locationTimer = Timer.periodic(Duration(minutes: 10), (timer) {
// _getCurrentLocation(); // 每 10 分钟更新一次位置
// });
// }
_locationTimer = Timer.periodic(Duration(minutes: 10), (timer) {
_getCurrentLocation(); // 每 10 分钟更新一次位置
});
}
// @override
// void onClose() {
// _weatherTimer?.cancel(); // 取消天气更新定时器
// _locationTimer?.cancel(); // 取消位置更新定时器
// super.onClose();
// }
@override
void onClose() {
_weatherTimer?.cancel(); // 取消天气更新定时器
_locationTimer?.cancel(); // 取消位置更新定时器
super.onClose();
}
// // 获取当前位置并存储到 model
// Future<void> _getCurrentLocation() async {
// try {
// Position position = await _determinePosition();
// if (position == null) {
// throw Exception("获取位置失败");
// }
// 获取当前位置并存储到 model
Future<void> _getCurrentLocation() async {
try {
Position position = await _determinePosition();
if (position == null) {
throw Exception("获取位置失败");
}
// String? language = "zh_CN";
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// List<Placemark> placemarks = await placemarkFromCoordinates(
// position.latitude, position.longitude,
// localeIdentifier: language);
String? language = "zh_CN";
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
List<Placemark> placemarks = await placemarkFromCoordinates(
position.latitude, position.longitude,
localeIdentifier: language);
// if (placemarks.isNotEmpty) {
// model.cityName = placemarks[0].locality ?? "未知数据".tr;
// model.latitude = position.latitude;
// model.longitude = position.longitude;
// }
if (placemarks.isNotEmpty) {
model.cityName = placemarks[0].locality ?? "未知数据".tr;
model.latitude = position.latitude;
model.longitude = position.longitude;
}
// // 调用获取天气方法
// _getCurrentWeather();
// } catch (e) {
// print(e);
// EasyDartModule.logger.error("获取位置失败: $e");
// }
// }
// 调用获取天气方法
_getCurrentWeather();
} catch (e) {
print(e);
EasyDartModule.logger.error("获取位置失败: $e");
}
}
// // 获取当前位置
// Future<Position> _determinePosition() async {
// bool serviceEnabled;
// LocationPermission permission;
// 获取当前位置
Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
// serviceEnabled = await Geolocator.isLocationServiceEnabled();
// if (!serviceEnabled) {
// return Future.error('位置服务未启用');
// }
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('位置服务未启用');
}
// permission = await Geolocator.checkPermission();
// if (permission == LocationPermission.denied) {
// permission = await Geolocator.requestPermission();
// if (permission == LocationPermission.denied) {
// return Future.error('位置权限被拒绝');
// }
// }
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('位置权限被拒绝');
}
}
// if (permission == LocationPermission.deniedForever) {
// return Future.error('位置权限被永久拒绝');
// }
if (permission == LocationPermission.deniedForever) {
return Future.error('位置权限被永久拒绝');
}
// return await Geolocator.getCurrentPosition();
// }
return await Geolocator.getCurrentPosition();
}
// // 获取天气信息
// Future<void> _getCurrentWeather() async {
// if (model.latitude == null || model.longitude == null) {
// EasyDartModule.logger.error("获取天气失败:位置数据获取失败");
// return; // 如果位置数据没有获取到,则不更新天气
// }
// String? language = "zh_CN";
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// List<Placemark> placemarks = await placemarkFromCoordinates(
// model.latitude!, model.longitude!,
// localeIdentifier: language);
// 获取天气信息
Future<void> _getCurrentWeather() async {
if (model.latitude == null || model.longitude == null) {
EasyDartModule.logger.error("获取天气失败:位置数据获取失败");
return; // 如果位置数据没有获取到,则不更新天气
}
String? language = "zh_CN";
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
List<Placemark> placemarks = await placemarkFromCoordinates(
model.latitude!, model.longitude!,
localeIdentifier: language);
// if (placemarks.isNotEmpty) {
// model.cityName = placemarks[0].locality ?? "未知数据".tr;
// }
if (placemarks.isNotEmpty) {
model.cityName = placemarks[0].locality ?? "未知数据".tr;
}
// try {
// weatherFactory.language = Language.CHINESE_SIMPLIFIED;
// String? language = "zh_CN";
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// if (language == "zh_CN") {
// weatherFactory.language = Language.CHINESE_SIMPLIFIED;
// } else {
// weatherFactory.language = Language.ENGLISH;
// }
// Weather weather = await weatherFactory.currentWeatherByLocation(
// model.latitude!, model.longitude!);
try {
weatherFactory.language = Language.CHINESE_SIMPLIFIED;
String? language = "zh_CN";
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
if (language == "zh_CN") {
weatherFactory.language = Language.CHINESE_SIMPLIFIED;
} else {
weatherFactory.language = Language.ENGLISH;
}
Weather weather = await weatherFactory.currentWeatherByLocation(
model.latitude!, model.longitude!);
// model.weather_info = weather.weatherDescription;
// model.min_temperature = weather.tempMin?.celsius?.toInt();
// model.max_temperature = weather.tempMax?.celsius?.toInt();
// model.current_temperature = weather.temperature?.celsius?.toInt();
// model.wind_speed = weather.windSpeed?.toInt();
// model.weatherIcon = weather.weatherIcon;
// if (model.weatherIcon != null) {
// model.weatherIconurl =
// "https://openweathermap.org/img/w/${model.weatherIcon}.png";
// }
// updateAll(); // 更新 UI
// } catch (e) {
// EasyDartModule.logger.error("获取天气失败: $e");
// print('获取天气失败: $e');
// }
// }
model.weather_info = weather.weatherDescription;
model.min_temperature = weather.tempMin?.celsius?.toInt();
model.max_temperature = weather.tempMax?.celsius?.toInt();
model.current_temperature = weather.temperature?.celsius?.toInt();
model.wind_speed = weather.windSpeed?.toInt();
model.weatherIcon = weather.weatherIcon;
if (model.weatherIcon != null) {
model.weatherIconurl =
"https://openweathermap.org/img/w/${model.weatherIcon}.png";
}
updateAll(); // 更新 UI
} catch (e) {
EasyDartModule.logger.error("获取天气失败: $e");
print('获取天气失败: $e');
}
}
// // 获取 5 天天气预报
// Future<List<Weather>> getWeatherForecast(
// double latitude, double longitude) async {
// try {
// return await weatherFactory.fiveDayForecastByLocation(
// latitude, longitude);
// } catch (e) {
// print('获取天气预报失败: $e');
// rethrow;
// }
// }
// 获取 5 天天气预报
Future<List<Weather>> getWeatherForecast(
double latitude, double longitude) async {
try {
return await weatherFactory.fiveDayForecastByLocation(
latitude, longitude);
} catch (e) {
print('获取天气预报失败: $e');
rethrow;
}
}
}