-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotFlask.py
171 lines (142 loc) · 4.25 KB
/
RobotFlask.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import flask
from flask import request, jsonify, render_template
from os import getcwd
import multiprocessing
from multiprocessing import Manager
import PythonExecutor
from time import sleep
#import json
UPLOAD_FOLDER = getcwd() + '/userscripts/'
app = flask.Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
killflag = Manager().dict()
killflag["kill"] = False
process: multiprocessing.Process = multiprocessing.Process()
lastTB = []
blocklyActive = False
@app.route('/')
def home():
return render_template('index.html')
#region Blockly
@app.route('/blockly')
def blockly():
return render_template('blockly.html')
@app.route('/media/<file>')
def redirect(file):
return flask.redirect('/static/blockly/media/' + file, 302)
@app.route('/favicon.ico')
def favicon():
return app.send_static_file('favicon.ico')
@app.route("/stopBlockly")
def stop():
killflag["kill"] = True
return jsonify(success=True)
@app.route('/runBlocklyCode', methods = ["POST"])
def runBlocklyCode():
import Blockly
global blocklyActive
#blocklyActive = True
Blockly.runCode(request, killflag)
return jsonify(success=True)
@app.route('/reloadconfig')
def reloadconfig():
import Blockly
Blockly.load_config()
return "Config reloaded"
@app.route('/restoreconfig')
def restoreconfig():
import Blockly
Blockly.restore_config()
return "Config restored to default"
@app.route('/config')
def viewconfig():
import Blockly
return jsonify(Blockly.config)
@app.route('/changeconfig')
def changeconfig():
import Blockly
if 'value' in request.args and 'key' in request.args:
Blockly.setconfig(str(request.args['key']), float(request.args['value']))
return "Key updated with value."
else:
return "Error. Missing value or key."
#endregion
#region calib
@app.route('/calibrate')
def calibrateUI():
return render_template("CalibrationPage.html")
@app.route('/calib_action')
def custom_calib_req():
import Blockly
#nokill = {"kill": False}
if 'type' not in request.args:
return "Type arg missing."
reqtype = request.args['type']
if reqtype not in ["forward", "turn"]:
return "Type not valid. Try forward / turn"
else:
p = multiprocessing.Process(target=Blockly.start_action_thread, args=(reqtype,))
p.start()
return "200 ok"
@app.route('/calib_response')
def handle_change_calib():
import Blockly
if ('type' not in request.args) or ('dir' not in request.args):
return "Missing type or dir"
reqtype = request.args['type']
if reqtype not in ["forward", "turn"]:
return "Type not valid. Try forward / turn"
reqdir = request.args['dir']
try:
reqdir = int(reqdir)
except ValueError:
return "Request direction is not int"
Blockly.configUI(reqtype, reqdir)
return "200 ok"
#endregion
#region Python
@app.route('/python')
def index():
return render_template('PythonExecutor.html')
@app.route('/uploadPython', methods=['POST'])
def upload():
PythonExecutor.upload_file(app, request)
return "200 OK"
@app.route("/runPython")
def runScript():
global lastTB
global process
manager = multiprocessing.Manager()
lastTB = manager.list()
process = multiprocessing.Process(target=PythonExecutor.runUserScript, args=(lastTB,))
process.start()
return "Code running."
@app.route("/traceback")
def getTraceback():
global lastTB
global blocklyActive
if len(lastTB) > 0:
return lastTB.pop(0)
if blocklyActive:
return "Blockly was started on the robot. Please reboot to use full functionality."
return ""
@app.route("/prints")
def getPrints():
with open("userscripts/printlog.txt") as f:
lines = f.readlines()
return ''.join(lines)
@app.route("/stopPython")
def stopScript():
global process
if process is not None and process.is_alive():
process.terminate()
with open("userscripts/printlog.txt", "a") as file:
file.write("---SCRIPT STOPPED---\n")
sleep(0.1)
import motoroff
motoroff.none()
return "Code stopped."
#endregion
def startFlask():
#app.run(host="0.0.0.0", port=80, ssl_context=('cert.pem', 'key.pem'))
app.run(host="0.0.0.0", port=80, ssl_context='adhoc') #try to auto gen?