Files
ai_chat_assistant/lib/services/control_recognition_service.dart
guangfei.zhao a7ed838c92 feat: 新增ai_chat_core,将core 和 widget 分离(未完成)
迁移了 models enums, utils, http 封装,还有一些extensions;service 只迁移了 sse service
2025-09-29 18:33:46 +08:00

61 lines
2.0 KiB
Dart

import 'dart:convert';
import 'package:ai_chat_core/ai_chat_core.dart';
import 'package:http/http.dart' as http;
/// 车辆命令服务 - 负责与后端交互以获取和处理车辆控制命令
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<String> getControlResponse(List<String> successCommandList) async {
String reply = "";
try {
final uri = Uri.parse('http://143.64.185.20:18606/control_resp');
final response = await http.post(
uri,
headers: {'Content-Type': 'application/json'},
body: json.encode(successCommandList),
);
if (response.statusCode == 200) {
return response.body;
} else {
print("请求控制回复失败: ${response.statusCode}");
}
} catch (e) {
print('请求控制回复异常: $e');
}
return reply;
}
}