96 lines
3.6 KiB
Dart
96 lines
3.6 KiB
Dart
|
||
import 'dart:convert';
|
||
import 'package:flutter/cupertino.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/domain/entities/running_status_entity.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/domain/entities/gps_entity.dart';
|
||
import 'package:maibu_satabot_v2/core/network/protocol_decoder.dart'; // 确保能访问 RawPacket
|
||
|
||
import '../../../../core/network/net_message_dispatcher.dart';
|
||
import 'device_status_event.dart';
|
||
import 'device_status_state.dart';
|
||
|
||
class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
|
||
final NetMessageDispatcher _dispatcher;
|
||
|
||
DeviceStatusBloc(this._dispatcher) : super(DeviceStatusInitial()) {
|
||
// 订阅 TCP 数据流
|
||
_dispatcher.onStringMessage().listen((jsonString) {
|
||
debugPrint('📩 Bloc 收到 0x02 数据');
|
||
add(DeviceStatusLoaded(jsonString));
|
||
});
|
||
|
||
_dispatcher.onJsonMessage(0x12).listen((jsonData) {
|
||
add(PushMessageReceived(jsonData));
|
||
});
|
||
|
||
//
|
||
on<DeviceStatusLoaded>(_handleDeviceStatusLoaded);
|
||
on<PushMessageReceived>(_handlePushMessageReceived);
|
||
|
||
|
||
// 🔥 新增:模拟测试数据(调试用)
|
||
// _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 {
|
||
debugPrint('🔍 开始解析数据:${event.jsonString}');
|
||
final fields = event.jsonString.trim().split(',');
|
||
debugPrint('🔍 字段数量:${fields.length}');
|
||
|
||
if (fields.length < 18) {
|
||
emit(DeviceStatusError('字段不足,期望 ≥18,实际:${fields.length}'));
|
||
return;
|
||
}
|
||
|
||
final status = RunningStatusEntity.fromFields(fields);
|
||
debugPrint('设备运行时候状态:$status');
|
||
final gps = GPSEntity(status.latitude, status.longitude);
|
||
debugPrint('GPS: ${gps.latitude}, ${gps.longitude}');
|
||
|
||
emit(DeviceStatusUpdated(status, gps)); // 🔥 使用 emit 发送状态
|
||
} catch (e) {
|
||
debugPrint('❌ 解析异常:$e');
|
||
emit(DeviceStatusError('解析设备运行时候状态失败:$e'));
|
||
}
|
||
}
|
||
|
||
Future<void> _handlePushMessageReceived(
|
||
PushMessageReceived event,
|
||
Emitter<DeviceStatusState> emit,
|
||
) async {
|
||
try {
|
||
final eventStr = event.jsonData['event'] ?? '';
|
||
final deviceId = event.jsonData['deviceId'] ?? '未知';
|
||
debugPrint('收到推送事件:$eventStr, 设备:$deviceId');
|
||
} catch (e) {
|
||
emit(DeviceStatusError('解析推送消息失败:$e'));
|
||
}
|
||
}
|
||
|
||
@override
|
||
Future<void> close() {
|
||
return super.close();
|
||
}
|
||
}
|
||
|