优化ai userid和markdown
This commit is contained in:
@@ -57,8 +57,9 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
this._generatePathUseCase,
|
||||
this._routePlanningUseCase,
|
||||
this._bindDeviceUseCase,
|
||||
this._deviceStatusBloc,
|
||||
this._tcpClient, this._pathPlanningService,
|
||||
this._deviceStatusBloc,
|
||||
this._tcpClient,
|
||||
this._pathPlanningService,
|
||||
) : super(const DevicesState());
|
||||
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
@@ -174,7 +175,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
|
||||
});
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
@@ -207,55 +207,51 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
|
||||
result.fold(
|
||||
// 失败处理
|
||||
(l) {
|
||||
(l) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: l.message));
|
||||
selectDevice(device);
|
||||
},
|
||||
// 成功处理:改为 async 函数块
|
||||
(r) async {
|
||||
(r) async {
|
||||
// 3. 最后更新 UI 状态
|
||||
try {
|
||||
// 🔥 关键修复 1:先强制断开旧连接!
|
||||
// 这一步会销毁旧 Socket,清除旧 Listener,防止旧数据继续推送
|
||||
if (_tcpClient.isConnected) {
|
||||
debugPrint('🛑 检测到已连接,先断开旧 TCP 连接...');
|
||||
_tcpClient.disconnects(forSwitch: true);
|
||||
// 稍微等待一下,确保底层 Socket 资源释放 (可选,但推荐)
|
||||
// await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
try {
|
||||
// 🔥 关键修复 1:先强制断开旧连接!
|
||||
// 这一步会销毁旧 Socket,清除旧 Listener,防止旧数据继续推送
|
||||
if (_tcpClient.isConnected) {
|
||||
debugPrint('🛑 检测到已连接,先断开旧 TCP 连接...');
|
||||
_tcpClient.disconnects(forSwitch: true);
|
||||
// 稍微等待一下,确保底层 Socket 资源释放 (可选,但推荐)
|
||||
// await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
// 🔥 关键修复 2:发起新连接
|
||||
// connect 方法内部会自动调用 _sendAuthPacket -> 获取设备列表 -> 自动订阅当前选中的设备
|
||||
debugPrint('🔌 开始重新新连接 TCP,将自动订阅新设备:${device.deviceName}');
|
||||
await _tcpClient.connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: device.deviceName);
|
||||
// 🔥 关键修复 3:重置设备状态 Bloc,清除旧设备图表数据
|
||||
_deviceStatusBloc.add(DeviceStatusReset());
|
||||
// 🔥 关键修复 2:发起新连接
|
||||
// connect 方法内部会自动调用 _sendAuthPacket -> 获取设备列表 -> 自动订阅当前选中的设备
|
||||
debugPrint('🔌 开始重新新连接 TCP,将自动订阅新设备:${device.deviceName}');
|
||||
await _tcpClient.connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: device.deviceName);
|
||||
// 🔥 关键修复 3:重置设备状态 Bloc,清除旧设备图表数据
|
||||
_deviceStatusBloc.add(DeviceStatusReset());
|
||||
|
||||
debugPrint('✅ 新设备切换流程完成');
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ 设备切换成功,但 TCP 重连或状态重置失败:$e');
|
||||
// 即使 TCP 失败,也更新 UI 选中状态,让用户知道切换了,只是没数据
|
||||
}
|
||||
debugPrint('✅ 新设备切换流程完成');
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ 设备切换成功,但 TCP 重连或状态重置失败:$e');
|
||||
// 即使 TCP 失败,也更新 UI 选中状态,让用户知道切换了,只是没数据
|
||||
}
|
||||
|
||||
emit(state.copyWith(isLoading: false, selectedDevice: device));
|
||||
emit(state.copyWith(isLoading: false, selectedDevice: device));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 绑定设备
|
||||
Future<void> bindDevice(String deviceId, String deviceAlias) async {
|
||||
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
try {
|
||||
final params = BindDeviceParams(deviceId, deviceAlias);
|
||||
final result = await _bindDeviceUseCase.call(params);
|
||||
result.fold(
|
||||
// 失败处理
|
||||
(failure) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '绑定设备失败',
|
||||
));
|
||||
(failure) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '绑定设备失败'));
|
||||
// 抛出异常,携带后端返回的错误消息(如“设备不存在”)
|
||||
throw Exception(failure.message ?? '绑定设备失败');
|
||||
},
|
||||
@@ -363,7 +359,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
// 开始路径规划
|
||||
emit(state.copyWith(isLoading: true));
|
||||
//传入全局的Service 中路径规划队列
|
||||
Queue<DeviceAddPathPointModel> queue = _pathPlanningService.getQueue() ;
|
||||
Queue<DeviceAddPathPointModel> queue = _pathPlanningService.getQueue();
|
||||
print('📦 从 Service 获取的队列长度:${queue.length}');
|
||||
final result = await _routePlanningUseCase.startRoutePlanning(queue);
|
||||
result.fold(
|
||||
@@ -372,7 +368,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 暂停
|
||||
Future<void> pauseRoutePlanning() async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
@@ -409,6 +404,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
void resetWorkStatus() {
|
||||
emit(state.copyWith(isFinshWork: false));
|
||||
}
|
||||
|
||||
void setArrivedLocation(double latitude, double longitude) {
|
||||
emit(state.copyWith(arriLatitude: latitude, arriLongitude: longitude));
|
||||
print('✅ [DevicesCubit] 监听更新已完成到达位置:Lat=$latitude, Lng=$longitude');
|
||||
|
||||
Reference in New Issue
Block a user