from flask import Flask, request, jsonify import json from csvread import read_dddd_data, read_satellite_data, get_position_by_time from stkcom import * # 开启http server app = Flask(__name__) # 存储dddd数据、satellite数据 dddd_array = [] satellite_array = [] global final_data final_data = [] @app.route("/") def hello_world(): return "这是电子gf弹道服务器" @app.route("/traj", methods=["GET","POST"]) def get_traj(): # 1. 启动STK root = activeSTK() if root is None: print("启动STK失败") else: print("启动STK成功") # axois用这个 # xdparams = request.get_json(force=True) # postman用这个 xdjson = request.form.to_dict() # xdjson = json.loads(xdparams) # 1.解析想定 ( begintime, endtime, timestep, id, xdname, redunit, blueunit, satellite, center, ) = get_data_from_json(xdjson) starttime, stoptime, duration = datetime_transform(begintime, endtime) # 2.创建场景 ret = createScenario( root, xdname, starttime, stoptime, duration ) # 3.导出轨迹文件 exportMovementFile(root, redunit, satellite, starttime, stoptime, timestep) # 从轨迹文件中读取轨迹数据 for i in range(len(redunit)): str = redunit[i].get("name") + '.csv' dddd_array.append(read_dddd_data("C:\\fire\\simulation",str)) for i in range(len(satellite)): str = satellite[i].get("name") + '.csv' satellite_array.append((read_satellite_data("C:\\fire\\simulation",str))) # 获取最终要返回给请求端的json list global final_data final_data = get_position_by_time(starttime, stoptime, dddd_array, satellite_array, timestep, redunit, satellite) return jsonify({"status":"success"},{"data":final_data}), 200 @app.route("/pos", methods=["GET","POST"]) def get_pos(): if len(final_data) == 0: return jsonify({}), 200 posjson = request.form.to_dict() currentTime = posjson['simulation_time'] print(currentTime) currentTime = datetime.datetime.strptime(currentTime, "%Y-%m-%d %H:%M:%S.%f") # 越界检查 starttime = datetime.datetime.strptime(final_data[0]["dateTime"], "%d %b %Y %H:%M:%S.%f") stoptime = datetime.datetime.strptime(final_data[-2]["dateTime"], "%d %b %Y %H:%M:%S.%f") steptime = (datetime.datetime.strptime(final_data[1]["dateTime"], "%d %b %Y %H:%M:%S.%f") - datetime.datetime.strptime(final_data[0]["dateTime"], "%d %b %Y %H:%M:%S.%f")).total_seconds() if currentTime < starttime or currentTime > stoptime: return jsonify({"status":"success"},{"data":None}), 200 else: # 计算下标 idx = int((currentTime - starttime).total_seconds()/steptime) return jsonify({"status":"success"},{"data":final_data[idx]}), 200