远程遥控视频的显示bug修复;恢复远程遥控中的前置推送检查和动态实时信息的展示。
This commit is contained in:
@@ -35,7 +35,8 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
StreamSubscription? _kickOutSub; // 新增:用于管理监听生命周期
|
||||
final NetMessageDispatcher dispatcher;
|
||||
final DeviceStatusBloc deviceStatusBloc; // 🔥 注入 DeviceStatusBloc
|
||||
StreamSubscription? _deviceStatusSub; // 🔥 订阅 DeviceStatusBloc 的状态流
|
||||
StreamSubscription? _deviceStatusSub; // 🔥 订阅 DeviceStatusBloc 的状态流(MQTT,已停用)
|
||||
StreamSubscription? _tcpStatusSub; // 🔥 订阅 TCP 0x02 机器状态推送(门槛 + 电压/电量/控制模式)
|
||||
static const platform = MethodChannel('com.maibu.satabot/ping');
|
||||
int _currentPing = 50;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
@@ -70,7 +71,11 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
),
|
||||
) {
|
||||
_initPacketListener();
|
||||
_initDeviceStatusListener(); // 🔥 改为订阅 DeviceStatusBloc
|
||||
// 🔥 门槛(hasReceivedStatusPush)与电压/电量/控制模式改由 TCP 0x02 驱动,
|
||||
// 与 MQTT 定位链路解耦;MQTT(DeviceStatusBloc) 仅继续负责定位。
|
||||
// 回滚:注释下行、改回 _initDeviceStatusListener() 即可。
|
||||
_initTcpStatusListener();
|
||||
// _initDeviceStatusListener(); // 🔥 旧:订阅 DeviceStatusBloc(MQTT),已停用
|
||||
}
|
||||
|
||||
// 🔥 新增:订阅 DeviceStatusBloc 的状态流,而不是直接监听TCP
|
||||
@@ -82,6 +87,9 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
int? _cachePing;
|
||||
DateTime? _lastStatusPushTime; // 最后一次收到设备状态推送的时间
|
||||
|
||||
// 🔥 已停用:门槛与电压/电量/控制模式改由 TCP 0x02 驱动(见 _initTcpStatusListener)。
|
||||
// 保留本方法用于回滚——恢复构造函数中的调用即可重新走 MQTT(DeviceStatusUpdated)。
|
||||
// ignore: unused_element
|
||||
void _initDeviceStatusListener() {
|
||||
_deviceStatusSub?.cancel();
|
||||
|
||||
@@ -129,6 +137,77 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
});
|
||||
}
|
||||
|
||||
/// 🔥 门槛 + 运行状态专用监听:直接监听 TCP 0x02 机器状态推送。
|
||||
///
|
||||
/// 与 MQTT 定位链路彻底解耦:
|
||||
/// - 门槛 hasReceivedStatusPush / _lastStatusPushTime 由 TCP 0x02 驱动;
|
||||
/// - 电压/电量/控制模式 直接由 0x02 报文的 fromFields 解析(fields[0]/[21]/[20]);
|
||||
/// - MQTT(DeviceStatusBloc) 仅继续负责定位,不再影响本页门槛与三个值。
|
||||
void _initTcpStatusListener() {
|
||||
_tcpStatusSub?.cancel();
|
||||
|
||||
_tcpStatusSub = tcpClient.packetStream
|
||||
.where((p) => p.command == 0x02)
|
||||
.listen((packet) async {
|
||||
try {
|
||||
final message = utf8.decode(packet.payload, allowMalformed: true);
|
||||
if (message.trim().isEmpty) return;
|
||||
|
||||
final fields = message.trim().split(',');
|
||||
if (fields.length < 18) {
|
||||
debugPrint('⚠️ [TCP-0x02门槛] 字段不足:${fields.length},期望≥18,跳过');
|
||||
return;
|
||||
}
|
||||
|
||||
final status = RunningStatusEntity.fromFields(fields);
|
||||
final controlMode = status.controlMode == '3' ? '远程模式' : '本地模式';
|
||||
|
||||
// 🔥 门槛:收到 TCP 0x02 即视为推送活跃(节流前刷新,轻量无阻塞)
|
||||
_lastStatusPushTime = DateTime.now();
|
||||
|
||||
_cacheVoltage = status.voltage.toString();
|
||||
_cacheBattery = status.battery;
|
||||
_cacheCtrlMode = controlMode;
|
||||
|
||||
// 500ms节流,不到时间不刷新UI
|
||||
final now = DateTime.now();
|
||||
if (_lastUiUpdateTime != null &&
|
||||
now.difference(_lastUiUpdateTime!) <
|
||||
const Duration(milliseconds: 500)) {
|
||||
// 节流期间仍需保证门槛标志已置位(否则首帧恰逢节流会导致门槛漏置)
|
||||
if (!state.hasReceivedStatusPush && !isClosed) {
|
||||
emit(state.copyWith(hasReceivedStatusPush: true));
|
||||
}
|
||||
return;
|
||||
}
|
||||
_lastUiUpdateTime = now;
|
||||
|
||||
// 🔥 ping 只在真正刷新UI时测(≤2Hz):0x02 原始推送可能高频,
|
||||
// 若每帧 await getNetworkDelay 会造成大量并发 ping,移到节流后规避。
|
||||
final c = await getNetworkDelay();
|
||||
_cachePing = c;
|
||||
|
||||
if (!isClosed) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
runningStatusModel: state.runningStatusModel.copyWith(
|
||||
voltage: _cacheVoltage,
|
||||
battery: _cacheBattery,
|
||||
controlMode: _cacheCtrlMode,
|
||||
),
|
||||
battery: int.tryParse(_cacheBattery ?? '') ?? 0,
|
||||
ping: _cachePing,
|
||||
hasReceivedStatusPush: true, // 🔥 门槛:TCP 0x02 到达即置位
|
||||
updateType: 'device_status',
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('❌ [TCP-0x02门槛] 解析异常:$e');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 🔥 超简单方法:传入 IP,得到 ping 值
|
||||
|
||||
// 🔥 模拟设备状态更新 - 用于测试
|
||||
@@ -701,6 +780,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
Future<void> close() {
|
||||
_timer?.cancel();
|
||||
_deviceStatusSub?.cancel(); // 🔥 取消订阅 DeviceStatusBloc
|
||||
_tcpStatusSub?.cancel(); // 🔥 取消订阅 TCP 0x02 门槛监听
|
||||
return super.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user