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 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 vehicleCommandList = (commandList as List) .map((item) => VehicleCommand.fromString( item['command'] as String, item['params'] as Map?, 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 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; } } }