2025-08-12 13:36:42 +08:00
|
|
|
|
import '../enums/vehicle_command_type.dart';
|
|
|
|
|
|
|
2025-09-18 15:53:39 +08:00
|
|
|
|
/// 车辆控制命令类
|
2025-08-12 13:36:42 +08:00
|
|
|
|
class VehicleCommand {
|
|
|
|
|
|
final VehicleCommandType type;
|
|
|
|
|
|
final Map<String, dynamic>? params;
|
|
|
|
|
|
final String error;
|
2025-09-18 15:53:39 +08:00
|
|
|
|
late String commandId;
|
2025-08-12 13:36:42 +08:00
|
|
|
|
|
2025-09-18 15:53:39 +08:00
|
|
|
|
VehicleCommand({required this.type, this.params, this.error = ''}) {
|
|
|
|
|
|
commandId = DateTime.now().millisecondsSinceEpoch.toString();
|
|
|
|
|
|
}
|
2025-08-12 13:36:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 从字符串创建命令(用于从API响应解析)
|
|
|
|
|
|
factory VehicleCommand.fromString(
|
|
|
|
|
|
String commandStr, Map<String, dynamic>? params, String error) {
|
|
|
|
|
|
// 将字符串转换为枚举值
|
|
|
|
|
|
VehicleCommandType type;
|
|
|
|
|
|
try {
|
|
|
|
|
|
type = VehicleCommandType.values.firstWhere(
|
|
|
|
|
|
(e) => e.name == commandStr,
|
|
|
|
|
|
orElse: () => VehicleCommandType.unknown,
|
|
|
|
|
|
);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('Error parsing command string: $e');
|
|
|
|
|
|
// 默认为搜车指令,或者你可以选择抛出异常
|
|
|
|
|
|
type = VehicleCommandType.unknown;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return VehicleCommand(type: type, params: params, error: error);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将命令转换为字符串(用于API请求)
|
|
|
|
|
|
String get commandString => type.name;
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
String toString() {
|
|
|
|
|
|
return 'VehicleCommand(command: ${type.name}, params: $params)';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|