Files
ai_chat_assistant/lib/services/control_recognition_service.dart

61 lines
2.0 KiB
Dart
Raw Permalink Normal View History

2025-08-12 13:36:42 +08:00
import 'dart:convert';
import 'package:ai_chat_core/ai_chat_core.dart';
2025-08-12 13:36:42 +08:00
import 'package:http/http.dart' as http;
/// 车辆命令服务 - 负责与后端交互以获取和处理车辆控制命令
2025-08-12 13:36:42 +08:00
class VehicleCommandService {
// final VehicleStateService vehicleStateService = VehicleStateService();
Future<VehicleCommandResponse?> getCommandFromText(String text) async {
try {
2025-08-22 17:35:02 +08:00
final uri = Uri.parse('http://143.64.185.20:18606/control');
2025-08-12 13:36:42 +08:00
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(
2025-08-22 17:35:02 +08:00
item['command'] as String,
item['params'] as Map<String, dynamic>?,
item['error'] ?? ''))
2025-08-12 13:36:42 +08:00
.toList();
return VehicleCommandResponse(tips: tips, commands: vehicleCommandList);
} else {
2025-09-30 14:48:12 +08:00
Logger.e(
2025-08-12 13:36:42 +08:00
'Vehicle command query failed: ${response.statusCode}, ${response.body}');
return null;
}
} catch (e) {
2025-09-30 14:48:12 +08:00
Logger.e('Error during vehicle command processing: $e');
2025-08-12 13:36:42 +08:00
return null;
}
}
2025-08-22 17:35:02 +08:00
Future<String> getControlResponse(List<String> successCommandList) async {
String reply = "";
2025-08-12 13:36:42 +08:00
try {
2025-08-22 17:35:02 +08:00
final uri = Uri.parse('http://143.64.185.20:18606/control_resp');
2025-08-12 13:36:42 +08:00
final response = await http.post(
uri,
headers: {'Content-Type': 'application/json'},
2025-08-22 17:35:02 +08:00
body: json.encode(successCommandList),
2025-08-12 13:36:42 +08:00
);
if (response.statusCode == 200) {
2025-08-22 17:35:02 +08:00
return response.body;
2025-08-12 13:36:42 +08:00
} else {
2025-09-30 14:48:12 +08:00
Logger.e("请求控制回复失败: ${response.statusCode}");
2025-08-12 13:36:42 +08:00
}
} catch (e) {
2025-09-30 14:48:12 +08:00
Logger.e('请求控制回复异常: $e');
2025-08-12 13:36:42 +08:00
}
2025-08-22 17:35:02 +08:00
return reply;
2025-08-12 13:36:42 +08:00
}
}