一次大的提交
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/gps_entity.dar
|
||||
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 'package:maibu_satabot_v2/core/network/mqtt/domain/entities/task_status_entity.dart';
|
||||
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/net_message_dispatcher.dart';
|
||||
@@ -27,6 +28,7 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
// 🔥 保存订阅引用,用于管理生命周期
|
||||
StreamSubscription? _tcpSubscription;
|
||||
StreamSubscription? _mqttArriveSubscription;
|
||||
StreamSubscription? _mqttStatusSubscription;
|
||||
|
||||
// 🔥 节流相关:500ms节流控制0x02数据推送频率
|
||||
Timer? _throttleTimer;
|
||||
@@ -34,6 +36,9 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
RunningStatusEntity? _cachedStatus;
|
||||
GPSEntity? _cachedGps;
|
||||
|
||||
// 🔥 调试计数器:跟踪0x02收包序号,排查断断续续问题
|
||||
int _packetSeq = 0;
|
||||
|
||||
// 🔥 当前监听的设备ID
|
||||
String? _currentDeviceId;
|
||||
|
||||
@@ -46,6 +51,9 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
// 🔥 初始化MQTT到达点监听
|
||||
_initMqttArriveListener();
|
||||
|
||||
// 🔥 初始化MQTT任务状态监听(接收完成推送)
|
||||
_initMqttStatusListener();
|
||||
|
||||
// 保留事件处理(用于手动重置等场景)
|
||||
on<DeviceStatusReset>(_handleReset);
|
||||
on<DeviceStatusLoaded>(_handleDeviceStatusLoaded);
|
||||
@@ -89,23 +97,27 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
// 🔥 关键修复:直接监听 tcpClient.packetStream,不经过 dispatcher 的 filtered stream
|
||||
// 这样即使没有其他监听者,TCP流也不会暂停
|
||||
_tcpSubscription = tcpClient.packetStream
|
||||
.where((p) => p.command == 0x02)
|
||||
.where((p) {
|
||||
final is02 = p.command == 0x02;
|
||||
if (is02) {
|
||||
_packetSeq++;
|
||||
debugPrint('📥 [0x02] #$_packetSeq 收到原始TCP包 | payload长度=${p.payload.length} | ${DateTime.now().toString().substring(11, 19)}');
|
||||
}
|
||||
return is02;
|
||||
})
|
||||
.map((p) {
|
||||
try {
|
||||
final result = utf8.decode(p.payload, allowMalformed: true);
|
||||
// debugPrint('✅ [DeviceStatusBloc] 收到0x02数据: $result');
|
||||
return result;
|
||||
} catch (e) {
|
||||
// debugPrint('❌ [DeviceStatusBloc] 解码失败: $e');
|
||||
debugPrint('❌ [0x02] #$_packetSeq 解码失败: $e, payload前20字节=${p.payload.take(20).toList()}');
|
||||
return '';
|
||||
}
|
||||
})
|
||||
.listen(
|
||||
(message) {
|
||||
//debugPrint('📩 [DeviceStatusBloc] 直接收到0x02数据,长度:${message.length}');
|
||||
|
||||
if (message.isEmpty) {
|
||||
//debugPrint('⚠️ [DeviceStatusBloc] 消息为空,跳过');
|
||||
debugPrint('⚠️ [0x02] #$_packetSeq 解码后为空,跳过');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,7 +126,7 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
final fields = message.trim().split(',');
|
||||
|
||||
if (fields.length < 18) {
|
||||
//debugPrint('⚠️ [DeviceStatusBloc] 字段不足:${fields.length},期望≥18');
|
||||
debugPrint('⚠️ [0x02] #$_packetSeq 字段不足:${fields.length},期望≥18, 原始数据前100字符=${message.substring(0, message.length > 100 ? 100 : message.length)}');
|
||||
// 🔥 错误不节流,立即emit以便UI显示错误
|
||||
if (!isClosed) {
|
||||
emit(DeviceStatusError('字段不足,期望≥18,实际:${fields.length}'));
|
||||
@@ -125,6 +137,8 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
final status = RunningStatusEntity.fromFields(fields);
|
||||
final gps = GPSEntity(status.latitude, status.longitude);
|
||||
|
||||
debugPrint('✅ [0x02] #$_packetSeq 解析成功 | 字段数=${fields.length} | 电压=${status.voltage}V 电量=${status.battery}% 控制模式=${status.controlMode} | 节流等待${_throttleDuration.inMilliseconds}ms');
|
||||
|
||||
// 🔥 缓存最新数据用于节流发射
|
||||
_cachedStatus = RunningStatusEntity(
|
||||
voltage: status.voltage,
|
||||
@@ -154,13 +168,14 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
);
|
||||
_cachedGps = GPSEntity(status.latitude, status.longitude);
|
||||
|
||||
// 🔥 节流:取消之前的timer,重新计时500ms
|
||||
_throttleTimer?.cancel();
|
||||
_throttleTimer = Timer(_throttleDuration, () {
|
||||
_emitCachedStatus();
|
||||
});
|
||||
// 🔥 节流:如果定时器已在运行,只更新缓存不重置;否则启动新的500ms节流周期
|
||||
if (_throttleTimer == null || !_throttleTimer!.isActive) {
|
||||
_throttleTimer = Timer(_throttleDuration, () {
|
||||
_emitCachedStatus();
|
||||
});
|
||||
}
|
||||
} catch (e, stack) {
|
||||
//debugPrint('❌ [DeviceStatusBloc] 直接解析异常:$e\n$stack');
|
||||
debugPrint('❌ [0x02] #$_packetSeq 解析异常:$e');
|
||||
// _logger.log('❌ [DeviceStatusBloc] 直接解析异常:$e');
|
||||
// 🔥 解析错误不节流,立即emit以便UI显示错误
|
||||
if (!isClosed) {
|
||||
@@ -225,6 +240,60 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
_logger.log('✅ [DeviceStatusBloc] MQTT到达点监听器已建立完成');
|
||||
}
|
||||
|
||||
// 🔥 初始化MQTT任务状态监听(接收 task/+/status 完成推送)
|
||||
void _initMqttStatusListener() {
|
||||
debugPrint('🔗 [DeviceStatusBloc] 初始化MQTT任务状态监听器');
|
||||
_logger.log('🔗 [DeviceStatusBloc] 初始化MQTT任务状态监听器');
|
||||
|
||||
_mqttStatusSubscription = _taskMessageRepo.taskStatusStream.listen(
|
||||
(TaskStatusEntity status) {
|
||||
debugPrint(
|
||||
'📋 [DeviceStatusBloc] 收到MQTT任务状态: type=${status.type}, deviceId=${status.deviceId}, status=${status.status}',
|
||||
);
|
||||
_logger.log(
|
||||
'📋 [DeviceStatusBloc] 收到MQTT任务状态: type=${status.type}, deviceId=${status.deviceId}, status=${status.status}',
|
||||
);
|
||||
|
||||
// 检查设备ID是否匹配当前监听的设备
|
||||
if (_currentDeviceId != null && status.deviceId != _currentDeviceId) {
|
||||
debugPrint(
|
||||
'⚠️ [DeviceStatusBloc] 任务状态设备ID不匹配,跳过 - 当前:$_currentDeviceId, 收到:${status.deviceId}',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 状态为 FINISH 表示任务完成
|
||||
if (status.status == 'FINISH') {
|
||||
debugPrint('🏁 [DeviceStatusBloc] 收到任务完成推送,触发完成流程');
|
||||
_logger.log('🏁 [DeviceStatusBloc] 收到任务完成推送,触发完成流程');
|
||||
|
||||
final devicesCubit = GetIt.I<DevicesCubit>();
|
||||
devicesCubit.finishWork();
|
||||
|
||||
// 🔥 无论缓存是否有效,都必须 emit 触发 BlocBuilder 重建
|
||||
if (!isClosed) {
|
||||
emit(DeviceStatusUpdated(
|
||||
_cachedStatus ?? RunningStatusEntity(),
|
||||
_cachedGps ?? GPSEntity(0.0, 0.0),
|
||||
));
|
||||
debugPrint('📤 [DeviceStatusBloc] 已 emit 完成信号,触发 UI 更新');
|
||||
}
|
||||
|
||||
Future.delayed(Duration(seconds: 1), () {
|
||||
devicesCubit.resetWorkStatus();
|
||||
});
|
||||
}
|
||||
},
|
||||
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');
|
||||
@@ -235,7 +304,7 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
// 🔥 节流发射:500ms到期后发射缓存的最新数据
|
||||
void _emitCachedStatus() {
|
||||
if (_cachedStatus != null && _cachedGps != null && !isClosed) {
|
||||
// debugPrint('📤 [DeviceStatusBloc] 🔥节流发射 - 电压:${_cachedStatus!.voltage}, 电量:${_cachedStatus!.battery}');
|
||||
debugPrint('📤 [0x02] 节流发射 | 电压=${_cachedStatus!.voltage}V 电量=${_cachedStatus!.battery}% 经纬度=(${_cachedGps!.latitude}, ${_cachedGps!.longitude}) | ${DateTime.now().toString().substring(11, 19)}');
|
||||
emit(DeviceStatusUpdated(_cachedStatus!, _cachedGps!));
|
||||
}
|
||||
}
|
||||
@@ -327,6 +396,16 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
|
||||
final devicesCubit = GetIt.I<DevicesCubit>();
|
||||
devicesCubit.finishWork();
|
||||
|
||||
// 🔥 关键:无论缓存是否有效,都必须 emit 触发 BlocBuilder 重建
|
||||
if (!isClosed) {
|
||||
emit(DeviceStatusUpdated(
|
||||
_cachedStatus ?? RunningStatusEntity(),
|
||||
_cachedGps ?? GPSEntity(0.0, 0.0),
|
||||
));
|
||||
debugPrint('📤 [DeviceStatusBloc] 已 emit 停止信号,触发 UI 更新');
|
||||
}
|
||||
|
||||
Future.delayed(Duration(seconds: 1), () {
|
||||
devicesCubit.resetWorkStatus();
|
||||
});
|
||||
@@ -341,6 +420,8 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||||
// 🔥 清理MQTT订阅
|
||||
_mqttArriveSubscription?.cancel();
|
||||
_mqttArriveSubscription = null;
|
||||
_mqttStatusSubscription?.cancel();
|
||||
_mqttStatusSubscription = null;
|
||||
|
||||
// 🔥 清理节流timer和缓存
|
||||
_throttleTimer?.cancel();
|
||||
|
||||
Reference in New Issue
Block a user