绑定设备+覆盖模式+控制显示bug+在线更新
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../../../core/consts/http_api_consts.dart';
|
||||
import '../../domain/entities/device_entity.dart';
|
||||
import '../../domain/entities/device_status_entity.dart';
|
||||
@@ -12,6 +13,7 @@ abstract class DeviceRemoteDataSource {
|
||||
int? siteId,
|
||||
String? typeFilter,
|
||||
});
|
||||
Future<List<DeviceDataModel>> getAllDevices();
|
||||
}
|
||||
|
||||
/// 设备远程数据源实现类 - 从真实 API 获取数据
|
||||
@@ -64,6 +66,37 @@ class DeviceRemoteDataSourceImpl implements DeviceRemoteDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<DeviceDataModel>> getAllDevices() async {
|
||||
try {
|
||||
final response = await dio.get(
|
||||
HttpApiConsts.getDeviceList,
|
||||
queryParameters: {'pageNum': 1, 'pageSize': 9999},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final responseData = response.data;
|
||||
if (responseData['code'] != 200) {
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
final List<dynamic> rows = responseData['rows'] ?? [];
|
||||
debugPrint(
|
||||
'>>> [DeviceRemoteDataSource] getAllDevices 返回 ${rows.length} 条设备',
|
||||
);
|
||||
for (int i = 0; i < rows.length && i < 10; i++) {
|
||||
debugPrint('>>> [DeviceRemoteDataSource] rows[$i]: ${rows[i]}');
|
||||
}
|
||||
return rows.map((item) => _parseDeviceFromJson(item)).toList();
|
||||
} catch (e) {
|
||||
debugPrint('>>> [DeviceRemoteDataSource] getAllDevices error: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<DeviceDataModel>> _fetchDevicesFromAPI(int siteId) async {
|
||||
final response = await dio.get(
|
||||
HttpApiConsts.getSiteDeviceList,
|
||||
@@ -107,10 +140,19 @@ class DeviceRemoteDataSourceImpl implements DeviceRemoteDataSource {
|
||||
DeviceDataModel _parseDeviceFromJson(Map<String, dynamic> json) {
|
||||
// 在线状态:使用 onlineStatus 字段(1=在线, 0=离线)
|
||||
final onlineStatus = json['onlineStatus'] ?? 0;
|
||||
final deviceId = json['deviceId']?.toString() ?? '';
|
||||
final name = json['deviceName'] ?? json['name'] ?? '';
|
||||
final type = json['deviceTypeName'] ?? json['type'] ?? '未知设备';
|
||||
final isRobot = _isRobotDevice(type, name, deviceId);
|
||||
|
||||
debugPrint(
|
||||
'🔍 [DeviceListDataSource] deviceId=$deviceId name=$name type=$type isRobot=$isRobot',
|
||||
);
|
||||
|
||||
return DeviceDataModel(
|
||||
deviceId: json['deviceId']?.toString() ?? '',
|
||||
name: json['deviceName'] ?? json['name'] ?? '',
|
||||
type: json['deviceTypeName'] ?? json['type'] ?? '未知设备',
|
||||
deviceId: deviceId,
|
||||
name: name,
|
||||
type: type,
|
||||
status: onlineStatus == 1 ? '在线' : '离线',
|
||||
power: (json['power'] as num?)?.toDouble() ?? 0,
|
||||
todayEnergy: (json['todayEnergy'] as num?)?.toDouble() ?? 0,
|
||||
@@ -118,6 +160,7 @@ class DeviceRemoteDataSourceImpl implements DeviceRemoteDataSource {
|
||||
current: (json['current'] as num?)?.toDouble(),
|
||||
voltage: (json['voltage'] as num?)?.toDouble(),
|
||||
radiation: (json['radiation'] as num?)?.toDouble(),
|
||||
isRobot: isRobot,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -131,9 +174,32 @@ class DeviceRemoteDataSourceImpl implements DeviceRemoteDataSource {
|
||||
power: (json['capacity_percent'] as num?)?.toDouble() ?? 0,
|
||||
todayEnergy: 0,
|
||||
temperature: (json['environment_temperature'] as num?)?.toDouble(),
|
||||
isRobot: false,
|
||||
);
|
||||
}
|
||||
|
||||
/// 🔥 通用判断:是否机器人(型号前缀 + 中文关键字双重匹配,新型号也能自动识别)
|
||||
bool _isRobotDevice(String type, String name, String deviceId) {
|
||||
final haystacks = [
|
||||
type.toLowerCase(),
|
||||
name.toLowerCase(),
|
||||
deviceId.toLowerCase(),
|
||||
];
|
||||
// 型号家族前缀(MC700/MC700STD/MC700PLUS/MC700AIR/MC700PRO 等;RCHWD/RCHETD 等)
|
||||
const prefixes = ['mc', 'rch', 'rcetd', 'rchetd'];
|
||||
// 中文类型关键字
|
||||
const keywords = ['除草', '巡检', '清洗', '割草', '机器人'];
|
||||
for (final s in haystacks) {
|
||||
for (final p in prefixes) {
|
||||
if (s.startsWith(p)) return true;
|
||||
}
|
||||
}
|
||||
for (final k in keywords) {
|
||||
if (type.contains(k) || name.contains(k)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String _getStatusText(int status) {
|
||||
switch (status) {
|
||||
case 1:
|
||||
|
||||
@@ -12,6 +12,7 @@ class DeviceDataModel {
|
||||
final double? current;
|
||||
final double? voltage;
|
||||
final double? radiation;
|
||||
final bool isRobot;
|
||||
|
||||
const DeviceDataModel({
|
||||
required this.deviceId,
|
||||
@@ -24,6 +25,7 @@ class DeviceDataModel {
|
||||
this.current,
|
||||
this.voltage,
|
||||
this.radiation,
|
||||
this.isRobot = false,
|
||||
});
|
||||
|
||||
/// 从 JSON 创建数据模型
|
||||
@@ -71,6 +73,7 @@ class DeviceDataModel {
|
||||
current: current,
|
||||
voltage: voltage,
|
||||
radiation: radiation,
|
||||
isRobot: isRobot,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,10 @@ class DeviceRepositoryImpl implements DeviceRepository {
|
||||
final dataModels = await remoteDataSource.getDeviceList(siteId: siteId, typeFilter: typeFilter);
|
||||
return dataModels.map((model) => model.toEntity()).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<DeviceEntity>> getAllDevices() async {
|
||||
final dataModels = await remoteDataSource.getAllDevices();
|
||||
return dataModels.map((model) => model.toEntity()).toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user