对接路径规划的列表(根据场站的下的)

优化无人机机场的监控视频的显示视频流畅度提升80%
添加首页tcp指示灯的机器状态信息展示功能。(暂不支持在此页面上设备的切换显示)
This commit is contained in:
2026-06-09 14:39:55 +08:00
parent ff4a3836c7
commit 058a5d9588
26 changed files with 3634 additions and 1028 deletions

View File

@@ -0,0 +1,237 @@
/// 工作记录实体(用于XML解析)
class WorkRecordEntity {
final String? createBy;
final String? createTime;
final String? updateBy;
final String? updateTime;
final bool delFlag;
final String? remark;
final int orgId;
final int siteId;
final int userId;
final int id;
final String workName;
final WorkRecordJsonData? jsonData;
final String? imgUrl;
WorkRecordEntity({
this.createBy,
this.createTime,
this.updateBy,
this.updateTime,
required this.delFlag,
this.remark,
required this.orgId,
required this.siteId,
required this.userId,
required this.id,
required this.workName,
this.jsonData,
this.imgUrl,
});
factory WorkRecordEntity.fromXml(Map<String, dynamic> xmlData) {
return WorkRecordEntity(
createBy: xmlData['createBy'] as String?,
createTime: xmlData['createTime'] as String?,
updateBy: xmlData['updateBy'] as String?,
updateTime: xmlData['updateTime'] as String?,
delFlag: xmlData['delFlag'] == 'true',
remark: xmlData['remark'] as String?,
orgId: int.tryParse(xmlData['orgId']?.toString() ?? '0') ?? 0,
siteId: int.tryParse(xmlData['siteId']?.toString() ?? '0') ?? 0,
userId: int.tryParse(xmlData['userId']?.toString() ?? '0') ?? 0,
id: int.tryParse(xmlData['id']?.toString() ?? '0') ?? 0,
workName: xmlData['workName'] as String? ?? '',
jsonData: xmlData['jsonData'] != null
? WorkRecordJsonData.fromXml(
xmlData['jsonData'] as Map<String, dynamic>,
)
: null,
imgUrl: xmlData['imgUrl'] as String?,
);
}
factory WorkRecordEntity.fromJson(Map<String, dynamic> json) {
return WorkRecordEntity(
createBy: json['createBy'] as String?,
createTime: json['createTime'] as String?,
updateBy: json['updateBy'] as String?,
updateTime: json['updateTime'] as String?,
delFlag: json['delFlag'] == true || json['delFlag'] == 'true',
remark: json['remark'] as String?,
orgId: _parseInt(json['orgId']),
siteId: _parseInt(json['siteId']),
userId: _parseInt(json['userId']),
id: _parseInt(json['id']),
workName: json['workName'] as String? ?? '',
jsonData: json['jsonData'] != null
? WorkRecordJsonData.fromJson(
json['jsonData'] as Map<String, dynamic>,
)
: null,
imgUrl: json['imgUrl'] as String?,
);
}
static int _parseInt(dynamic value) {
if (value == null) return 0;
if (value is int) return value;
if (value is num) return value.toInt();
if (value is String) return int.tryParse(value) ?? 0;
return 0;
}
}
/// 工作记录JSON数据
class WorkRecordJsonData {
final String? name;
final List<Map<String, double>>? path;
final List<Map<String, double>>? outer;
final String? img;
final int? planModel;
WorkRecordJsonData({
this.name,
this.path,
this.outer,
this.img,
this.planModel,
});
factory WorkRecordJsonData.fromXml(Map<String, dynamic> xmlData) {
return WorkRecordJsonData(
name: xmlData['name'] as String?,
path: _parsePathList(xmlData['path']),
outer: _parseOuterList(xmlData['outer']),
img: xmlData['img'] as String?,
planModel: int.tryParse(xmlData['planModel']?.toString() ?? '0'),
);
}
factory WorkRecordJsonData.fromJson(Map<String, dynamic> json) {
return WorkRecordJsonData(
name: json['name'] as String?,
path: _parsePathListJson(json['path']),
outer: _parseOuterListJson(json['outer']),
img: json['img'] as String?,
planModel: (json['planModel'] as num?)?.toInt(),
);
}
static List<Map<String, double>>? _parsePathList(dynamic pathData) {
if (pathData == null) return null;
// path 可能是 List 或 Map
if (pathData is List) {
return pathData.map((item) {
if (item is Map) {
return {
'lat': double.tryParse(item['lat']?.toString() ?? '0') ?? 0.0,
'lng': double.tryParse(item['lng']?.toString() ?? '0') ?? 0.0,
};
}
return <String, double>{};
}).toList();
} else if (pathData is Map) {
// 如果是单个 Map,检查是否包含 path 子节点
if (pathData.containsKey('path')) {
final pathList = pathData['path'];
if (pathList is List) {
return pathList.map((item) {
if (item is Map) {
return {
'lat': double.tryParse(item['lat']?.toString() ?? '0') ?? 0.0,
'lng': double.tryParse(item['lng']?.toString() ?? '0') ?? 0.0,
};
}
return <String, double>{};
}).toList();
}
}
// 直接是单个 path 节点
return [
{
'lat': double.tryParse(pathData['lat']?.toString() ?? '0') ?? 0.0,
'lng': double.tryParse(pathData['lng']?.toString() ?? '0') ?? 0.0,
},
];
}
return null;
}
static List<Map<String, double>>? _parseOuterList(dynamic outerData) {
if (outerData == null) return null;
// outer 可能是 List 或 Map
if (outerData is List) {
return outerData.map((item) {
if (item is Map) {
return {
'lat': double.tryParse(item['lat']?.toString() ?? '0') ?? 0.0,
'lng': double.tryParse(item['lng']?.toString() ?? '0') ?? 0.0,
};
}
return <String, double>{};
}).toList();
} else if (outerData is Map) {
// 如果是单个 Map,检查是否包含 outer 子节点
if (outerData.containsKey('outer')) {
final outerList = outerData['outer'];
if (outerList is List) {
return outerList.map((item) {
if (item is Map) {
return {
'lat': double.tryParse(item['lat']?.toString() ?? '0') ?? 0.0,
'lng': double.tryParse(item['lng']?.toString() ?? '0') ?? 0.0,
};
}
return <String, double>{};
}).toList();
}
}
// 直接是单个 outer 节点
return [
{
'lat': double.tryParse(outerData['lat']?.toString() ?? '0') ?? 0.0,
'lng': double.tryParse(outerData['lng']?.toString() ?? '0') ?? 0.0,
},
];
}
return null;
}
/// JSON格式解析path
static List<Map<String, double>>? _parsePathListJson(dynamic pathData) {
if (pathData == null) return null;
if (pathData is List) {
return pathData.map((item) {
if (item is Map) {
return {
'lat': (item['lat'] as num?)?.toDouble() ?? 0.0,
'lng': (item['lng'] as num?)?.toDouble() ?? 0.0,
};
}
return <String, double>{};
}).toList();
}
return null;
}
/// JSON格式解析outer
static List<Map<String, double>>? _parseOuterListJson(dynamic outerData) {
if (outerData == null) return null;
if (outerData is List) {
return outerData.map((item) {
if (item is Map) {
return {
'lat': (item['lat'] as num?)?.toDouble() ?? 0.0,
'lng': (item['lng'] as num?)?.toDouble() ?? 0.0,
};
}
return <String, double>{};
}).toList();
}
return null;
}
}

