app.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from flask import Flask, request, jsonify
  2. import json
  3. from csvread import read_dddd_data, read_satellite_data, get_position_by_time
  4. from stkcom import *
  5. # 开启http server
  6. app = Flask(__name__)
  7. # 存储dddd数据、satellite数据
  8. dddd_array = []
  9. satellite_array = []
  10. global final_data
  11. final_data = []
  12. @app.route("/")
  13. def hello_world():
  14. return "这是电子gf弹道服务器"
  15. @app.route("/traj", methods=["GET","POST"])
  16. def get_traj():
  17. # 1. 启动STK
  18. root = activeSTK()
  19. if root is None:
  20. print("启动STK失败")
  21. else:
  22. print("启动STK成功")
  23. # axois用这个
  24. # xdparams = request.get_json(force=True)
  25. # postman用这个
  26. xdjson = request.form.to_dict()
  27. # xdjson = json.loads(xdparams)
  28. # 1.解析想定
  29. (
  30. begintime,
  31. endtime,
  32. timestep,
  33. id,
  34. xdname,
  35. redunit,
  36. blueunit,
  37. satellite,
  38. center,
  39. ) = get_data_from_json(xdjson)
  40. starttime, stoptime, duration = datetime_transform(begintime, endtime)
  41. # 2.创建场景
  42. ret = createScenario(
  43. root,
  44. xdname,
  45. starttime,
  46. stoptime,
  47. duration
  48. )
  49. # 3.导出轨迹文件
  50. exportMovementFile(root, redunit, satellite, starttime, stoptime, timestep)
  51. # 从轨迹文件中读取轨迹数据
  52. for i in range(len(redunit)):
  53. str = redunit[i].get("name") + '.csv'
  54. dddd_array.append(read_dddd_data("C:\\fire\\simulation",str))
  55. for i in range(len(satellite)):
  56. str = satellite[i].get("name") + '.csv'
  57. satellite_array.append((read_satellite_data("C:\\fire\\simulation",str)))
  58. # 获取最终要返回给请求端的json list
  59. global final_data
  60. final_data = get_position_by_time(starttime, stoptime, dddd_array, satellite_array, timestep, redunit, satellite)
  61. return jsonify({"status":"success"},{"data":final_data}), 200
  62. @app.route("/pos", methods=["GET","POST"])
  63. def get_pos():
  64. if len(final_data) == 0:
  65. return jsonify({}), 200
  66. posjson = request.form.to_dict()
  67. currentTime = posjson['simulation_time']
  68. print(currentTime)
  69. currentTime = datetime.datetime.strptime(currentTime, "%Y-%m-%d %H:%M:%S.%f")
  70. # 越界检查
  71. starttime = datetime.datetime.strptime(final_data[0]["dateTime"], "%d %b %Y %H:%M:%S.%f")
  72. stoptime = datetime.datetime.strptime(final_data[-2]["dateTime"], "%d %b %Y %H:%M:%S.%f")
  73. 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()
  74. if currentTime < starttime or currentTime > stoptime:
  75. return jsonify({"status":"success"},{"data":None}), 200
  76. else:
  77. # 计算下标
  78. idx = int((currentTime - starttime).total_seconds()/steptime)
  79. return jsonify({"status":"success"},{"data":final_data[idx]}), 200