-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimulation_server.py
117 lines (92 loc) · 3.53 KB
/
simulation_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
import os.path
import threading
import time
import sys
from flask import Flask, request, abort, send_file, render_template, Response
from flask_cors import CORS
##UNCOMMENT LINE IF TESTING ON LOCAL MACHINE
##sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
from PythonClient.multirotor.control.simulation_task_manager import SimulationTaskManager
app = Flask(__name__, template_folder="./templates")
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
CORS(app)
task_dispatcher = SimulationTaskManager()
threading.Thread(target=task_dispatcher.start).start()
task_number = 1
# For Frontend to fetch all missions available to use
# @app.route('/mission', methods=['GET'])
# def mission():
# directory = '../multirotor/mission'
# return [file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))]
@app.route('/addTask', methods=['POST'])
def add_task():
global task_number
uuid_string = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + "_Batch_" + str(task_number)
task_dispatcher.add_task(request.get_json(), uuid_string)
task_number += 1
print(f"New task added to queue, currently {task_dispatcher.mission_queue.qsize()} in queue")
return uuid_string
@app.route('/currentRunning', methods=['GET'])
def get_current_running():
current_task_batch = task_dispatcher.get_current_task_batch()
if current_task_batch == "None":
return f"{'None'}, {task_dispatcher.mission_queue.qsize()}"
else:
return f"{'Running'}, {task_dispatcher.mission_queue.qsize()}"
@app.route('/report')
@app.route('/report/<path:dir_name>')
def get_report(dir_name=''):
report_root_dir = os.path.join(os.path.expanduser("~"), "Documents", "AirSim", "report")
dir_path = os.path.join(report_root_dir, dir_name)
if not os.path.exists(dir_path):
return abort(404)
if os.path.isfile(dir_path):
return send_file(dir_path)
files = os.listdir(dir_path)
return render_template('files.html', files=files)
@app.route('/stream/<drone_name>/<camera_name>')
def stream(drone_name, camera_name):
if task_dispatcher.unreal_state['state'] == 'idle':
return "No task running"
else:
try:
return Response(
task_dispatcher.get_stream(drone_name, camera_name),
mimetype='multipart/x-mixed-replace; boundary=frame'
)
except Exception as e:
print(e)
return "Error"
# @app.route('/uploadMission', methods=['POST'])
# def upload_file():
# file = request.files['file']
# filename = file.filename
# custom_mission_dir = '../multirotor/mission/custom'
# path = os.path.join(custom_mission_dir, filename)
# file.save(path)
# return 'File uploaded'
# def update_settings_json(drone_number, separation_distance):
# SettingGenerator(drone_number, separation_distance)
@app.route('/state', methods=['GET'])
def get_state():
"""
For unreal engine to check the current run state
:return: json obj consists of the current state with this specific format
{
"state": "idle"
}
or
{
"state": "start"
}
any other state will be not accepted by the unreal engine side and the change will be ignored
"""
return task_dispatcher.unreal_state
@app.route('/cesiumCoordinate', methods=['GET'])
def get_map():
return task_dispatcher.load_cesium_setting()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
# makes it discoverable by other devices in the network