播放器修改,功能完善
This commit is contained in:
@@ -16,6 +16,7 @@ class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
StreamSubscription? _statusSub;
|
||||
StreamSubscription<PacketEntity>? _packetSub;
|
||||
Timer? _watchdogTimer;
|
||||
static const int _watchdogTimerDuration = 10;
|
||||
|
||||
// 核心:这是唯一的流入口
|
||||
Stream<PacketEntity>? _broadcastPacketStream;
|
||||
@@ -23,12 +24,12 @@ class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
ConnectivityCubit() : super(ConnectivityState.initial());
|
||||
|
||||
void setConnection(IConnection? connection, ControlMode mode) {
|
||||
// 1. 彻底清理(按顺序来)
|
||||
// 1. 彻底清理
|
||||
_stopMonitoring();
|
||||
_statusSub?.cancel();
|
||||
_activeConnection?.disconnect();
|
||||
|
||||
// 2. 关键:切断旧流的所有引用
|
||||
// 2. 切断旧流的所有引用
|
||||
_broadcastPacketStream = null;
|
||||
|
||||
_activeConnection = connection;
|
||||
@@ -38,7 +39,7 @@ class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
emit(state.copyWith(activeMode: mode, status: status));
|
||||
|
||||
if (status == ConnectionStatus.connected) {
|
||||
// 3. 只有连接成功时,才初始化唯一的广播流
|
||||
// 3. 只有连接成功时,才初始化广播流
|
||||
_ensureStreamInitialized();
|
||||
_startMonitoring();
|
||||
} else {
|
||||
@@ -54,17 +55,10 @@ class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
// 核心:强制初始化,确保 transform 只跑一次
|
||||
void _ensureStreamInitialized() {
|
||||
if (_activeConnection != null && _broadcastPacketStream == null) {
|
||||
print("DEBUG: 正在创建唯一的广播流转换器");
|
||||
|
||||
// 确保 rawStream 是有效的
|
||||
if (_activeConnection!.rawStream != null) {
|
||||
_broadcastPacketStream = _activeConnection!.rawStream
|
||||
.transform(PacketParserTransformer())
|
||||
.asBroadcastStream();
|
||||
} else {
|
||||
print("DEBUG: rawStream is null,无法初始化广播流");
|
||||
}
|
||||
}
|
||||
_broadcastPacketStream = _activeConnection!.rawStream
|
||||
.transform(PacketParserTransformer())
|
||||
.asBroadcastStream();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,9 +74,9 @@ class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
_resetWatchdog();
|
||||
if (packet.cmdType == 0xFF) {
|
||||
sendCommand(0xFF, null);
|
||||
sl<ILoggerService>().log("receive heartbeat,has response...");
|
||||
sl<ILoggerService>().log("收到心跳包,已回复...");
|
||||
}
|
||||
}, onError: (e) => print("流监听错误: $e"));
|
||||
}, onError: (e) => sl<ILoggerService>().log("流监听错误: $e"));
|
||||
}
|
||||
|
||||
// Getter 也要改,保证它只返回已经创建好的流
|
||||
@@ -94,8 +88,8 @@ class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
// --- 其他逻辑保持不变 ---
|
||||
void _resetWatchdog() {
|
||||
_watchdogTimer?.cancel();
|
||||
_watchdogTimer = Timer(const Duration(seconds: 10), () {
|
||||
print("警告:10秒内数据,触发断开逻辑");
|
||||
_watchdogTimer = Timer(const Duration(seconds: _watchdogTimerDuration), () {
|
||||
sl<ILoggerService>().log("$_watchdogTimerDuration秒内未收到心跳包...");
|
||||
emit(state.copyWith(status: ConnectionStatus.disconnected));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,6 +22,5 @@ class ConnectivityState extends Equatable{
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement props
|
||||
List<Object?> get props => [activeMode, status];
|
||||
}
|
||||
Reference in New Issue
Block a user