1.测试和修改了无人机在线实时信息的展示数据集的展示。使其能够全面展示。
2.添加了在无人机设备摄像头的为空时候,通过新增的接口获取无人机的摄像头的列表。 3.替换原先依照tcp推送消息绘制机器运动作业的轨迹过程,先用订阅mqtt特定话题推送的信息做动画过程的绘制。
This commit is contained in:
@@ -6,12 +6,18 @@ import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/running_status_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/gps_entity.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/protocol_decoder.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/repositories/task_message_repository.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/entities/task_arrive_entity.dart';
|
||||
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/net_message_dispatcher.dart';
|
||||
import '../../../../core/network/tcp/tcp_client.dart';
|
||||
import 'device_status_event.dart';
|
||||
import 'device_status_state.dart';
|
||||
import 'devices_cubit.dart';
|
||||
|
||||
// 定义全局的TaskMessageRepository获取方式
|
||||
TaskMessageRepository get _taskMessageRepo => GetIt.I<TaskMessageRepository>();
|
||||
|
||||
class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
final NetMessageDispatcher _dispatcher;
|
||||
@@ -20,6 +26,7 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
|
||||
// 🔥 保存订阅引用,用于管理生命周期
|
||||
StreamSubscription? _tcpSubscription;
|
||||
StreamSubscription? _mqttArriveSubscription;
|
||||
|
||||
// 🔥 节流相关:500ms节流控制0x02数据推送频率
|
||||
Timer? _throttleTimer;
|
||||
@@ -27,16 +34,23 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
RunningStatusEntity? _cachedStatus;
|
||||
GPSEntity? _cachedGps;
|
||||
|
||||
// 🔥 当前监听的设备ID
|
||||
String? _currentDeviceId;
|
||||
|
||||
DeviceStatusBloc(this._dispatcher, {TcpClient? client})
|
||||
: tcpClient = client ?? GetIt.I<TcpClient>(),
|
||||
super(DeviceStatusInitial()) {
|
||||
// 🔥 核心改动:直接在构造函数中建立TCP监听,类似RemoteControlCubit
|
||||
_initDirectTcpListener();
|
||||
|
||||
// 🔥 初始化MQTT到达点监听
|
||||
_initMqttArriveListener();
|
||||
|
||||
// 保留事件处理(用于手动重置等场景)
|
||||
on<DeviceStatusReset>(_handleReset);
|
||||
on<DeviceStatusLoaded>(_handleDeviceStatusLoaded);
|
||||
on<PushMessageReceived>(_handlePushMessageReceived);
|
||||
on<DeviceTaskArrivePointEvent>(_handleDeviceTaskArrivePointEvent);
|
||||
}
|
||||
|
||||
// 🔥 已废弃:重新初始化TCP监听器会导致数据流中断
|
||||
@@ -162,6 +176,62 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
_logger.log('✅ [DeviceStatusBloc] 直接TCP监听器已建立完成');
|
||||
}
|
||||
|
||||
// 🔥 初始化MQTT到达点监听
|
||||
void _initMqttArriveListener() {
|
||||
debugPrint('🔗 [DeviceStatusBloc] 初始化MQTT到达点监听器');
|
||||
_logger.log('🔗 [DeviceStatusBloc] 初始化MQTT到达点监听器');
|
||||
|
||||
_mqttArriveSubscription = _taskMessageRepo.taskArriveStream.listen(
|
||||
(TaskArriveEntity arrive) {
|
||||
debugPrint(
|
||||
'📍 [DeviceStatusBloc] 收到MQTT到达点消息: type=${arrive.type}, deviceId=${arrive.deviceId}',
|
||||
);
|
||||
_logger.log(
|
||||
'📍 [DeviceStatusBloc] 收到MQTT到达点消息: type=${arrive.type}, deviceId=${arrive.deviceId}',
|
||||
);
|
||||
|
||||
// 过滤出路径规划到达点消息
|
||||
if (arrive.type == 'device_task_arrive_point') {
|
||||
// 检查设备ID是否匹配当前监听的设备
|
||||
if (_currentDeviceId != null && arrive.deviceId != _currentDeviceId) {
|
||||
debugPrint(
|
||||
'⚠️ [DeviceStatusBloc] 设备ID不匹配,跳过 - 当前:$_currentDeviceId, 收到:${arrive.deviceId}',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 提取坐标(支持空entity场景)
|
||||
double? lat = arrive.entity?.lat;
|
||||
double? lng = arrive.entity?.lng;
|
||||
|
||||
// 发送事件到Bloc
|
||||
add(
|
||||
DeviceTaskArrivePointEvent(
|
||||
deviceId: arrive.deviceId,
|
||||
taskId: arrive.taskId,
|
||||
lat: lat,
|
||||
lng: lng,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
onError: (e) {
|
||||
debugPrint('❌ [DeviceStatusBloc] MQTT到达点监听错误: $e');
|
||||
_logger.log('❌ [DeviceStatusBloc] MQTT到达点监听错误: $e');
|
||||
},
|
||||
);
|
||||
|
||||
debugPrint('✅ [DeviceStatusBloc] MQTT到达点监听器已建立完成');
|
||||
_logger.log('✅ [DeviceStatusBloc] MQTT到达点监听器已建立完成');
|
||||
}
|
||||
|
||||
// 🔥 设置当前监听的设备ID(用于过滤MQTT消息)
|
||||
void setListeningDeviceId(String deviceId) {
|
||||
debugPrint('📱 [DeviceStatusBloc] 设置监听设备ID: $deviceId');
|
||||
_logger.log('📱 [DeviceStatusBloc] 设置监听设备ID: $deviceId');
|
||||
_currentDeviceId = deviceId;
|
||||
}
|
||||
|
||||
// 🔥 节流发射:500ms到期后发射缓存的最新数据
|
||||
void _emitCachedStatus() {
|
||||
if (_cachedStatus != null && _cachedGps != null && !isClosed) {
|
||||
@@ -228,17 +298,59 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 处理MQTT到达点事件
|
||||
Future<void> _handleDeviceTaskArrivePointEvent(
|
||||
DeviceTaskArrivePointEvent event,
|
||||
Emitter<DeviceStatusState> emit,
|
||||
) async {
|
||||
debugPrint(
|
||||
'📍 [DeviceStatusBloc] 处理到达点事件: deviceId=${event.deviceId}, taskId=${event.taskId}, lat=${event.lat}, lng=${event.lng}',
|
||||
);
|
||||
_logger.log(
|
||||
'📍 [DeviceStatusBloc] 处理到达点事件: deviceId=${event.deviceId}, taskId=${event.taskId}, lat=${event.lat}, lng=${event.lng}',
|
||||
);
|
||||
|
||||
if (event.lat != null && event.lng != null && event.lat != 0.0) {
|
||||
// ✅ 收到有效到达点 - 更新位置状态
|
||||
final gps = GPSEntity(event.lat!, event.lng!);
|
||||
|
||||
// 发送到DevicesCubit更新到达位置(触发地图轨迹更新)
|
||||
final devicesCubit = GetIt.I<DevicesCubit>();
|
||||
devicesCubit.setArrivedLocation(event.lat!, event.lng!);
|
||||
|
||||
// 同时更新DeviceStatusBloc的状态
|
||||
emit(DeviceStatusUpdated(_cachedStatus ?? RunningStatusEntity(), gps));
|
||||
} else {
|
||||
// ❌ entity为空或坐标无效 - 表示任务停止
|
||||
debugPrint('🛑 [DeviceStatusBloc] 收到任务停止信号');
|
||||
_logger.log('🛑 [DeviceStatusBloc] 收到任务停止信号');
|
||||
|
||||
final devicesCubit = GetIt.I<DevicesCubit>();
|
||||
devicesCubit.finishWork();
|
||||
Future.delayed(Duration(seconds: 1), () {
|
||||
devicesCubit.resetWorkStatus();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
debugPrint('🚫 [DeviceStatusBloc] 页面退出,仅取消TCP订阅(不关闭BLoC)');
|
||||
_logger.log('🚫 [DeviceStatusBloc] 页面退出,仅取消TCP订阅(不关闭BLoC)');
|
||||
//_tcpSubscription?.cancel();
|
||||
debugPrint('🚫 [DeviceStatusBloc] 页面退出,清理资源');
|
||||
_logger.log('🚫 [DeviceStatusBloc] 页面退出,清理资源');
|
||||
|
||||
// 🔥 清理MQTT订阅
|
||||
_mqttArriveSubscription?.cancel();
|
||||
_mqttArriveSubscription = null;
|
||||
|
||||
// 🔥 清理节流timer和缓存
|
||||
_throttleTimer?.cancel();
|
||||
_throttleTimer = null;
|
||||
_cachedStatus = null;
|
||||
_cachedGps = null;
|
||||
// 🔥 关键修复:不调用 super.close(),保持 BLoC 活跃
|
||||
|
||||
// 🔥 重置设备ID
|
||||
_currentDeviceId = null;
|
||||
|
||||
return Future.value();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,22 @@ class PushMessageReceived extends DeviceStatusEvent {
|
||||
|
||||
class DeviceStatusReset extends DeviceStatusEvent {
|
||||
@override
|
||||
// TODO: implement props
|
||||
List<Object?> get props => throw UnimplementedError();
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class DeviceTaskArrivePointEvent extends DeviceStatusEvent {
|
||||
final String deviceId;
|
||||
final int? taskId;
|
||||
final double? lat;
|
||||
final double? lng;
|
||||
|
||||
const DeviceTaskArrivePointEvent({
|
||||
required this.deviceId,
|
||||
this.taskId,
|
||||
this.lat,
|
||||
this.lng,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [deviceId, taskId, lat, lng];
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import 'package:maibu_satabot_v2/features/devices/domain/usecases/update_devicen
|
||||
import '../../../../core/consts/tcp_consts.dart';
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/tcp/tcp_client.dart';
|
||||
import '../../../../core/network/mqtt/domain/repositories/task_message_repository.dart';
|
||||
import '../../data/models/device_add_path_point_model.dart';
|
||||
import '../../data/models/device_work_area_param_model.dart';
|
||||
import '../../domain/usecases/bind_device_usecase.dart';
|
||||
@@ -30,6 +31,9 @@ import 'device_status_bloc.dart';
|
||||
import 'device_status_event.dart';
|
||||
import 'devices_state.dart';
|
||||
|
||||
// 定义全局的TaskMessageRepository获取方式
|
||||
TaskMessageRepository get _taskMessageRepo => GetIt.I<TaskMessageRepository>();
|
||||
|
||||
class DevicesCubit extends Cubit<DevicesState> {
|
||||
final GetUserDeviceUseCase _getUserDeviceUseCase;
|
||||
final GetDeviceLocationUseCase _getDeviceLocationUseCase;
|
||||
@@ -672,4 +676,42 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔥 启动MQTT到达点监听(用于路径规划动画)
|
||||
/// [deviceId] - 目标设备ID,即targetDevice的deviceId
|
||||
Future<void> startListeningMqttArrive({required String deviceId}) async {
|
||||
debugPrint('📡 [DevicesCubit] 启动MQTT到达点监听 - deviceId: $deviceId');
|
||||
_logger.logWithLevel('📡 [DevicesCubit] 启动MQTT到达点监听 - deviceId: $deviceId');
|
||||
|
||||
try {
|
||||
// 启动MQTT订阅
|
||||
await _taskMessageRepo.startListening(deviceId: deviceId);
|
||||
|
||||
// 设置DeviceStatusBloc监听的设备ID
|
||||
_deviceStatusBloc.setListeningDeviceId(deviceId);
|
||||
|
||||
debugPrint('✅ [DevicesCubit] MQTT到达点监听已启动');
|
||||
_logger.logWithLevel('✅ [DevicesCubit] MQTT到达点监听已启动');
|
||||
} catch (e) {
|
||||
debugPrint('❌ [DevicesCubit] 启动MQTT监听失败: $e');
|
||||
_logger.logWithLevel('❌ [DevicesCubit] 启动MQTT监听失败: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔥 停止MQTT到达点监听
|
||||
Future<void> stopListeningMqttArrive() async {
|
||||
debugPrint('🛑 [DevicesCubit] 停止MQTT到达点监听');
|
||||
_logger.logWithLevel('🛑 [DevicesCubit] 停止MQTT到达点监听');
|
||||
|
||||
try {
|
||||
await _taskMessageRepo.stopListening();
|
||||
_deviceStatusBloc.setListeningDeviceId('');
|
||||
debugPrint('✅ [DevicesCubit] MQTT到达点监听已停止');
|
||||
_logger.logWithLevel('✅ [DevicesCubit] MQTT到达点监听已停止');
|
||||
} catch (e) {
|
||||
debugPrint('❌ [DevicesCubit] 停止MQTT监听失败: $e');
|
||||
_logger.logWithLevel('❌ [DevicesCubit] 停止MQTT监听失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user