View File

@@ -2,6 +2,7 @@ import 'dart:math';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:xml/xml.dart';
import '../../../../core/di/injection.dart';
import '../../../../core/storage/user_storage.dart';
@@ -9,10 +10,12 @@ import '../../domain/repositories/path_repository.dart';
import '../datasources/path_http_datasource.dart';
import '../models/device_add_path_point_model.dart';
import '../models/device_work_area_param_model.dart';
import '../models/work_record_entity.dart';
class PathRepositoryImpl implements PathRepository {
final PathHttpDatasource _datasource;
PathRepositoryImpl({required PathHttpDatasource datasource}) : _datasource = datasource;
PathRepositoryImpl({required PathHttpDatasource datasource})
: _datasource = datasource;
// 生成路径
@override
Future<List<DeviceAddPathPointModel>> generatePath({
@@ -38,10 +41,18 @@ class PathRepositoryImpl implements PathRepository {
// 保存工作记录
@override
Future<Map<String, dynamic>> saveWorkRecord({required String workName, required String userId, required String jsonData}) async {
Future<Map<String, dynamic>> saveWorkRecord({
required String workName,
required String userId,
required String jsonData,
}) async {
final url = Uri.parse('https://serviceri.satabot.com/iot/workRecord/add');
final headers = {'Content-Type': 'application/json'};
final body = jsonEncode({'workName': workName, 'userId': userId, 'jsonData': jsonData});
final body = jsonEncode({
'workName': workName,
'userId': userId,
'jsonData': jsonData,
});
try {
final response = await http.post(url, headers: headers, body: body);
@@ -57,7 +68,9 @@ class PathRepositoryImpl implements PathRepository {
}
@override
Future<List<Map<String, dynamic>>> getWorkRecord({required String userId}) async {
Future<List<Map<String, dynamic>>> getWorkRecord({
required String userId,
}) async {
final timestamp = DateTime.now().millisecondsSinceEpoch;
final url = Uri.parse(
'https://serviceri.satabot.com/iot/workRecord/selectByUserId',
@@ -73,7 +86,9 @@ class PathRepositoryImpl implements PathRepository {
throw Exception('API error: ${data['msg'] ?? 'Unknown'}');
}
} else {
throw Exception('HTTP ${response.statusCode}: ${response.reasonPhrase}');
throw Exception(
'HTTP ${response.statusCode}: ${response.reasonPhrase}',
);
}
} catch (e) {
throw Exception('Network error in getWorkRecord: $e');
@@ -81,11 +96,16 @@ class PathRepositoryImpl implements PathRepository {
}
@override
Future<Map<String, dynamic>> deleteWorkRecord({required String workName}) async {
Future<Map<String, dynamic>> deleteWorkRecord({
required String workName,
}) async {
final timestamp = DateTime.now().millisecondsSinceEpoch;
final url = Uri.parse(
'https://serviceri.satabot.com/iot/workRecord/deleteByWorkName',
).replace(queryParameters: {'workName': workName, '_t': timestamp.toString()});
final url =
Uri.parse(
'https://serviceri.satabot.com/iot/workRecord/deleteByWorkName',
).replace(
queryParameters: {'workName': workName, '_t': timestamp.toString()},
);
try {
final response = await http.get(url);
@@ -94,7 +114,9 @@ class PathRepositoryImpl implements PathRepository {
if (response.statusCode == 200 && data['code'] == 200) {
return data; // 返回 {"code": 200, "msg": "删除成功"}
} else {
throw Exception('Delete failed: ${data['msg'] ?? response.reasonPhrase}');
throw Exception(
'Delete failed: ${data['msg'] ?? response.reasonPhrase}',
);
}
} catch (e) {
throw Exception('Network error in deleteWorkRecord: $e');
@@ -103,11 +125,16 @@ class PathRepositoryImpl implements PathRepository {
/// 选择工作记录
@override
Future<List<Map<String, dynamic>>> selectWorkRecordByName({required String workName}) async {
Future<List<Map<String, dynamic>>> selectWorkRecordByName({
required String workName,
}) async {
final timestamp = DateTime.now().millisecondsSinceEpoch;
final url = Uri.parse(
'https://serviceri.satabot.com/iot/workRecord/selectByWorkName',
).replace(queryParameters: {'workName': workName, '_t': timestamp.toString()});
final url =
Uri.parse(
'https://serviceri.satabot.com/iot/workRecord/selectByWorkName',
).replace(
queryParameters: {'workName': workName, '_t': timestamp.toString()},
);
try {
final response = await http.get(url);
@@ -131,7 +158,9 @@ class PathRepositoryImpl implements PathRepository {
} else if (rawData is Map) {
records = [Map<String, dynamic>.from(rawData)];
} else {
throw Exception('Unexpected data type for "data": ${rawData.runtimeType}');
throw Exception(
'Unexpected data type for "data": ${rawData.runtimeType}',
);
}
return records;
@@ -139,10 +168,228 @@ class PathRepositoryImpl implements PathRepository {
throw Exception('API error: ${data['msg'] ?? 'Unknown'}');
}
} else {
throw Exception('HTTP ${response.statusCode}: ${response.reasonPhrase}');
throw Exception(
'HTTP ${response.statusCode}: ${response.reasonPhrase}',
);
}
} catch (e) {
throw Exception('Network error in selectWorkRecordByName: $e');
}
}
/// 根据场站ID查询工作记录列表(XML格式)
@override
Future<List<WorkRecordEntity>> getWorkRecordsBySiteId({
required int siteId,
}) async {
final timestamp = DateTime.now().millisecondsSinceEpoch;
final url =
Uri.parse(
'http://1.95.137.212:59015/iot/workRecord/selectBySiteId',
).replace(
queryParameters: {
'siteId': siteId.toString(),
'_t': timestamp.toString(),
},
);
try {
final response = await http.get(
url,
headers: {'Accept': 'application/xml, text/xml, */*'},
);
print('[XML接口] 响应状态码: ${response.statusCode}');
print('[XML接口] 响应内容长度: ${response.body.length}');
print('[XML接口] Content-Type: ${response.headers['content-type']}');
if (response.statusCode == 200) {
// 打印前200字符确认格式
final preview = response.body.length > 200
? response.body.substring(0, 200)
: response.body;
print('[XML接口] 响应开头: $preview');
// 判断是JSON还是XML格式
final trimmed = response.body.trim();
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
print('[XML接口] 检测到JSON格式,使用JSON解析');
return _parseJsonResponse(response.body);
} else {
print('[XML接口] 检测到XML格式,使用XML解析');
return _parseXmlResponse(response.body);
}
} else {
throw Exception(
'HTTP ${response.statusCode}: ${response.reasonPhrase}',
);
}
} catch (e) {
print('[XML接口] 错误: $e');
throw Exception('Network error in getWorkRecordsBySiteId: $e');
}
}
/// 解析JSON格式响应
Future<List<WorkRecordEntity>> _parseJsonResponse(String body) async {
final data = jsonDecode(body) as Map<String, dynamic>;
if (data['code'] != 200) {
throw Exception('API error: ${data['msg'] ?? 'Unknown'}');
}
final recordsData = data['data'];
if (recordsData == null) return [];
final List<WorkRecordEntity> records = [];
if (recordsData is List) {
for (final item in recordsData) {
if (item is Map<String, dynamic>) {
records.add(WorkRecordEntity.fromJson(item));
}
}
} else if (recordsData is Map<String, dynamic>) {
records.add(WorkRecordEntity.fromJson(recordsData));
}
print('[XML接口] 最终解析记录数: ${records.length}');
return records;
}
/// 解析XML格式响应
Future<List<WorkRecordEntity>> _parseXmlResponse(String body) async {
// 用更宽松的正则检查响应码(支持命名空间前缀)
final codeMatch = RegExp(
r'<\w*:?code[^>]*>(\d+)</\w*:?code>',
).firstMatch(body);
final code = codeMatch?.group(1);
print('[XML接口] code: $code');
if (code != '200') {
final msgMatch = RegExp(
r'<\w*:?msg[^>]*>(.*?)</\w*:?msg>',
).firstMatch(body);
throw Exception('API error: ${msgMatch?.group(1) ?? 'Unknown'}');
}
// 使用正则表达式提取所有<data>...</data>节点
// 使用非贪婪匹配,确保每个data节点独立提取
final dataRegex = RegExp(r'<data>([\s\S]*?)</data>');
final dataMatches = dataRegex.allMatches(body);
print('[XML接口] 找到data节点数量: ${dataMatches.length}');
// 解析所有工作记录
final List<WorkRecordEntity> records = [];
for (int i = 0; i < dataMatches.length; i++) {
final match = dataMatches.elementAt(i);
final dataContent = match.group(1)!; // 获取<data>和</data>之间的内容
try {
// 将提取的内容包装成完整XML进行解析
final wrappedXml = '<root>$dataContent</root>';
final document = XmlDocument.parse(wrappedXml);
final recordElement = document.rootElement;
final recordData = _parseXmlRecord(recordElement);
print(
'[XML接口] data[$i] 解析结果: workName=${recordData['workName']}, id=${recordData['id']}, imgUrl=${recordData['imgUrl']}',
);
records.add(WorkRecordEntity.fromXml(recordData));
} catch (e) {
print('[XML接口] 解析单个data节点失败: $e');
}
}
print('[XML接口] 最终解析记录数: ${records.length}');
return records;
}
/// 解析XML工作记录节点
Map<String, dynamic> _parseXmlRecord(XmlElement recordElement) {
final Map<String, dynamic> result = {};
print('[XML解析] 开始解析节点,子元素数量: ${recordElement.childElements.length}');
for (final child in recordElement.childElements) {
final tagName = child.name.local;
final innerText = child.innerText.trim();
print(
'[XML解析] 标签: $tagName, 值: ${innerText.length > 50 ? innerText.substring(0, 50) + '...' : innerText}',
);
// 特殊处理jsonData节点(包含嵌套结构)
if (tagName == 'jsonData') {
result['jsonData'] = _parseJsonDataNode(child);
} else {
// 普通节点直接取值
result[tagName] = innerText;
}
}
print('[XML解析] 解析完成,结果keys: ${result.keys.toList()}');
return result;
}
/// 解析jsonData节点
Map<String, dynamic> _parseJsonDataNode(XmlElement jsonDataElement) {
final Map<String, dynamic> result = {};
for (final child in jsonDataElement.childElements) {
final tagName = child.name.local;
if (tagName == 'path' || tagName == 'outer') {
// path和outer可能包含嵌套的path/outer节点
result[tagName] = _parseCoordinateList(child, tagName);
} else {
// 普通字段(name, img, planModel等)
result[tagName] = child.innerText.trim();
}
}
return result;
}
/// 解析坐标列表(path或outer)
List<Map<String, double>> _parseCoordinateList(
XmlElement element,
String tagName,
) {
final List<Map<String, double>> coordinates = [];
// 检查是否有嵌套的同名节点
final nestedElements = element.childElements
.where((e) => e.name.local == tagName)
.toList();
if (nestedElements.isNotEmpty) {
// 有嵌套结构:outer > outer > {lat, lng}
for (final nestedElement in nestedElements) {
final latElement = nestedElement.getElement('lat');
final lngElement = nestedElement.getElement('lng');
if (latElement != null && lngElement != null) {
coordinates.add({
'lat': double.tryParse(latElement.innerText.trim()) ?? 0.0,
'lng': double.tryParse(lngElement.innerText.trim()) ?? 0.0,
});
}
}
} else {
// 直接包含lat/lng节点
final latElement = element.getElement('lat');
final lngElement = element.getElement('lng');
if (latElement != null && lngElement != null) {
coordinates.add({
'lat': double.tryParse(latElement.innerText.trim()) ?? 0.0,
'lng': double.tryParse(lngElement.innerText.trim()) ?? 0.0,
});
}
}
return coordinates;
}
}

View File

@@ -1,5 +1,6 @@
import '../../data/models/device_add_path_point_model.dart';
import '../../data/models/device_work_area_param_model.dart';
import '../../data/models/work_record_entity.dart';
abstract class PathRepository {
// Generate path(打点生成路径规划)
@@ -20,6 +21,9 @@ abstract class PathRepository {
// Delete work record (删除)
Future<Map<String, dynamic>> deleteWorkRecord({required String workName});
/// 根据作业名查询路径记录(用于“选择一个路径”)
/// 根据作业名查询路径记录(用于"选择一个路径")
Future<List<Map<String, dynamic>>> selectWorkRecordByName({required String workName});
/// 根据场站ID查询工作记录列表(XML格式)
Future<List<WorkRecordEntity>> getWorkRecordsBySiteId({required int siteId});
}

View File

@@ -0,0 +1,19 @@
import 'package:fpdart/fpdart.dart';
import '../../data/models/work_record_entity.dart';
import '../../domain/errors/device_failure.dart';
import '../../domain/repositories/path_repository.dart';
class GetWorkRecordsBySiteIdUseCase {
final PathRepository repository;
GetWorkRecordsBySiteIdUseCase(this.repository);
Future<Either<DeviceFailure, List<WorkRecordEntity>>> call(int siteId) async {
try {
final result = await repository.getWorkRecordsBySiteId(siteId: siteId);
return Right(result);
} catch (e) {
return Left(DeviceFailure.networkError(message: e.toString()));
}
}
}

View File

@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:collection';
import 'package:flutter/rendering.dart';
@@ -7,6 +8,7 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/generate_path_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/get_user_device_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/data/models/work_record_entity.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/save_work_record_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/select_work_record_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
@@ -21,6 +23,7 @@ import '../../domain/usecases/bind_device_usecase.dart';
import '../../domain/usecases/delete_work_record_usecase.dart';
import '../../domain/usecases/get_device_location_usecase.dart';
import '../../domain/usecases/get_work_record_usecase.dart';
import '../../domain/usecases/get_work_records_by_site_id_usecase.dart';
import '../../domain/usecases/route_planning_usecase.dart';
import '../../services/path_planning_service.dart';
import 'device_status_bloc.dart';
@@ -32,6 +35,7 @@ class DevicesCubit extends Cubit<DevicesState> {
final GetDeviceLocationUseCase _getDeviceLocationUseCase;
final DeviceRepository repository;
final GetWorkRecordUseCase _getWorkRecordUseCase;
final GetWorkRecordsBySiteIdUseCase _getWorkRecordsBySiteIdUseCase;
final DeleteWorkRecordUseCase _deleteWorkRecordUseCase;
final BindDeviceUseCase _bindDeviceUseCase;
final UnbindDeviceUseCase _unbindDeviceUseCase;
@@ -52,6 +56,7 @@ class DevicesCubit extends Cubit<DevicesState> {
this._getUserDeviceUseCase,
this._getDeviceLocationUseCase,
this._getWorkRecordUseCase,
this._getWorkRecordsBySiteIdUseCase,
this._deleteWorkRecordUseCase,
this._unbindDeviceUseCase,
this._updateDevicename,
@@ -66,7 +71,13 @@ class DevicesCubit extends Cubit<DevicesState> {
) : super(const DevicesState());
Future<void> unbindDevice(String deviceId, String deviceName) async {
emit(state.copyWith(isLoading: true, errorMessage: '', operationType: DeviceOperationType.unbind));
emit(
state.copyWith(
isLoading: true,
errorMessage: '',
operationType: DeviceOperationType.unbind,
),
);
try {
final params = UnbindDeviceParams(deviceId, deviceName);
@@ -92,55 +103,94 @@ class DevicesCubit extends Cubit<DevicesState> {
state.copyWith(
isLoading: false,
devices: updatedDevices,
selectedDevice: state.selectedDevice?.deviceName == deviceId ? null : state.selectedDevice,
selectedDevice: state.selectedDevice?.deviceName == deviceId
? null
: state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(state.copyWith(isLoading: false, errorMessage: '解绑失败:状态码 $successCode', operationType: DeviceOperationType.none));
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑失败:状态码 $successCode',
operationType: DeviceOperationType.none,
),
);
}
},
);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}', operationType: DeviceOperationType.none));
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑异常:${e.toString()}',
operationType: DeviceOperationType.none,
),
);
}
}
Future<void> updateDeviceName(String deviceId, String deviceName) async {
emit(state.copyWith(isLoading: true, errorMessage: '', operationType: DeviceOperationType.updateName));
emit(
state.copyWith(
isLoading: true,
errorMessage: '',
operationType: DeviceOperationType.updateName,
),
);
try {
final params = UpdateDevicenameParams(deviceId, deviceName);
final result = await _updateDevicename.call(params);
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '更新设备名称失败', operationType: DeviceOperationType.none)), (
successCode,
) {
// 核心修复:int 转 bool 条件判断
//print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
_logger.logWithLevel('更新设备名称结果代码: $successCode');
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
//final updatedDevices = state.devices?.map((device) {
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
//}).toList();
result.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '更新设备名称失败',
operationType: DeviceOperationType.none,
),
),
(successCode) {
// 核心修复:int 转 bool 条件判断
//print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
_logger.logWithLevel('更新设备名称结果代码: $successCode');
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
//final updatedDevices = state.devices?.map((device) {
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
//}).toList();
emit(
state.copyWith(
isLoading: false,
//devices: updatedDevices,
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(state.copyWith(isLoading: false, errorMessage: '更新设备名称失败:状态码 $successCode', operationType: DeviceOperationType.none));
}
});
emit(
state.copyWith(
isLoading: false,
//devices: updatedDevices,
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(
state.copyWith(
isLoading: false,
errorMessage: '更新设备名称失败:状态码 $successCode',
operationType: DeviceOperationType.none,
),
);
}
},
);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: '更新设备名称异常:${e.toString()}', operationType: DeviceOperationType.none));
emit(
state.copyWith(
isLoading: false,
errorMessage: '更新设备名称异常:${e.toString()}',
operationType: DeviceOperationType.none,
),
);
}
}
@@ -154,32 +204,39 @@ class DevicesCubit extends Cubit<DevicesState> {
final String? oldSelectedDeviceName = state.selectedDevice?.deviceName;
// 网络请求获取新列表
var resultEither = await _getUserDeviceUseCase.call(GetUserDeviceParams(username));
var resultEither = await _getUserDeviceUseCase.call(
GetUserDeviceParams(username),
);
resultEither.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)), (deviceList) {
// 🔥 关键步骤2:匹配新列表中对应的旧选中设备
DeviceEntity? newSelectedDevice;
if (oldSelectedDeviceName != null && deviceList.isNotEmpty) {
// 在新列表中查找和旧选中设备名称一致的设备
newSelectedDevice = deviceList.firstWhere(
(device) => device.deviceName == oldSelectedDeviceName,
// 如果找不到(如设备已解绑),返回 null
orElse: () => deviceList.first, // 兜底:选中第一个
resultEither.fold(
(failure) => emit(
state.copyWith(isLoading: false, errorMessage: failure.message),
),
(deviceList) {
// 🔥 关键步骤2:匹配新列表中对应的旧选中设备
DeviceEntity? newSelectedDevice;
if (oldSelectedDeviceName != null && deviceList.isNotEmpty) {
// 在新列表中查找和旧选中设备名称一致的设备
newSelectedDevice = deviceList.firstWhere(
(device) => device.deviceName == oldSelectedDeviceName,
// 如果找不到(如设备已解绑),返回 null
orElse: () => deviceList.first, // 兜底:选中第一个
);
} else {
// 无旧选中设备,默认选中第一个
newSelectedDevice = deviceList.isNotEmpty ? deviceList.first : null;
}
// 🔥 关键步骤3:更新状态,使用匹配后的选中设备
emit(
state.copyWith(
devices: deviceList,
selectedDevice: newSelectedDevice, // 保留旧选中设备
isLoading: false,
),
);
} else {
// 无旧选中设备,默认选中第一个
newSelectedDevice = deviceList.isNotEmpty ? deviceList.first : null;
}
// 🔥 关键步骤3:更新状态,使用匹配后的选中设备
emit(
state.copyWith(
devices: deviceList,
selectedDevice: newSelectedDevice, // 保留旧选中设备
isLoading: false,
),
);
});
},
);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
}
@@ -197,7 +254,10 @@ class DevicesCubit extends Cubit<DevicesState> {
}).toList();
// 如果更新的是当前选中的设备,也要同步更新 selectedDevice
final newSelected = state.selectedDevice?.deviceName == updatedDevice.deviceName ? updatedDevice : state.selectedDevice;
final newSelected =
state.selectedDevice?.deviceName == updatedDevice.deviceName
? updatedDevice
: state.selectedDevice;
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
}
@@ -233,7 +293,11 @@ class DevicesCubit extends Cubit<DevicesState> {
// connect 方法内部会自动调用 _sendAuthPacket -> 获取设备列表 -> 自动订阅当前选中的设备
// debugPrint('🔌 开始重新新连接 TCP,将自动订阅新设备:${device.deviceName}');
_logger.logWithLevel('🔌 启动重新新连接 TCP,将自动订阅新设备:${device.deviceName}');
await _tcpClient.connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: device.deviceName);
await _tcpClient.connectBySwitch(
host: TCPConsts.TCP_IP,
port: TCPConsts.TCP_PORT,
deviceName: device.deviceName,
);
// 🔥 关键修复 3:重置设备状态 Bloc,清除旧设备图表数据
_deviceStatusBloc.add(DeviceStatusReset());
@@ -257,7 +321,12 @@ class DevicesCubit extends Cubit<DevicesState> {
result.fold(
// 失败处理
(failure) {
emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '绑定设备失败'));
emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '绑定设备失败',
),
);
// 抛出异常,携带后端返回的错误消息(如“设备不存在”)
throw Exception(failure.message ?? '绑定设备失败');
},
@@ -268,7 +337,12 @@ class DevicesCubit extends Cubit<DevicesState> {
// 绑定成功后刷新设备列表
//fetchAllDevices(state.?.username ?? '');
} else {
emit(state.copyWith(isLoading: false, errorMessage: '绑定失败:状态码 $successCode'));
emit(
state.copyWith(
isLoading: false,
errorMessage: '绑定失败:状态码 $successCode',
),
);
}
},
);
@@ -287,8 +361,15 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _getDeviceLocationUseCase.call(device.deviceName);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(state.copyWith(isLoading: false, deviceLatitude: location.latitude, deviceLongitude: location.longitude)),
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(
state.copyWith(
isLoading: false,
deviceLatitude: location.latitude,
deviceLongitude: location.longitude,
),
),
);
}
@@ -297,18 +378,71 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _getWorkRecordUseCase(userId);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
);
}
/// 根据场站ID获取工作记录(XML格式接口)
Future<void> loadWorkRecordsBySiteId(int siteId) async {
print('🔍 [DevicesCubit] 开始加载场站ID=$siteId的工作记录');
emit(state.copyWith(isLoading: true));
final result = await _getWorkRecordsBySiteIdUseCase(siteId);
result.fold(
(failure) {
print('❌ [DevicesCubit] 加载失败: ${failure.message}');
emit(state.copyWith(isLoading: false, errorMessage: failure.message));
},
(records) {
print('🔍 [DevicesCubit] 加载成功,记录数: ${records.length}');
// 将 WorkRecordEntity 转换为 Map<String, dynamic> 以兼容现有UI
final mappedRecords = records.map((record) {
return <String, dynamic>{
'id': record.id.toString(),
'workName': record.workName,
'imgUrl': record.imgUrl ?? '',
'jsonData': record.jsonData != null
? _workRecordJsonDataToJson(record.jsonData!)
: null,
};
}).toList();
print('🔍 [DevicesCubit] 转换后的数据: $mappedRecords');
emit(state.copyWith(isLoading: false, workRecords: mappedRecords));
},
);
}
/// 将 WorkRecordJsonData 转换为 JSON 字符串
String _workRecordJsonDataToJson(dynamic jsonData) {
// 将 jsonData 对象序列化为 JSON 字符串供UI使用
if (jsonData is WorkRecordJsonData) {
return jsonEncode({
'name': jsonData.name,
'path': jsonData.path,
'outer': jsonData.outer,
'img': jsonData.img,
'planModel': jsonData.planModel,
});
}
return jsonData.toString();
}
/// 删除工作记录
Future<void> deleteWorkRecord(String workName) async {
emit(state.copyWith(isLoading: true));
final result = await _deleteWorkRecordUseCase(workName);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(state.copyWith(isLoading: false, workRecords: state.workRecords?.where((record) => record != workName).toList())),
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(
state.copyWith(
isLoading: false,
workRecords: state.workRecords
?.where((record) => record != workName)
.toList(),
),
),
);
}
@@ -318,7 +452,9 @@ class DevicesCubit extends Cubit<DevicesState> {
try {
final result = await _selectWorkRecordUseCase.call(workName);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(failure) => emit(
state.copyWith(isLoading: false, errorMessage: failure.message),
),
(data) => emit(state.copyWith(isLoading: false, pathData: data)),
);
} catch (e) {
@@ -327,10 +463,22 @@ class DevicesCubit extends Cubit<DevicesState> {
}
/// 保存路径数据
Future<void> saveWorkRecord(String workName, String userId, String jsonData) async {
Future<void> saveWorkRecord(
String workName,
String userId,
String jsonData,
) async {
emit(state.copyWith(isLoading: true));
final result = await _saveWorkRecordUseCase.call(workName: workName, userId: userId, jsonData: jsonData);
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)), (data) => emit(state.copyWith(isLoading: false)));
final result = await _saveWorkRecordUseCase.call(
workName: workName,
userId: userId,
jsonData: jsonData,
);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(data) => emit(state.copyWith(isLoading: false)),
);
}
/// generatePath
@@ -346,13 +494,24 @@ class DevicesCubit extends Cubit<DevicesState> {
// debugPrint('第${index + 1}组holes:$pointsStr');
//});
emit(state.copyWith(isLoading: true));
final result = await _generatePathUseCase.execute(reference: reference, heading: heading, outer: outer, holes: holes, workType: workType);
final result = await _generatePathUseCase.execute(
reference: reference,
heading: heading,
outer: outer,
holes: holes,
workType: workType,
);
result.fold((failure) => emit(state.copyWith(errorMessage: failure.message)), (pathData) => emit(state.copyWith(generatedPath: pathData)));
result.fold(
(failure) => emit(state.copyWith(errorMessage: failure.message)),
(pathData) => emit(state.copyWith(generatedPath: pathData)),
);
}
// 开始路径规划
Future<void> startRoutePlanning(Queue<DeviceAddPathPointModel> locationQueue) async {
Future<void> startRoutePlanning(
Queue<DeviceAddPathPointModel> locationQueue,
) async {
/// print("cubit层开始路径规划");
_logger.logWithLevel('开始路径规划');
// 清空全局 Service 中的队列
@@ -373,7 +532,12 @@ class DevicesCubit extends Cubit<DevicesState> {
_logger.logWithLevel('从 Service 获取的队列长度:${queue.length}');
final result = await _routePlanningUseCase.startRoutePlanning(queue);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '路径规划启动失败')),
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '路径规划启动失败',
),
),
(_) => emit(state.copyWith(isLoading: false, errorMessage: '')),
);
}
@@ -382,14 +546,30 @@ class DevicesCubit extends Cubit<DevicesState> {
Future<void> pauseRoutePlanning() async {
emit(state.copyWith(isLoading: true));
final result = await _routePlanningUseCase.pauseRPWork();
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '暂停失败')), (_) => emit(state.copyWith(isLoading: false)));
result.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '暂停失败',
),
),
(_) => emit(state.copyWith(isLoading: false)),
);
}
// 恢复
Future<void> resumeRoutePlanning() async {
emit(state.copyWith(isLoading: true));
final result = await _routePlanningUseCase.resumeRPWork();
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '恢复失败')), (_) => emit(state.copyWith(isLoading: false)));
result.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '恢复失败',
),
),
(_) => emit(state.copyWith(isLoading: false)),
);
}
// 停止
@@ -399,7 +579,15 @@ class DevicesCubit extends Cubit<DevicesState> {
// 🔥 重置 PathPlanningService 中的全局的队列
_pathPlanningService.clear();
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '停止失败')), (_) => emit(state.copyWith(isLoading: false)));
result.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '停止失败',
),
),
(_) => emit(state.copyWith(isLoading: false)),
);
}
void updateAppState(AppState appState) {
@@ -419,7 +607,9 @@ class DevicesCubit extends Cubit<DevicesState> {
void setArrivedLocation(double latitude, double longitude) {
emit(state.copyWith(arriLatitude: latitude, arriLongitude: longitude));
// print('✅ [DevicesCubit] 监听更新已完成到达位置:Lat=$latitude, Lng=$longitude');
_logger.logWithLevel('✅ [DevicesCubit] 监听更新已完成到达位置:Lat=$latitude, Lng=$longitude');
_logger.logWithLevel(
'✅ [DevicesCubit] 监听更新已完成到达位置:Lat=$latitude, Lng=$longitude',
);
}
//获取已到达的点的经纬度