完成页面对接tcp推送的设备状态数据

完成页面测试解析tcp推送的设备状态数据的UI展示
This commit is contained in:
2026-03-03 19:34:17 +08:00
parent 0a3a062c04
commit 34b4c2bcbc
12 changed files with 676 additions and 230 deletions

View File

@@ -14,77 +14,82 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
final NetMessageDispatcher _dispatcher;
DeviceStatusBloc(this._dispatcher) : super(DeviceStatusInitial()) {
// 直接订阅 0x02 指令的字符串流
// 订阅 TCP 数据流
_dispatcher.onStringMessage().listen((jsonString) {
debugPrint('📩 Bloc 收到 0x02 数据');
add(DeviceStatusLoaded(jsonString));
});
// 订阅 0x12 的 JSON 流()
_dispatcher.onJsonMessage(0x12).listen((jsonData) {
add(PushMessageReceived(jsonData));
});
}
@override
Stream<DeviceStatusState> mapEventToState(DeviceStatusEvent event) async* {
if (event is DeviceStatusLoaded) {
yield* _mapDeviceStatusLoaded(event);
} else if (event is PushMessageReceived) {
yield* _mapPushMessageReceived(event);
}
}
//
on<DeviceStatusLoaded>(_handleDeviceStatusLoaded);
on<PushMessageReceived>(_handlePushMessageReceived);
Stream<DeviceStatusState> _mapDeviceStatusLoaded(DeviceStatusLoaded event) async* {
// 🔥 新增:模拟测试数据(调试用)
_testMockData();
}
void _testMockData() {
// 用户提供的真实指令数据(字节数组)
final mockBytes = [
171, 170, 2, 48, 46, 48, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 46, 48, 48, 44, 48, 46, 48, 48, 44, 48, 46, 48, 48, 44, 48, 46, 48, 48, 44, 50, 55, 46, 52, 55, 44, 49, 56, 48, 46, 48, 48, 44, 48, 46, 48, 48, 44, 48, 46, 48, 48, 44, 52, 49, 44, 52, 44, 49, 44, 51, 50, 46, 48, 52, 49, 50, 57, 54, 56, 54, 52, 48, 48, 57, 57, 57, 44, 49, 50, 48, 46, 56, 48, 56, 57, 57, 51, 48, 56, 49, 49, 53, 56, 53, 48, 44, 50, 48, 45, 49, 45, 49, 49, 45, 51, 49, 32, 48, 56, 58, 48, 48, 58, 48, 48, 44, 48, 44, 51, 44, 54, 54, 44, 57, 53, 50, 55, 46, 48, 48, 48, 48, 44, 48, 0, 0, 0, 170, 171
];
// 解码为字符串(去掉帧头 3 字节和帧尾 5 字节)
final payloadBytes = mockBytes.sublist(3, mockBytes.length - 5);
final mockData = String.fromCharCodes(payloadBytes);
debugPrint('🧪 模拟测试数据:$mockData');
// 发送模拟事件
add(DeviceStatusLoaded(mockData));
}
// 🔥 修改:使用 Emitter 发送状态
Future<void> _handleDeviceStatusLoaded(
DeviceStatusLoaded event,
Emitter<DeviceStatusState> emit,
) async {
try {
final fields = event.jsonString.trim().split(' ');
debugPrint('🔍 开始解析数据:${event.jsonString}');
final fields = event.jsonString.trim().split(',');
debugPrint('🔍 字段数量:${fields.length}');
if (fields.length < 18) {
yield DeviceStatusError('字段不足,期望 ≥18,实际: ${fields.length}');
emit(DeviceStatusError('字段不足,期望 ≥18,实际:${fields.length}'));
return;
}
///解析设备状态
final status = RunningStatusEntity().copyWith(
voltage: double.tryParse(fields[0]) ?? 0.0,
leftTargetSpeed: double.tryParse(fields[1]) ?? 0.0,
rightTargetSpeed: double.tryParse(fields[2]) ?? 0.0,
leftMeasureSpeed: double.tryParse(fields[3]) ?? 0.0,
rightMeasureSpeed: double.tryParse(fields[4]) ?? 0.0,
leftCurrent: double.tryParse(fields[5]) ?? 0.0,
rightCurrent: double.tryParse(fields[6]) ?? 0.0,
leftMotorTemp: double.tryParse(fields[7]) ?? 0.0,
rightMotorTemp: double.tryParse(fields[8]) ?? 0.0,
chipTemp: double.tryParse(fields[9]) ?? 0.0,
yaw: double.tryParse(fields[10]) ?? 0.0,
pitch: double.tryParse(fields[11]) ?? 0.0,
roll: double.tryParse(fields[12]) ?? 0.0,
satelliteCnt: int.tryParse(fields[13]) ?? 0,
qual: int.tryParse(fields[14]) ?? 0,
headingStatus: int.tryParse(fields[15]) ?? 0,
latitude: double.tryParse(fields[16]) ?? 0.0,
longitude: double.tryParse(fields[17]) ?? 0.0,
knifeCuttingSpeed: fields.length > 19 && fields[19].isNotEmpty ? fields[19] : "0",
controlMode: fields.length > 20 && fields[20].isNotEmpty ? fields[20] : "0",
battery: fields.length > 21 && fields[21].isNotEmpty ? fields[21] : "0",
workingArea: fields.length > 22 && fields[22].isNotEmpty ? fields[22] : "0",
obstacleFlag: fields.length > 23 && fields[23].isNotEmpty ? fields[23] : "0",
);
///解析GPS
final status = RunningStatusEntity.fromFields(fields);
debugPrint('设备运行时候状态:$status');
final gps = GPSEntity(status.latitude, status.longitude);
yield DeviceStatusUpdated(status, gps);
debugPrint('GPS: ${gps.latitude}, ${gps.longitude}');
emit(DeviceStatusUpdated(status, gps)); // 🔥 使用 emit 发送状态
} catch (e) {
yield DeviceStatusError('解析设备状态失败: $e');
debugPrint('❌ 解析异常:$e');
emit(DeviceStatusError('解析设备运行时候状态失败:$e'));
}
}
/// 解析 推送消息
Stream<DeviceStatusState> _mapPushMessageReceived(PushMessageReceived event) async* {
Future<void> _handlePushMessageReceived(
PushMessageReceived event,
Emitter<DeviceStatusState> emit,
) async {
try {
final eventStr = event.jsonData['event'] ?? '';
final deviceId = event.jsonData['deviceId'] ?? '未知';
debugPrint('收到推送事件: $eventStr, 设备: $deviceId');
// 可扩展:yield PushMessageState(event.jsonData)
debugPrint('收到推送事件:$eventStr, 设备:$deviceId');
} catch (e) {
yield DeviceStatusError('解析推送消息失败: $e');
emit(DeviceStatusError('解析推送消息失败:$e'));
}
}
@override
Future<void> close() {
return super.close();
}
}