重整多余的UI组件和未定功能的逻辑链路的修正
This commit is contained in:
@@ -24,6 +24,11 @@ abstract class MowerRealtimeDataSource {
|
||||
/// 🔥 主机实时状态(device/{sn}/host/realTime/message,新增话题,纯附加)
|
||||
Stream<HostRealtimeMessageEntity> get hostRealtimeStream;
|
||||
|
||||
/// 按设备观察概览所需的电量和控制模式,订阅随监听者释放。
|
||||
Stream<({double? battery, String controlMode})> watchOverviewStatus(
|
||||
String deviceSn,
|
||||
);
|
||||
|
||||
/// 按设备 SN 启动订阅:自动退订旧设备主题、同 SN 去重
|
||||
Future<void> startListening({required String deviceSn});
|
||||
|
||||
@@ -76,6 +81,70 @@ class MowerRealtimeDataSourceImpl implements MowerRealtimeDataSource {
|
||||
String _routeTopic(String sn) => 'device/$sn/$_routeSuffix';
|
||||
String _hostRealtimeTopic(String sn) => 'device/$sn/$_hostRealtimeSuffix';
|
||||
|
||||
@override
|
||||
Stream<({double? battery, String controlMode})> watchOverviewStatus(
|
||||
String deviceSn,
|
||||
) {
|
||||
final sn = deviceSn.trim();
|
||||
final messages = mqttClient.messageStream;
|
||||
if (sn.isEmpty || messages == null) return const Stream.empty();
|
||||
|
||||
final topic = _hostRealtimeTopic(sn);
|
||||
StreamSubscription<MqttMessage>? messageSubscription;
|
||||
Future<void>? topicSubscription;
|
||||
double? lastBattery;
|
||||
late final StreamController<({double? battery, String controlMode})>
|
||||
controller;
|
||||
|
||||
controller = StreamController(
|
||||
onListen: () {
|
||||
// 先监听报文再订阅 Topic,接住订阅时立即到达的消息。
|
||||
messageSubscription = messages.where((msg) => msg.topic == topic).listen(
|
||||
(message) {
|
||||
try {
|
||||
final json = jsonDecode(message.payload);
|
||||
if (json is! Map) return;
|
||||
final payload = json['data'];
|
||||
final data = payload is Map ? payload : json;
|
||||
final battery = HostRealtimeMessageEntity.parseBatteryPercentage(
|
||||
data['battery_percentage'],
|
||||
);
|
||||
lastBattery = battery ?? lastBattery;
|
||||
controller.add(
|
||||
(
|
||||
battery: lastBattery,
|
||||
controlMode: data['control_mode']?.toString().trim() ?? '',
|
||||
),
|
||||
);
|
||||
} on FormatException {
|
||||
// 忽略格式异常的单帧,等待下一次上报。
|
||||
}
|
||||
},
|
||||
onError: controller.addError,
|
||||
onDone: controller.close,
|
||||
);
|
||||
topicSubscription = mqttClient.subscribe(topic).catchError(
|
||||
(Object error, StackTrace stackTrace) {
|
||||
if (!controller.isClosed) controller.addError(error, stackTrace);
|
||||
},
|
||||
);
|
||||
},
|
||||
onCancel: () async {
|
||||
await messageSubscription?.cancel();
|
||||
final subscription = topicSubscription;
|
||||
if (subscription == null) return;
|
||||
await subscription;
|
||||
try {
|
||||
// 客户端按引用计数释放,详情页仍在使用时会保留其订阅。
|
||||
await mqttClient.unsubscribe(topic);
|
||||
} catch (error) {
|
||||
debugPrint('[设备概览] 释放订阅失败 $topic: $error');
|
||||
}
|
||||
},
|
||||
);
|
||||
return controller.stream.distinct();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> startListening({required String deviceSn}) async {
|
||||
if (deviceSn.isEmpty) {
|
||||
|
||||
@@ -28,6 +28,11 @@ class MowerRealtimeRepositoryImpl implements MowerRealtimeRepository {
|
||||
Stream<HostRealtimeMessageEntity> get hostRealtimeStream =>
|
||||
dataSource.hostRealtimeStream;
|
||||
|
||||
@override
|
||||
Stream<({double? battery, String controlMode})> watchOverviewStatus(
|
||||
String deviceSn,
|
||||
) => dataSource.watchOverviewStatus(deviceSn);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, void>> startListening({
|
||||
required String deviceSn,
|
||||
|
||||
Reference in New Issue
Block a user