Files
ai_chat_assistant/lib/services/control_recognition_service.dart
Chen Li 130755f9e1 0812
2025-08-12 13:36:42 +08:00

70 lines
2.2 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/vehicle_cmd.dart';
import '../models/vehicle_cmd_response.dart';
import 'vehicle_state_service.dart';
class VehicleCommandService {
// final VehicleStateService vehicleStateService = VehicleStateService();
Future<VehicleCommandResponse?> getCommandFromText(String text) async {
try {
final uri = Uri.parse(
'http://143.64.185.20:18606/control');
final response = await http.post(
uri,
headers: {'Content-Type': 'application/json'},
body: json.encode({'text': text}),
);
if (response.statusCode == 200) {
final decoded = json.decode(response.body);
final tips = decoded['tips'];
final commandList = decoded['commands'];
List<VehicleCommand> vehicleCommandList = (commandList as List)
.map<VehicleCommand>((item) => VehicleCommand.fromString(
item['command'] as String,
item['params'] as Map<String, dynamic>?,
item['error'] ?? ''))
.toList();
return VehicleCommandResponse(tips: tips, commands: vehicleCommandList);
} else {
print(
'Vehicle command query failed: ${response.statusCode}, ${response.body}');
return null;
}
} catch (e) {
print('Error during vehicle command processing: $e');
return null;
}
}
// 执行车辆控制命令的方法
Future<bool> executeCommand(VehicleCommand command) async {
try {
final uri = Uri.parse('http://143.64.185.20:18607/executeCommand');
final response = await http.post(
uri,
headers: {'Content-Type': 'application/json'},
body: json.encode({
'command': command.commandString, // 使用getter转换为字符串
'params': command.params,
}),
);
if (response.statusCode == 200) {
print('命令执行成功: ${command.commandString}');
return true;
} else {
print('命令执行失败: ${response.statusCode}, ${response.body}');
return false;
}
} catch (e) {
print('命令执行出错: $e');
return false;
}
}
}