集成接口 开始执行任务的领域层和数据层和UI层的开发啊(待测试)
集成功能 暂停 取消功能 恢复功能的接口的领域层和数据层的开发 下一步待集成到页面上 优化功能,优化了接口异常和未知异常对页面渲染的影响对用户的体验的不好情况。具体通过弹窗友好提示! 优化更新了tcp指示灯点击出现机器状态中添加选中设备的编号 方便用户使用的明白明了!。 优化更新关闭了tcp重连操作内链条中的获取用户设备和切换设备操作项。 调整了获取无人机状态信息的更新频率 为14秒一次
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../models/device_task_model.dart';
|
||||
|
||||
abstract class DeviceTaskDatasource {
|
||||
Future<List<DeviceTaskModel>> getDeviceTaskPool({
|
||||
required String userId,
|
||||
required int siteId,
|
||||
required int orgId,
|
||||
int pageNum = 1,
|
||||
int pageSize = 99999999,
|
||||
});
|
||||
|
||||
Future<bool> cancelTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
});
|
||||
|
||||
Future<bool> pauseTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
});
|
||||
|
||||
Future<Map<String, dynamic>> recoveryTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
});
|
||||
}
|
||||
|
||||
class DeviceTaskDatasourceImpl implements DeviceTaskDatasource {
|
||||
final Dio dio;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
DeviceTaskDatasourceImpl(this.dio);
|
||||
|
||||
@override
|
||||
Future<List<DeviceTaskModel>> getDeviceTaskPool({
|
||||
required String userId,
|
||||
required int siteId,
|
||||
required int orgId,
|
||||
int pageNum = 1,
|
||||
int pageSize = 99999999,
|
||||
}) async {
|
||||
try {
|
||||
final url = 'http://1.95.137.212:59015/iot/deviceTask/deviceTaskPool';
|
||||
final response = await dio.get(
|
||||
url,
|
||||
queryParameters: {
|
||||
'userId': userId,
|
||||
'siteId': siteId,
|
||||
'orgId': orgId,
|
||||
'pageNum': pageNum,
|
||||
'pageSize': pageSize,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
if (data['code'] == 200) {
|
||||
final rows = data['rows'] as List? ?? [];
|
||||
return rows
|
||||
.map((item) => DeviceTaskModel.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
} else {
|
||||
throw Exception(data['msg'] ?? '获取任务池失败');
|
||||
}
|
||||
} else {
|
||||
throw Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 获取任务池失败: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> cancelTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
final url = 'http://1.95.137.212:59015/iot/deviceTask/cancelTask';
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: {
|
||||
'deviceId': deviceId,
|
||||
'taskId': taskId,
|
||||
'orgId': orgId,
|
||||
'siteId': siteId,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
if (data['code'] == 200) {
|
||||
return data['data'] as bool? ?? false;
|
||||
} else {
|
||||
throw Exception(data['msg'] ?? '取消任务失败');
|
||||
}
|
||||
} else {
|
||||
throw Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 取消任务失败: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> pauseTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
final url = 'http://1.95.137.212:59015/iot/deviceTask/pauseTask';
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: {
|
||||
'deviceId': deviceId,
|
||||
'taskId': taskId,
|
||||
'orgId': orgId,
|
||||
'siteId': siteId,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
if (data['code'] == 200) {
|
||||
return data['data'] as bool? ?? false;
|
||||
} else {
|
||||
throw Exception(data['msg'] ?? '暂停任务失败');
|
||||
}
|
||||
} else {
|
||||
throw Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 暂停任务失败: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> recoveryTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
final url = 'http://1.95.137.212:59015/iot/deviceTask/recoveryTask';
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: {
|
||||
'deviceId': deviceId,
|
||||
'taskId': taskId,
|
||||
'orgId': orgId,
|
||||
'siteId': siteId,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
if (data['code'] == 200) {
|
||||
return data['data'] as Map<String, dynamic>? ?? {};
|
||||
} else {
|
||||
throw Exception(data['msg'] ?? '恢复任务失败');
|
||||
}
|
||||
} else {
|
||||
throw Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 恢复任务失败: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
40
lib/features/devices/data/models/device_task_model.dart
Normal file
40
lib/features/devices/data/models/device_task_model.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import '../../domain/entities/device_task_entity.dart';
|
||||
|
||||
class DeviceTaskModel extends DeviceTaskEntity {
|
||||
const DeviceTaskModel({
|
||||
required super.id,
|
||||
required super.deviceId,
|
||||
required super.taskStatus,
|
||||
required super.taskStatusTranslate,
|
||||
super.routeId,
|
||||
super.siteId,
|
||||
super.orgId,
|
||||
super.createTime,
|
||||
});
|
||||
|
||||
factory DeviceTaskModel.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceTaskModel(
|
||||
id: json['id'] as int,
|
||||
deviceId: json['deviceId'] as String? ?? '',
|
||||
taskStatus: json['taskStaus'] as String? ?? '',
|
||||
taskStatusTranslate: json['taskStausTranslate'] as String? ?? '',
|
||||
routeId: json['routeId'] as int?,
|
||||
siteId: json['siteId'] as int?,
|
||||
orgId: json['orgId'] as int?,
|
||||
createTime: json['createTime'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
DeviceTaskEntity toEntity() {
|
||||
return DeviceTaskEntity(
|
||||
id: id,
|
||||
deviceId: deviceId,
|
||||
taskStatus: taskStatus,
|
||||
taskStatusTranslate: taskStatusTranslate,
|
||||
routeId: routeId,
|
||||
siteId: siteId,
|
||||
orgId: orgId,
|
||||
createTime: createTime,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../../domain/entities/device_task_entity.dart';
|
||||
import '../../domain/repositories/device_task_repository.dart';
|
||||
import '../datasources/device_task_datasource.dart';
|
||||
|
||||
class DeviceTaskRepositoryImpl implements DeviceTaskRepository {
|
||||
final DeviceTaskDatasource datasource;
|
||||
|
||||
DeviceTaskRepositoryImpl({required this.datasource});
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<DeviceTaskEntity>>> getDeviceTaskPool({
|
||||
required String userId,
|
||||
required int siteId,
|
||||
required int orgId,
|
||||
int pageNum = 1,
|
||||
int pageSize = 99999999,
|
||||
}) async {
|
||||
try {
|
||||
final models = await datasource.getDeviceTaskPool(
|
||||
userId: userId,
|
||||
siteId: siteId,
|
||||
orgId: orgId,
|
||||
pageNum: pageNum,
|
||||
pageSize: pageSize,
|
||||
);
|
||||
return Right(models.map((model) => model.toEntity()).toList());
|
||||
} catch (e) {
|
||||
return Left(Failure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, bool>> cancelTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await datasource.cancelTask(
|
||||
deviceId: deviceId,
|
||||
taskId: taskId,
|
||||
orgId: orgId,
|
||||
siteId: siteId,
|
||||
);
|
||||
return Right(result);
|
||||
} catch (e) {
|
||||
return Left(Failure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, bool>> pauseTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await datasource.pauseTask(
|
||||
deviceId: deviceId,
|
||||
taskId: taskId,
|
||||
orgId: orgId,
|
||||
siteId: siteId,
|
||||
);
|
||||
return Right(result);
|
||||
} catch (e) {
|
||||
return Left(Failure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, Map<String, dynamic>>> recoveryTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await datasource.recoveryTask(
|
||||
deviceId: deviceId,
|
||||
taskId: taskId,
|
||||
orgId: orgId,
|
||||
siteId: siteId,
|
||||
);
|
||||
return Right(result);
|
||||
} catch (e) {
|
||||
return Left(Failure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,24 +198,24 @@ class PathRepositoryImpl implements PathRepository {
|
||||
url,
|
||||
headers: {'Accept': 'application/xml, text/xml, */*'},
|
||||
);
|
||||
print('[XML接口] 响应状态码: ${response.statusCode}');
|
||||
print('[XML接口] 响应内容长度: ${response.body.length}');
|
||||
print('[XML接口] Content-Type: ${response.headers['content-type']}');
|
||||
/// 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');
|
||||
/// print('[XML接口] 响应开头: $preview');
|
||||
|
||||
// 判断是JSON还是XML格式
|
||||
final trimmed = response.body.trim();
|
||||
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
||||
print('[XML接口] 检测到JSON格式,使用JSON解析');
|
||||
///print('[XML接口] 检测到JSON格式,使用JSON解析');
|
||||
return _parseJsonResponse(response.body);
|
||||
} else {
|
||||
print('[XML接口] 检测到XML格式,使用XML解析');
|
||||
///print('[XML接口] 检测到XML格式,使用XML解析');
|
||||
return _parseXmlResponse(response.body);
|
||||
}
|
||||
} else {
|
||||
@@ -224,7 +224,7 @@ class PathRepositoryImpl implements PathRepository {
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print('[XML接口] 错误: $e');
|
||||
/// print('[XML接口] 错误: $e');
|
||||
throw Exception('Network error in getWorkRecordsBySiteId: $e');
|
||||
}
|
||||
}
|
||||
@@ -252,7 +252,7 @@ class PathRepositoryImpl implements PathRepository {
|
||||
records.add(WorkRecordEntity.fromJson(recordsData));
|
||||
}
|
||||
|
||||
print('[XML接口] 最终解析记录数: ${records.length}');
|
||||
/// print('[XML接口] 最终解析记录数: ${records.length}');
|
||||
return records;
|
||||
}
|
||||
|
||||
@@ -276,7 +276,7 @@ class PathRepositoryImpl implements PathRepository {
|
||||
// 使用非贪婪匹配,确保每个data节点独立提取
|
||||
final dataRegex = RegExp(r'<data>([\s\S]*?)</data>');
|
||||
final dataMatches = dataRegex.allMatches(body);
|
||||
print('[XML接口] 找到data节点数量: ${dataMatches.length}');
|
||||
///print('[XML接口] 找到data节点数量: ${dataMatches.length}');
|
||||
|
||||
// 解析所有工作记录
|
||||
final List<WorkRecordEntity> records = [];
|
||||
@@ -292,9 +292,9 @@ class PathRepositoryImpl implements PathRepository {
|
||||
final recordElement = document.rootElement;
|
||||
|
||||
final recordData = _parseXmlRecord(recordElement);
|
||||
print(
|
||||
'[XML接口] data[$i] 解析结果: workName=${recordData['workName']}, id=${recordData['id']}, imgUrl=${recordData['imgUrl']}',
|
||||
);
|
||||
/// print(
|
||||
/// '[XML接口] data[$i] 解析结果: workName=${recordData['workName']}, id=${recordData['id']}, imgUrl=${recordData['imgUrl']}',
|
||||
/// );
|
||||
|
||||
records.add(WorkRecordEntity.fromXml(recordData));
|
||||
} catch (e) {
|
||||
@@ -302,7 +302,7 @@ class PathRepositoryImpl implements PathRepository {
|
||||
}
|
||||
}
|
||||
|
||||
print('[XML接口] 最终解析记录数: ${records.length}');
|
||||
///print('[XML接口] 最终解析记录数: ${records.length}');
|
||||
return records;
|
||||
}
|
||||
|
||||
@@ -310,15 +310,15 @@ class PathRepositoryImpl implements PathRepository {
|
||||
Map<String, dynamic> _parseXmlRecord(XmlElement recordElement) {
|
||||
final Map<String, dynamic> result = {};
|
||||
|
||||
print('[XML解析] 开始解析节点,子元素数量: ${recordElement.childElements.length}');
|
||||
//// 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}',
|
||||
);
|
||||
// print(
|
||||
// '[XML解析] 标签: $tagName, 值: ${innerText.length > 50 ? innerText.substring(0, 50) + '...' : innerText}',
|
||||
// );
|
||||
|
||||
// 特殊处理jsonData节点(包含嵌套结构)
|
||||
if (tagName == 'jsonData') {
|
||||
@@ -329,7 +329,7 @@ class PathRepositoryImpl implements PathRepository {
|
||||
}
|
||||
}
|
||||
|
||||
print('[XML解析] 解析完成,结果keys: ${result.keys.toList()}');
|
||||
//print('[XML解析] 解析完成,结果keys: ${result.keys.toList()}');
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -392,4 +392,42 @@ class PathRepositoryImpl implements PathRepository {
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
/// 创建设备任务(通过接口执行作业)
|
||||
/// 接口地址: http://1.95.137.212:59015/iot/deviceTask/createDeviceTask
|
||||
/// 入参: {"deviceId":"...","routeId":76,"siteId":22}
|
||||
@override
|
||||
Future<void> createDeviceTask({
|
||||
required String deviceId,
|
||||
required int routeId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
final url = Uri.parse('http://1.95.137.212:59015/iot/deviceTask/createDeviceTask');
|
||||
|
||||
final body = jsonEncode({
|
||||
'deviceId': deviceId,
|
||||
'routeId': routeId,
|
||||
'siteId': siteId,
|
||||
});
|
||||
|
||||
print('📤 [创建设备任务] 请求参数: $body');
|
||||
|
||||
try {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: body,
|
||||
);
|
||||
|
||||
print('📥 [创建设备任务] 响应状态码: ${response.statusCode}');
|
||||
print('📥 [创建设备任务] 响应内容: ${response.body}');
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('创建设备任务失败: HTTP ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('❌ [创建设备任务] 错误: $e');
|
||||
throw Exception('创建设备任务失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
48
lib/features/devices/domain/entities/device_task_entity.dart
Normal file
48
lib/features/devices/domain/entities/device_task_entity.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class DeviceTaskEntity extends Equatable {
|
||||
final int id;
|
||||
final String deviceId;
|
||||
final String taskStatus;
|
||||
final String taskStatusTranslate;
|
||||
final int? routeId;
|
||||
final int? siteId;
|
||||
final int? orgId;
|
||||
final String? createTime;
|
||||
|
||||
const DeviceTaskEntity({
|
||||
required this.id,
|
||||
required this.deviceId,
|
||||
required this.taskStatus,
|
||||
required this.taskStatusTranslate,
|
||||
this.routeId,
|
||||
this.siteId,
|
||||
this.orgId,
|
||||
this.createTime,
|
||||
});
|
||||
|
||||
factory DeviceTaskEntity.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceTaskEntity(
|
||||
id: json['id'] as int,
|
||||
deviceId: json['deviceId'] as String? ?? '',
|
||||
taskStatus: json['taskStaus'] as String? ?? '',
|
||||
taskStatusTranslate: json['taskStausTranslate'] as String? ?? '',
|
||||
routeId: json['routeId'] as int?,
|
||||
siteId: json['siteId'] as int?,
|
||||
orgId: json['orgId'] as int?,
|
||||
createTime: json['createTime'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
deviceId,
|
||||
taskStatus,
|
||||
taskStatusTranslate,
|
||||
routeId,
|
||||
siteId,
|
||||
orgId,
|
||||
createTime,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../entities/device_task_entity.dart';
|
||||
|
||||
abstract class DeviceTaskRepository {
|
||||
/// 获取设备任务池列表
|
||||
Future<Either<Failure, List<DeviceTaskEntity>>> getDeviceTaskPool({
|
||||
required String userId,
|
||||
required int siteId,
|
||||
required int orgId,
|
||||
int pageNum = 1,
|
||||
int pageSize = 99999999,
|
||||
});
|
||||
|
||||
/// 取消任务
|
||||
Future<Either<Failure, bool>> cancelTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
});
|
||||
|
||||
/// 暂停任务
|
||||
Future<Either<Failure, bool>> pauseTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
});
|
||||
|
||||
/// 恢复任务
|
||||
Future<Either<Failure, Map<String, dynamic>>> recoveryTask({
|
||||
required String deviceId,
|
||||
required int taskId,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
});
|
||||
}
|
||||
@@ -26,4 +26,14 @@ abstract class PathRepository {
|
||||
|
||||
/// 根据场站ID查询工作记录列表(XML格式)
|
||||
Future<List<WorkRecordEntity>> getWorkRecordsBySiteId({required int siteId});
|
||||
|
||||
/// 创建设备任务(通过接口执行作业)
|
||||
/// deviceId: 设备ID(targetDevice)
|
||||
/// routeId: 路线ID(选中的路线任务ID)
|
||||
/// siteId: 场站ID
|
||||
Future<void> createDeviceTask({
|
||||
required String deviceId,
|
||||
required int routeId,
|
||||
required int siteId,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../../../core/domain/usecases/base_usecase.dart';
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../repositories/device_task_repository.dart';
|
||||
|
||||
class CancelTaskUseCase implements BaseUseCase<bool, CancelTaskParams> {
|
||||
final DeviceTaskRepository repository;
|
||||
|
||||
CancelTaskUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, bool>> call(CancelTaskParams params) async {
|
||||
return await repository.cancelTask(
|
||||
deviceId: params.deviceId,
|
||||
taskId: params.taskId,
|
||||
orgId: params.orgId,
|
||||
siteId: params.siteId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CancelTaskParams {
|
||||
final String deviceId;
|
||||
final int taskId;
|
||||
final int orgId;
|
||||
final int siteId;
|
||||
|
||||
CancelTaskParams({
|
||||
required this.deviceId,
|
||||
required this.taskId,
|
||||
required this.orgId,
|
||||
required this.siteId,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../domain/errors/device_failure.dart';
|
||||
import '../../domain/repositories/path_repository.dart';
|
||||
|
||||
class CreateDeviceTaskUseCase {
|
||||
final PathRepository repository;
|
||||
|
||||
CreateDeviceTaskUseCase(this.repository);
|
||||
|
||||
Future<Either<DeviceFailure, void>> execute({
|
||||
required String deviceId,
|
||||
required int routeId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
try {
|
||||
await repository.createDeviceTask(
|
||||
deviceId: deviceId,
|
||||
routeId: routeId,
|
||||
siteId: siteId,
|
||||
);
|
||||
return const Right(null);
|
||||
} catch (e) {
|
||||
return Left(DeviceFailure.networkError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../../../core/domain/usecases/base_usecase.dart';
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../entities/device_task_entity.dart';
|
||||
import '../repositories/device_task_repository.dart';
|
||||
|
||||
class GetDeviceTaskPoolUseCase
|
||||
implements BaseUseCase<List<DeviceTaskEntity>, GetDeviceTaskPoolParams> {
|
||||
final DeviceTaskRepository repository;
|
||||
|
||||
GetDeviceTaskPoolUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<DeviceTaskEntity>>> call(
|
||||
GetDeviceTaskPoolParams params,
|
||||
) async {
|
||||
return await repository.getDeviceTaskPool(
|
||||
userId: params.userId,
|
||||
siteId: params.siteId,
|
||||
orgId: params.orgId,
|
||||
pageNum: params.pageNum,
|
||||
pageSize: params.pageSize,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class GetDeviceTaskPoolParams {
|
||||
final String userId;
|
||||
final int siteId;
|
||||
final int orgId;
|
||||
final int pageNum;
|
||||
final int pageSize;
|
||||
|
||||
GetDeviceTaskPoolParams({
|
||||
required this.userId,
|
||||
required this.siteId,
|
||||
required this.orgId,
|
||||
this.pageNum = 1,
|
||||
this.pageSize = 99999999,
|
||||
});
|
||||
}
|
||||
34
lib/features/devices/domain/usecases/pause_task_usecase.dart
Normal file
34
lib/features/devices/domain/usecases/pause_task_usecase.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../../../core/domain/usecases/base_usecase.dart';
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../repositories/device_task_repository.dart';
|
||||
|
||||
class PauseTaskUseCase implements BaseUseCase<bool, PauseTaskParams> {
|
||||
final DeviceTaskRepository repository;
|
||||
|
||||
PauseTaskUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, bool>> call(PauseTaskParams params) async {
|
||||
return await repository.pauseTask(
|
||||
deviceId: params.deviceId,
|
||||
taskId: params.taskId,
|
||||
orgId: params.orgId,
|
||||
siteId: params.siteId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PauseTaskParams {
|
||||
final String deviceId;
|
||||
final int taskId;
|
||||
final int orgId;
|
||||
final int siteId;
|
||||
|
||||
PauseTaskParams({
|
||||
required this.deviceId,
|
||||
required this.taskId,
|
||||
required this.orgId,
|
||||
required this.siteId,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import '../../../../core/domain/usecases/base_usecase.dart';
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../repositories/device_task_repository.dart';
|
||||
|
||||
class RecoveryTaskUseCase
|
||||
implements BaseUseCase<Map<String, dynamic>, RecoveryTaskParams> {
|
||||
final DeviceTaskRepository repository;
|
||||
|
||||
RecoveryTaskUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, Map<String, dynamic>>> call(
|
||||
RecoveryTaskParams params,
|
||||
) async {
|
||||
return await repository.recoveryTask(
|
||||
deviceId: params.deviceId,
|
||||
taskId: params.taskId,
|
||||
orgId: params.orgId,
|
||||
siteId: params.siteId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RecoveryTaskParams {
|
||||
final String deviceId;
|
||||
final int taskId;
|
||||
final int orgId;
|
||||
final int siteId;
|
||||
|
||||
RecoveryTaskParams({
|
||||
required this.deviceId,
|
||||
required this.taskId,
|
||||
required this.orgId,
|
||||
required this.siteId,
|
||||
});
|
||||
}
|
||||
315
lib/features/devices/presentation/bloc/device_task_cubit.dart
Normal file
315
lib/features/devices/presentation/bloc/device_task_cubit.dart
Normal file
@@ -0,0 +1,315 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../../../../core/app/app_user_cubit.dart';
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/error_handler.dart';
|
||||
import '../../../../features/v2/site/presentation/cubit/site_cubit.dart';
|
||||
import '../../domain/usecases/cancel_task_usecase.dart';
|
||||
import '../../domain/usecases/get_device_task_pool_usecase.dart';
|
||||
import '../../domain/usecases/pause_task_usecase.dart';
|
||||
import '../../domain/usecases/recovery_task_usecase.dart';
|
||||
import 'device_task_state.dart';
|
||||
|
||||
class DeviceTaskCubit extends Cubit<DeviceTaskState> {
|
||||
final GetDeviceTaskPoolUseCase _getDeviceTaskPoolUseCase;
|
||||
final CancelTaskUseCase _cancelTaskUseCase;
|
||||
final PauseTaskUseCase _pauseTaskUseCase;
|
||||
final RecoveryTaskUseCase _recoveryTaskUseCase;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
DeviceTaskCubit(
|
||||
this._getDeviceTaskPoolUseCase,
|
||||
this._cancelTaskUseCase,
|
||||
this._pauseTaskUseCase,
|
||||
this._recoveryTaskUseCase,
|
||||
) : super(const DeviceTaskState());
|
||||
|
||||
/// 获取任务池并过滤出当前设备的任务
|
||||
Future<void> fetchAndFilterTask(String deviceId) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||
|
||||
try {
|
||||
// 获取用户信息
|
||||
final userCubit = GetIt.I<AppUserCubit>();
|
||||
final user = userCubit.state.user;
|
||||
if (user == null) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '用户未登录',
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取场站ID
|
||||
final siteCubit = GetIt.I<SiteCubit>();
|
||||
final siteId = siteCubit.state.selectedSite?.id;
|
||||
if (siteId == null) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '未选择场站',
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用接口获取任务池
|
||||
final result = await _getDeviceTaskPoolUseCase.call(
|
||||
GetDeviceTaskPoolParams(
|
||||
userId: user.userId ?? '',
|
||||
siteId: siteId,
|
||||
orgId: user.orgId ?? 0,
|
||||
),
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) {
|
||||
_logger.logWithLevel('❌ 获取任务池失败: ${failure.message}');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
},
|
||||
(taskList) {
|
||||
// 过滤出当前设备的任务
|
||||
final deviceTasks = taskList
|
||||
.where((task) => task.deviceId == deviceId)
|
||||
.toList();
|
||||
|
||||
// 取第一个任务(或根据业务逻辑选择)
|
||||
final currentTask = deviceTasks.isNotEmpty ? deviceTasks.first : null;
|
||||
|
||||
_logger.logWithLevel(
|
||||
'✅ 找到 ${deviceTasks.length} 个任务,当前任务ID: ${currentTask?.id}',
|
||||
);
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
taskPool: taskList,
|
||||
currentTask: currentTask,
|
||||
currentTaskId: currentTask?.id,
|
||||
));
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 获取任务池异常: $e');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(e),
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 取消任务
|
||||
Future<void> cancelTask(String deviceId) async {
|
||||
final taskId = state.currentTaskId;
|
||||
if (taskId == null) {
|
||||
emit(state.copyWith(errorMessage: '无可用任务'));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: true,
|
||||
operationType: DeviceTaskOperationType.cancel,
|
||||
));
|
||||
|
||||
try {
|
||||
final userCubit = GetIt.I<AppUserCubit>();
|
||||
final user = userCubit.state.user;
|
||||
final siteCubit = GetIt.I<SiteCubit>();
|
||||
final siteId = siteCubit.state.selectedSite?.id;
|
||||
|
||||
if (user == null || siteId == null) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '参数不完整',
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await _cancelTaskUseCase.call(
|
||||
CancelTaskParams(
|
||||
deviceId: deviceId,
|
||||
taskId: taskId,
|
||||
orgId: user.orgId ?? 0,
|
||||
siteId: siteId,
|
||||
),
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) {
|
||||
_logger.logWithLevel('❌ 取消任务失败: ${failure.message}');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(failure.message),
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
},
|
||||
(success) {
|
||||
_logger.logWithLevel('✅ 取消任务成功');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
));
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 取消任务异常: $e');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(e),
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 暂停任务
|
||||
Future<void> pauseTask(String deviceId) async {
|
||||
final taskId = state.currentTaskId;
|
||||
if (taskId == null) {
|
||||
emit(state.copyWith(errorMessage: '无可用任务'));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: true,
|
||||
operationType: DeviceTaskOperationType.pause,
|
||||
));
|
||||
|
||||
try {
|
||||
final userCubit = GetIt.I<AppUserCubit>();
|
||||
final user = userCubit.state.user;
|
||||
final siteCubit = GetIt.I<SiteCubit>();
|
||||
final siteId = siteCubit.state.selectedSite?.id;
|
||||
|
||||
if (user == null || siteId == null) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '参数不完整',
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await _pauseTaskUseCase.call(
|
||||
PauseTaskParams(
|
||||
deviceId: deviceId,
|
||||
taskId: taskId,
|
||||
orgId: user.orgId ?? 0,
|
||||
siteId: siteId,
|
||||
),
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) {
|
||||
_logger.logWithLevel('❌ 暂停任务失败: ${failure.message}');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(failure.message),
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
},
|
||||
(success) {
|
||||
_logger.logWithLevel('✅ 暂停任务成功');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
));
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 暂停任务异常: $e');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(e),
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 恢复任务
|
||||
Future<void> recoveryTask(String deviceId) async {
|
||||
final taskId = state.currentTaskId;
|
||||
if (taskId == null) {
|
||||
emit(state.copyWith(errorMessage: '无可用任务'));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: true,
|
||||
operationType: DeviceTaskOperationType.recovery,
|
||||
));
|
||||
|
||||
try {
|
||||
final userCubit = GetIt.I<AppUserCubit>();
|
||||
final user = userCubit.state.user;
|
||||
final siteCubit = GetIt.I<SiteCubit>();
|
||||
final siteId = siteCubit.state.selectedSite?.id;
|
||||
|
||||
if (user == null || siteId == null) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '参数不完整',
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await _recoveryTaskUseCase.call(
|
||||
RecoveryTaskParams(
|
||||
deviceId: deviceId,
|
||||
taskId: taskId,
|
||||
orgId: user.orgId ?? 0,
|
||||
siteId: siteId,
|
||||
),
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) {
|
||||
_logger.logWithLevel('❌ 恢复任务失败: ${failure.message}');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(failure.message),
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
},
|
||||
(data) {
|
||||
_logger.logWithLevel('✅ 恢复任务成功: $data');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
));
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ 恢复任务异常: $e');
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: ErrorHandler.getErrorMessage(e),
|
||||
operationType: DeviceTaskOperationType.none,
|
||||
shouldShowError: true, // 🔥 标记需要显示错误弹窗
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 更新当前任务ID(当选择新航线时调用)
|
||||
void updateCurrentTaskId(int taskId) {
|
||||
emit(state.copyWith(currentTaskId: taskId));
|
||||
_logger.logWithLevel('🔄 更新当前任务ID: $taskId');
|
||||
}
|
||||
|
||||
/// 清除当前任务
|
||||
void clearCurrentTask() {
|
||||
emit(state.copyWith(
|
||||
currentTask: null,
|
||||
currentTaskId: null,
|
||||
));
|
||||
_logger.logWithLevel('🧹 清除当前任务');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../domain/entities/device_task_entity.dart';
|
||||
|
||||
enum DeviceTaskOperationType {
|
||||
none,
|
||||
cancel,
|
||||
pause,
|
||||
recovery,
|
||||
}
|
||||
|
||||
class DeviceTaskState extends Equatable {
|
||||
final List<DeviceTaskEntity> taskPool;
|
||||
final DeviceTaskEntity? currentTask;
|
||||
final int? currentTaskId;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
final DeviceTaskOperationType operationType;
|
||||
final bool shouldShowError; // 🔥 标记是否需要显示错误弹窗
|
||||
|
||||
const DeviceTaskState({
|
||||
this.taskPool = const [],
|
||||
this.currentTask,
|
||||
this.currentTaskId,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
this.operationType = DeviceTaskOperationType.none,
|
||||
this.shouldShowError = false,
|
||||
});
|
||||
|
||||
DeviceTaskState copyWith({
|
||||
List<DeviceTaskEntity>? taskPool,
|
||||
DeviceTaskEntity? currentTask,
|
||||
int? currentTaskId,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
DeviceTaskOperationType? operationType,
|
||||
bool? shouldShowError,
|
||||
}) {
|
||||
return DeviceTaskState(
|
||||
taskPool: taskPool ?? this.taskPool,
|
||||
currentTask: currentTask ?? this.currentTask,
|
||||
currentTaskId: currentTaskId ?? this.currentTaskId,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
operationType: operationType ?? this.operationType,
|
||||
shouldShowError: shouldShowError ?? false, // 🔥 默认重置为 false
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
taskPool,
|
||||
currentTask,
|
||||
currentTaskId,
|
||||
isLoading,
|
||||
errorMessage,
|
||||
operationType,
|
||||
shouldShowError,
|
||||
];
|
||||
}
|
||||
@@ -623,4 +623,53 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
//print('🔄 [DevicesCubit] 已重置到达位置');
|
||||
_logger.logWithLevel('🔄 [DevicesCubit] 已重置到达位置');
|
||||
}
|
||||
|
||||
/// 🔥 获取当前选中的设备
|
||||
DeviceEntity? getSelectedDevice() {
|
||||
return state.selectedDevice;
|
||||
}
|
||||
|
||||
/// 🔥 异步获取当前选中的设备(兼容 RemoteControlCubit 接口)
|
||||
Future<DeviceEntity?> getDevice() async {
|
||||
return state.selectedDevice;
|
||||
}
|
||||
|
||||
/// 🔥 判断是否有选中设备
|
||||
bool hasSelectedDevice() {
|
||||
return state.selectedDevice != null;
|
||||
}
|
||||
|
||||
/// 🔥 获取选中设备名称(安全获取,返回空字符串而非null)
|
||||
String getSelectedDeviceName() {
|
||||
return state.selectedDevice?.deviceName ?? '';
|
||||
}
|
||||
|
||||
/// 🔥 获取选中设备ID(安全获取,返回空字符串而非null)
|
||||
String getSelectedDeviceId() {
|
||||
return state.selectedDevice?.deviceName ?? '';
|
||||
}
|
||||
|
||||
/// 🔥 清除选中设备
|
||||
void clearSelectedDevice() {
|
||||
debugPrint('🧹 [DevicesCubit] 清除选中设备');
|
||||
_logger.logWithLevel('🧹 [DevicesCubit] 清除选中设备');
|
||||
emit(state.copyWith(selectedDevice: null));
|
||||
}
|
||||
|
||||
/// 🔥 检查设备是否在列表中
|
||||
bool isDeviceInList(String deviceName) {
|
||||
return state.devices.any((device) => device.deviceName == deviceName);
|
||||
}
|
||||
|
||||
/// 🔥 根据设备名称查找设备
|
||||
DeviceEntity? findDeviceByName(String deviceName) {
|
||||
try {
|
||||
return state.devices.firstWhere(
|
||||
(device) => device.deviceName == deviceName,
|
||||
orElse: () => throw Exception('Device not found'),
|
||||
);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user