import requests
import os
import sys

def upload_mission(drone_ip, file_path):
    """上传一个JSON任务文件到无人机"""
    if not os.path.exists(file_path):
        print(f"Error: File not found at {file_path}")
        return

    url = f"http://{drone_ip}:5000/missions"
    print(f"正在上传 {file_path} 到 {url} ...")
    
    try:
        with open(file_path, 'rb') as f:
            files = {'file': (os.path.basename(file_path), f, 'application/json')}
            response = requests.post(url, files=files, timeout=10)
            
            # 检查HTTP响应状态码
            response.raise_for_status()
            
            print("上传成功!")
            print("无人机端响应:", response.json())

    except requests.exceptions.RequestException as e:
        print(f"上传过程中发生错误: {e}")

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print("用法: python ground_station_client.py [无人机IP地址] [JSON文件路径]")
        print("示例: python ground_station_client.py 192.168.1.10 ./missions/rescue_mission.json")
        sys.exit(1)
    
    drone_ip_address = sys.argv[1]
    mission_file_path = sys.argv[2]
    
    upload_mission(drone_ip_address, mission_file_path)
