Files
flutterApp/lib/features/devices/domain/entities/running_status_entity.dart
Songzex 34b4c2bcbc 完成页面对接tcp推送的设备状态数据
完成页面测试解析tcp推送的设备状态数据的UI展示
2026-03-03 19:34:17 +08:00

204 lines
6.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// lib/features/devices/domain/entities/running_status_entity.dart
import 'package:equatable/equatable.dart';
class RunningStatusEntity extends Equatable {
// === 基础电气信息 (字段 0-6) ===
final double voltage; // [0] 电压 (V)
final double leftTargetSpeed; // [1] 左轮目标速度
final double rightTargetSpeed; // [2] 右轮目标速度
final double leftMeasureSpeed; // [3] 左轮实测速度
final double rightMeasureSpeed; // [4] 右轮实测速度
final double leftCurrent; // [5] 左轮电流 (A)
final double rightCurrent; // [6] 右轮电流 (A)
// === 温度信息 (字段 7-9) ===
final double leftMotorTemp; // [7] 左电机温度 (℃)
final double rightMotorTemp; // [8] 右电机温度 (℃)
final double chipTemp; // [9] 芯片温度 (℃)
// === 姿态信息 (字段 10-12) ===
final double yaw; // [10] 偏航角 (°)
final double pitch; // [11] 俯仰角 (°)
final double roll; // [12] 翻滚角 (°)
// === GPS 信号质量 (字段 13-15) ===
final int satelliteCnt; // [13] 卫星数量
final int qual; // [14] 定位质量
final int headingStatus; // [15] 航向状态
// === GPS 位置信息 (字段 16-17) ===
final double latitude; // [16] 纬度
final double longitude; // [17] 经度
// === 扩展信息 (字段 18-23) ===
final String timestamp; // [18] 时间戳 (格式:20-1-11-31 08:00:00)
final String knifeCuttingSpeed; // [19] 割刀速度
final String controlMode; // [20] 控制模式
final String battery; // [21] 电量百分比
final String workingArea; // [22] 工作面积
final String obstacleFlag; // [23] 障碍物标志
const RunningStatusEntity({
this.voltage = 0.0,
this.leftTargetSpeed = 0.0,
this.rightTargetSpeed = 0.0,
this.leftMeasureSpeed = 0.0,
this.rightMeasureSpeed = 0.0,
this.leftCurrent = 0.0,
this.rightCurrent = 0.0,
this.leftMotorTemp = 0.0,
this.rightMotorTemp = 0.0,
this.chipTemp = 0.0,
this.yaw = 0.0,
this.pitch = 0.0,
this.roll = 0.0,
this.satelliteCnt = 0,
this.qual = 0,
this.headingStatus = 0,
this.latitude = 0.0,
this.longitude = 0.0,
this.timestamp = '',
this.knifeCuttingSpeed = '0',
this.controlMode = '0',
this.battery = '0',
this.workingArea = '0',
this.obstacleFlag = '0',
});
/// 从字段数组创建实体 (核心解析方法)
factory RunningStatusEntity.fromFields(List<String> fields) {
// 确保至少有 24 个字段,不足的用默认值填充
while (fields.length < 24) {
fields.add('');
}
return RunningStatusEntity(
// 基础电气信息
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,
// GPS 信号质量
satelliteCnt: int.tryParse(fields[13]) ?? 0,
qual: int.tryParse(fields[14]) ?? 0,
headingStatus: int.tryParse(fields[15]) ?? 0,
// GPS 位置信息
latitude: double.tryParse(fields[16]) ?? 0.0,
longitude: double.tryParse(fields[17]) ?? 0.0,
// 扩展信息 (保持原始字符串,由调用方决定如何解析)
timestamp: fields[18].trim(),
knifeCuttingSpeed: fields[19].trim().isNotEmpty ? fields[19].trim() : '0',
controlMode: fields[20].trim().isNotEmpty ? fields[20].trim() : '0',
battery: fields[21].trim().isNotEmpty ? fields[21].trim() : '0',
workingArea: fields[22].trim().isNotEmpty ? fields[22].trim() : '0',
obstacleFlag: fields[23].trim().isNotEmpty ? fields[23].trim() : '0',
);
}
/// copyWith 方法 (用于局部更新)
RunningStatusEntity copyWith({
double? voltage,
double? leftTargetSpeed,
double? rightTargetSpeed,
double? leftMeasureSpeed,
double? rightMeasureSpeed,
double? leftCurrent,
double? rightCurrent,
double? leftMotorTemp,
double? rightMotorTemp,
double? chipTemp,
double? yaw,
double? pitch,
double? roll,
int? satelliteCnt,
int? qual,
int? headingStatus,
double? latitude,
double? longitude,
String? timestamp,
String? knifeCuttingSpeed,
String? controlMode,
String? battery,
String? workingArea,
String? obstacleFlag,
}) {
return RunningStatusEntity(
voltage: voltage ?? this.voltage,
leftTargetSpeed: leftTargetSpeed ?? this.leftTargetSpeed,
rightTargetSpeed: rightTargetSpeed ?? this.rightTargetSpeed,
leftMeasureSpeed: leftMeasureSpeed ?? this.leftMeasureSpeed,
rightMeasureSpeed: rightMeasureSpeed ?? this.rightMeasureSpeed,
leftCurrent: leftCurrent ?? this.leftCurrent,
rightCurrent: rightCurrent ?? this.rightCurrent,
leftMotorTemp: leftMotorTemp ?? this.leftMotorTemp,
rightMotorTemp: rightMotorTemp ?? this.rightMotorTemp,
chipTemp: chipTemp ?? this.chipTemp,
yaw: yaw ?? this.yaw,
pitch: pitch ?? this.pitch,
roll: roll ?? this.roll,
satelliteCnt: satelliteCnt ?? this.satelliteCnt,
qual: qual ?? this.qual,
headingStatus: headingStatus ?? this.headingStatus,
latitude: latitude ?? this.latitude,
longitude: longitude ?? this.longitude,
timestamp: timestamp ?? this.timestamp,
knifeCuttingSpeed: knifeCuttingSpeed ?? this.knifeCuttingSpeed,
controlMode: controlMode ?? this.controlMode,
battery: battery ?? this.battery,
workingArea: workingArea ?? this.workingArea,
obstacleFlag: obstacleFlag ?? this.obstacleFlag,
);
}
@override
List<Object?> get props => [
voltage,
leftTargetSpeed,
rightTargetSpeed,
leftMeasureSpeed,
rightMeasureSpeed,
leftCurrent,
rightCurrent,
leftMotorTemp,
rightMotorTemp,
chipTemp,
yaw,
pitch,
roll,
satelliteCnt,
qual,
headingStatus,
latitude,
longitude,
timestamp,
knifeCuttingSpeed,
controlMode,
battery,
workingArea,
obstacleFlag,
];
@override
String toString() {
return 'RunningStatusEntity(voltage: $voltage, chipTemp: $chipTemp, '
'latitude: $latitude, longitude: $longitude, timestamp: $timestamp)';
}
}