一次大的提交

This commit is contained in:
2026-08-07 08:49:29 +08:00
parent bf2fc49c19
commit db70665ced
181 changed files with 23060 additions and 2804 deletions

View File

@@ -28,6 +28,8 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
StreamSubscription<MqttMessage>? _subscription;
String? _deviceSn;
String? _gatewaySn;
int _listenerCount = 0;
bool _isDisposed = false;
DroneOsdDataSourceImpl(this.mqttClient);
@@ -42,10 +44,14 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
required String deviceSn,
required String gatewaySn,
}) async {
if (_deviceSn == deviceSn &&
if (_isDisposed) return;
if (_listenerCount > 0 &&
_deviceSn == deviceSn &&
_gatewaySn == gatewaySn &&
_subscription != null) {
debugPrint('[DroneOsdDataSource] already listening');
_listenerCount++;
debugPrint('[DroneOsdDataSource] 引用计数+1: $_listenerCount');
return;
}
@@ -73,10 +79,20 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
_subscription = mqttClient.messageStream?.listen((message) {
_handleMessage(message);
});
_listenerCount = 1;
}
@override
Future<void> stopListening() async {
_listenerCount--;
if (_listenerCount > 0) {
debugPrint('[DroneOsdDataSource] 引用计数-1: $_listenerCount (保留订阅)');
return;
}
_listenerCount = 0;
await _subscription?.cancel();
_subscription = null;
@@ -94,26 +110,19 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
}
void _handleMessage(MqttMessage message) {
if (_isDisposed) return;
try {
final jsonData = jsonDecode(message.payload) as Map<String, dynamic>;
final osdData = DroneOsdEntity.fromJson(jsonData);
// 🔥 重要修复:先判断 gatewaySn,再判断 deviceSn
// 因为 topic 可能同时包含两者,但我们需要优先匹配机场
if (_gatewaySn != null &&
_gatewaySn!.isNotEmpty &&
message.topic.contains(_gatewaySn!)) {
debugPrint('🏢 [DroneOsdDataSource] 机场 OSD 更新');
debugPrint('🏢 [DroneOsdDataSource] Topic: ${message.topic}');
_stationOsdController.add(osdData);
} else if (_deviceSn != null &&
_deviceSn!.isNotEmpty &&
message.topic.contains(_deviceSn!)) {
debugPrint('🛸 [DroneOsdDataSource] 无人机 OSD 更新');
debugPrint('🛸 [DroneOsdDataSource] Topic: ${message.topic}');
_droneOsdController.add(osdData);
} else {
debugPrint('⚠️ [DroneOsdDataSource] 未知设备类型,Topic: ${message.topic}');
}
} catch (e) {
debugPrint('❌ [DroneOsdDataSource] 解析 OSD 数据失败: $e');
@@ -121,8 +130,9 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
}
void dispose() {
_isDisposed = true;
stopListening();
_droneOsdController.close();
_stationOsdController.close();
}
}
}

View File

@@ -13,7 +13,7 @@ abstract class TaskMessageDataSource {
Stream<TaskArriveEntity> get taskArriveStream;
Stream<RealTimeMessageEntity> get realTimeMessageStream;
Future<void> startListening({required String deviceId});
Future<void> startListening({required String deviceId, int? taskId});
Future<void> stopListening();
}
@@ -26,6 +26,7 @@ class TaskMessageDataSourceImpl implements TaskMessageDataSource {
StreamSubscription<MqttMessage>? _subscription;
String? _deviceId;
int? _taskId;
TaskMessageDataSourceImpl(this.mqttClient);
@@ -40,9 +41,9 @@ class TaskMessageDataSourceImpl implements TaskMessageDataSource {
_realTimeMessageController.stream;
@override
Future<void> startListening({required String deviceId}) async {
if (_deviceId == deviceId && _subscription != null) {
debugPrint('[TaskMessageDataSource] already listening: $deviceId');
Future<void> startListening({required String deviceId, int? taskId}) async {
if (_deviceId == deviceId && _taskId == taskId && _subscription != null) {
debugPrint('[TaskMessageDataSource] already listening: deviceId=$deviceId, taskId=$taskId');
return;
}
@@ -54,18 +55,20 @@ class TaskMessageDataSourceImpl implements TaskMessageDataSource {
}
_deviceId = deviceId;
_taskId = taskId;
final taskStatusTopic = 'task/$deviceId/status';
final taskArriveTopic = 'task/$deviceId/arrive';
// 🔥 status/arrive 话题使用 taskId,realTimeMessage 使用 deviceId
final taskStatusTopic = taskId != null ? 'task/$taskId/status' : null;
final taskArriveTopic = taskId != null ? 'task/$taskId/arrive' : null;
final realTimeTopic = 'device/$deviceId/realTimeMessage';
debugPrint('📋 [TaskMessageDataSource] 开始监听:');
debugPrint(' 任务状态: $taskStatusTopic');
debugPrint(' 到达通知: $taskArriveTopic');
if (taskStatusTopic != null) debugPrint(' 任务状态: $taskStatusTopic');
if (taskArriveTopic != null) debugPrint(' 到达通知: $taskArriveTopic');
debugPrint(' 实时消息: $realTimeTopic');
await mqttClient.subscribe(taskStatusTopic);
await mqttClient.subscribe(taskArriveTopic);
if (taskStatusTopic != null) await mqttClient.subscribe(taskStatusTopic);
if (taskArriveTopic != null) await mqttClient.subscribe(taskArriveTopic);
await mqttClient.subscribe(realTimeTopic);
_subscription = mqttClient.messageStream?.listen((message) {
@@ -80,12 +83,16 @@ class TaskMessageDataSourceImpl implements TaskMessageDataSource {
if (_deviceId != null) {
final deviceId = _deviceId!;
await mqttClient.unsubscribe('task/$deviceId/status');
await mqttClient.unsubscribe('task/$deviceId/arrive');
// 🔥 status/arrive 用 taskId 取消订阅
if (_taskId != null) {
await mqttClient.unsubscribe('task/$_taskId/status');
await mqttClient.unsubscribe('task/$_taskId/arrive');
}
await mqttClient.unsubscribe('device/$deviceId/realTimeMessage');
}
_deviceId = null;
_taskId = null;
}
void _handleMessage(MqttMessage message) {

View File

@@ -24,9 +24,9 @@ class TaskMessageRepositoryImpl implements TaskMessageRepository {
Stream<TaskStatusEntity> get taskStatusStream => dataSource.taskStatusStream;
@override
Future<Either<Failure, void>> startListening({required String deviceId}) async {
Future<Either<Failure, void>> startListening({required String deviceId, int? taskId}) async {
try {
await dataSource.startListening(deviceId: deviceId);
await dataSource.startListening(deviceId: deviceId, taskId: taskId);
return right(null);
} catch (e) {
return left(Failure(e.toString()));

View File

@@ -71,7 +71,7 @@ class MqttConfig extends Equatable {
factory MqttConfig.taskMessage() {
return const MqttConfig(
host: '1.95.137.212',
port: 59020,
port: 1883,
username: 'maibu',
password: 'jsmbzn520',
protocol: MqttProtocol.tcp,

View File

@@ -11,6 +11,6 @@ abstract class TaskMessageRepository {
Stream<TaskArriveEntity> get taskArriveStream;
Stream<TaskStatusEntity> get taskStatusStream;
Future<Either<Failure, void>> startListening({required String deviceId});
Future<Either<Failure, void>> startListening({required String deviceId, int? taskId});
Future<void> stopListening();
}