重整多余的UI组件和未定功能的逻辑链路的修正

This commit is contained in:
2026-11-11 16:33:04 +08:00
parent 34cb9d2805
commit 47f518f94c
100 changed files with 5789 additions and 3070 deletions

View File

@@ -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) {

View File

@@ -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,

View File

@@ -66,7 +66,7 @@ class HostRealtimeMessageEntity extends Equatable {
/// [data] 为报文 `data` 子对象(由 DataSource 提取,与 MowerLocationEntity 一致)
factory HostRealtimeMessageEntity.fromJson(Map<String, dynamic> data) {
return HostRealtimeMessageEntity(
batteryPercentage: _toDouble(data['battery_percentage']),
batteryPercentage: parseBatteryPercentage(data['battery_percentage']) ?? 0,
batteryVoltage: _toDouble(data['battery_voltage']),
leftMotorCurrent: _toDouble(data['left_motor_current']),
rightMotorCurrent: _toDouble(data['right_motor_current']),
@@ -95,6 +95,20 @@ class HostRealtimeMessageEntity extends Equatable {
);
}
/// 供主机状态和设备概览使用,区分无效值与真实的 0%。
static double? parseBatteryPercentage(Object? value) {
final percentage = value is num
? value.toDouble()
: double.tryParse(value?.toString() ?? '');
if (percentage == null ||
!percentage.isFinite ||
percentage < 0 ||
percentage > 100) {
return null;
}
return percentage;
}
static double _toDouble(dynamic value) {
if (value == null) return 0;
if (value is num) return value.toDouble();

View File

@@ -13,6 +13,10 @@ abstract class MowerRealtimeRepository {
Stream<RouteStatusEntity> get routeStatusStream;
Stream<HostRealtimeMessageEntity> get hostRealtimeStream;
Stream<({double? battery, String controlMode})> watchOverviewStatus(
String deviceSn,
);
Future<Either<Failure, void>> startListening({
required String deviceSn,
});