-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_wrapper.py
106 lines (75 loc) · 2.66 KB
/
web_wrapper.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
#!/usr/bin/env python3
import inspect
import os
import subprocess
import sys
import threading
from typing import Optional
from flask import Flask, json, request, send_from_directory
from flask_cors import CORS
DEFAULT_SCORE = 58 # TODO estimation
app = Flask(__name__)
CORS(app)
process: Optional[subprocess.Popen] = None
@app.route('/<path:filename>')
def send_report(filename):
if '.' not in filename:
if not filename.endswith('/'):
filename += '/'
filename += 'index.html'
print("File :", filename)
return send_from_directory('web/public', filename)
def run_main_program(args: list):
global process
if process is not None:
kill_main_program()
filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
process = subprocess.Popen([sys.executable, path + "/main.py", "--log-level=debug", *args])
def kill_main_program():
global process
if process is not None:
process.kill()
process = None
def run_main_program_with_strategy(strategy: str):
run_main_program(["--strategy", strategy])
def run_main_program_with_remote():
run_main_program(["--server"])
@app.route('/api/strategies', methods=['GET'])
def get_strategies():
filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
return json.dumps([entry.name[:-3] for entry in os.scandir(path + '/strategies') if not entry.is_dir()])
@app.route('/api/run_strategy', methods=['POST'])
def run_strategy():
print("run_strategy", request.json)
run_main_program_with_strategy(request.json["strategy"])
return json.dumps({"status": "ok"})
@app.route('/api/run_remote', methods=['POST'])
def run_remote():
print("run_remote")
run_main_program_with_remote()
return json.dumps({"status": "ok"})
@app.route('/api/kill', methods=['POST'])
def kill():
print("kill")
kill_main_program()
return json.dumps({"status": "ok"})
@app.route('/api/get_score', methods=['get'])
def get_score():
if process is not None:
process.poll()
score = process.returncode if process else None
if score is not None:
score = score - 1000
if score is not None: # and not (0 <= score <= 200):
score = DEFAULT_SCORE
print("get_score", score)
return json.dumps({"status": "ok", "score": score})
def run_web_server():
app.run(host='0.0.0.0')
if __name__ == '__main__':
threading.Thread(target=run_web_server).start()
os.system("DISPLAY=:0 /usr/bin/chromium-browser --kiosk http://127.0.0.1:5000/embedded/home")
if process is not None:
process.kill()