and so on

This commit is contained in:
2026-08-18 08:52:33 +08:00
parent 87609c7dc4
commit ab75e8c5b0
56 changed files with 1816 additions and 1146 deletions

View File

@@ -46,6 +46,7 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
}) async {
if (_isDisposed) return;
// 同一设备(SN 相同):只增加引用计数,共享订阅
if (_listenerCount > 0 &&
_deviceSn == deviceSn &&
_gatewaySn == gatewaySn &&
@@ -55,7 +56,11 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
return;
}
// 切换到不同设备:强制清理旧订阅
// 不管引用计数多少,SN 变了就必须先取消旧订阅再订阅新的
if (_deviceSn != null || _gatewaySn != null) {
// 重置引用计数为 1,让 stopListening 能真正取消 MQTT 订阅
_listenerCount = 1;
await stopListening();
}
@@ -113,15 +118,19 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
if (_isDisposed) return;
try {
final jsonData = jsonDecode(message.payload) as Map<String, dynamic>;
final osdData = DroneOsdEntity.fromJson(jsonData);
if (_gatewaySn != null &&
_gatewaySn!.isNotEmpty &&
message.topic.contains(_gatewaySn!)) {
// 注入来源 SN,供下游解析层验证消息归属
jsonData['_sourceSn'] = _gatewaySn;
final osdData = DroneOsdEntity.fromJson(jsonData);
_stationOsdController.add(osdData);
} else if (_deviceSn != null &&
_deviceSn!.isNotEmpty &&
message.topic.contains(_deviceSn!)) {
jsonData['_sourceSn'] = _deviceSn;
final osdData = DroneOsdEntity.fromJson(jsonData);
_droneOsdController.add(osdData);
}
} catch (e) {
@@ -130,9 +139,28 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
}
void dispose() {
if (_isDisposed) return;
_isDisposed = true;
stopListening();
// 强制清理:不受引用计数限制,确保 MQTT 订阅被真正取消
// 避免切换机场时旧订阅残留导致数据串台
_listenerCount = 0;
_subscription?.cancel();
_subscription = null;
if (_deviceSn != null && _deviceSn!.isNotEmpty) {
final deviceSn = _deviceSn!;
mqttClient.unsubscribe('thing/product/$deviceSn/osd');
}
if (_gatewaySn != null && _gatewaySn!.isNotEmpty) {
final gatewaySn = _gatewaySn!;
mqttClient.unsubscribe('thing/product/$gatewaySn/osd');
}
_deviceSn = null;
_gatewaySn = null;
_droneOsdController.close();
_stationOsdController.close();
}
}
}