-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
475 lines (408 loc) · 13.8 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
"""
This module provides the http web server exposing the
CoderBot REST API and static resources
"""
import os
import json
import logging
import logging.handlers
import subprocess
import connexion
from flask import (render_template,
request,
send_file,
Response,
jsonify,
send_from_directory,
redirect)
from flask_babel import Babel
from flask_cors import CORS
from werkzeug.datastructures import Headers
from coderbot import CoderBot
from program import ProgramEngine, Program
from config import Config
# Logging configuration
logger = logging.getLogger()
logger.setLevel(logging.INFO)
sh = logging.StreamHandler()
formatter = logging.Formatter('%(message)s')
sh.setFormatter(formatter)
#logger.addHandler(sh)
## (Connexion) Flask app configuration
# Serve a custom version of the swagger ui (Jinja2 templates) based on the default one
# from the folder 'swagger-ui'. Clone the 'swagger-ui' repository inside the backend folder
connexionApp = connexion.App(__name__, swagger_ui=True, swagger_path='swagger-ui/')
# Connexion wraps FlaskApp, so app becomes connexionApp.app
app = connexionApp.app
# Access-Control-Allow-Origin
CORS(app)
babel = Babel(app)
app.debug = False
app.prog_engine = ProgramEngine.get_instance()
app.prog = None
app.shutdown_requested = False
## New API and web application
# API v2 is defined in v2.yml and its methods are in api.py
connexionApp.add_api('v2.yml')
@app.route('/vue/<path:filename>')
def serve_vue_app(filename):
"""
Serve (a build of) the new Vue application
"dist" is the output of `npm run build` from the 'vue-app' repository
"""
return send_from_directory('dist', filename)
@app.route('/docs/')
def redirect_docs_app():
return redirect('/docs/index.html', code=302)
@app.route('/docs/<path:subpath>')
def serve_docs_app(subpath):
"""
Serve (a build of) the documentation
'cb_docs' is the output of `npx vuepress build pages/`
from the 'docs' repository
"""
print("Running docs path")
print(subpath)
if (subpath[-1] == '/'):
subpath = subpath + 'index.html'
return send_from_directory('cb_docs', subpath)
@app.route('/')
def redirect_vue_app():
return redirect('/vue/index.html', code=302)
## Legacy API and web application
@app.route("/old")
def serve_legacy():
"""
Serve the the legacy web application
"""
return render_template('main.html',
host=request.host[:request.host.find(':')],
locale=get_locale(),
config=app.bot_config,
program_level=app.bot_config.get("prog_level", "std"),
cam=False,
cnn_model_names={})
@babel.localeselector
def get_locale():
# otherwise try to guess the language from the user accept
# header the browser transmits.
loc = request.accept_languages.best_match(['it', 'en', 'fr', 'es'])
if loc is None:
loc = 'en'
return loc
# Workaround: serve the 'static' subfolders with 'send_from_directory'
# (connexion wrapper ignores `static_url_path`)
@app.route('/css/<path:filename>')
def render_static_assets0(filename):
return send_from_directory('static/css', filename)
@app.route('/fonts/<path:filename>')
def render_static_assets1(filename):
return send_from_directory('static/fonts', filename)
@app.route('/images/<path:filename>')
def render_static_assets2(filename):
return send_from_directory('static/images', filename)
@app.route('/js/<path:filename>')
def render_static_assets3(filename):
return send_from_directory('static/js', filename)
@app.route('/media/<path:filename>')
def render_static_assets4(filename):
return send_from_directory('static/media', filename)
def updateDict(oldDict, updatedValues):
"""
Update the keys of oldDict appearing in updatedValues with the values in
updatedValues
"""
result = oldDict
for key, value in updatedValues.items():
result[key] = value
return result
@app.route("/config", methods=["POST"])
def handle_config():
"""
Overwrite configuration file on disk and reload it
"""
Config.write(updateDict(app.bot_config, request.form))
app.bot_config = Config.get()
return "ok"
@app.route("/config", methods=["GET"])
def returnConfig():
"""
Expose configuration as JSON
"""
app.bot_config = Config.get()
return jsonify(app.bot_config)
@app.route("/wifi", methods=["POST"])
def handle_wifi():
"""
Passes the received Wi-Fi configuration to the wifi.py script, applying it.
Then reboots
"""
mode = request.form.get("wifi_mode")
ssid = request.form.get("wifi_ssid")
psk = request.form.get("wifi_psk")
logging.info("mode " + mode +" ssid: " + ssid + " psk: " + psk)
client_params = " \"" + ssid + "\" \"" + psk + "\"" if ssid != "" and psk != "" else ""
logging.info(client_params)
os.system("sudo ./wifi.py updatecfg " + mode + client_params)
os.system("sudo reboot")
if mode == "ap":
return "http://coder.bot"
return "http://coderbot.local"
@app.route("/bot", methods=["GET"])
def handle_bot():
"""
Execute a bot command
"""
bot = CoderBot.get_instance()
try:
cam = Camera.get_instance()
motion = Motion.get_instance()
except:
cam = None
motion = None
audio = None
cmd = request.args.get('cmd')
param1 = request.args.get('param1')
param2 = request.args.get('param2')
print('/bot', json.dumps(request.args))
if cmd == "move":
bot.move(speed=int(param1), elapse=float(param2))
elif cmd == "turn":
bot.turn(speed=int(param1), elapse=float(param2))
elif cmd == "move_motion":
motion.move(dist=float(param2))
elif cmd == "turn_motion":
motion.turn(angle=float(param2))
elif cmd == "stop":
bot.stop()
try:
motion.stop()
except Exception:
logging.warning("Camera not present")
elif cmd == "take_photo":
try:
cam.photo_take()
except Exception:
logging.warning("Camera not present")
elif cmd == "video_rec":
try:
cam.video_rec()
except Exception:
logging.warning("Camera not present")
elif cmd == "video_stop":
try:
cam.video_stop()
except Exception:
logging.warning("Camera not present")
elif cmd == "say":
logging.info("say: " + str(param1) + " in: " + str(get_locale()))
elif cmd == "halt":
logging.info("shutting down")
bot.halt()
elif cmd == "restart":
logging.info("restarting bot")
bot.restart()
elif cmd == "reboot":
logging.info("rebooting")
bot.reboot()
return "ok"
@app.route("/bot/status", methods=["GET"])
def handle_bot_status():
return json.dumps({'status': 'ok'})
def video_stream(a_cam):
while not app.shutdown_requested:
frame = a_cam.get_image_jpeg()
yield ("--BOUNDARYSTRING\r\n" +
"Content-type: image/jpeg\r\n" +
"Content-Length: " + str(len(frame)) + "\r\n\r\n")
yield frame
yield "\r\n"
# Render cam stream
@app.route("/video/stream")
def handle_video_stream():
try:
cam = Camera.get_instance()
h = Headers()
h.add('Age', 0)
h.add('Cache-Control', 'no-cache, private')
h.add('Pragma', 'no-cache')
return Response(video_stream(cam), headers=h, mimetype="multipart/x-mixed-replace; boundary=--BOUNDARYSTRING")
except Exception:
pass
@app.route("/photos", methods=["GET"])
def handle_photos():
"""
Expose the list of taken photos
"""
cam = Camera.get_instance()
logging.info("photos")
return json.dumps(cam.get_photo_list())
@app.route("/photos/<filename>", methods=["GET"])
def handle_photo_get(filename):
cam = Camera.get_instance()
logging.info("media filename: %s", filename)
mimetype = {'jpg': 'image/jpeg', 'mp4': 'video/mp4'}
try:
media_file = cam.get_photo_file(filename)
return send_file(media_file, mimetype=mimetype.get(filename[:-3], 'image/jpeg'), cache_timeout=0)
except picamera.exc.PiCameraError as e:
logging.error("Error: %s", str(e))
@app.route("/photos/<filename>", methods=["PUT"])
def handle_photo_put(filename):
cam = Camera.get_instance()
logging.info("photo update")
data = request.get_data(as_text=True)
data = json.loads(data)
cam.update_photo({"name": filename, "tag": data["tag"]})
return jsonify({"res":"ok"})
@app.route("/photos/<filename>", methods=["DELETE"])
def handle_photo_cmd(filename):
cam = Camera.get_instance()
logging.debug("photo delete")
cam.delete_photo(filename)
return "ok"
@app.route("/program/list", methods=["GET"])
def handle_program_list():
"""
Expose the list of saved programs
"""
logging.debug("program_list")
return json.dumps(app.prog_engine.prog_list())
@app.route("/program/load", methods=["GET"])
def handle_program_load():
"""
Expose a saved program as JSON
"""
logging.debug("program_load")
name = request.args.get('name')
app.prog = app.prog_engine.load(name)
return jsonify(app.prog.as_dict())
@app.route("/program/save", methods=["POST"])
def handle_program_save():
"""
Save the given program
"""
logging.debug("program_save")
name = request.form.get('name')
dom_code = request.form.get('dom_code')
code = request.form.get('code')
prog = Program(name, dom_code=dom_code, code=code)
app.prog_engine.save(prog)
return "ok"
@app.route("/program/delete", methods=["POST"])
def handle_program_delete():
"""
Delete the given saved program
"""
logging.debug("program_delete")
name = request.form.get('name')
app.prog_engine.delete(name)
return "ok"
@app.route("/program/exec", methods=["POST"])
def handle_program_exec():
"""
Execute the given program
"""
logging.debug("program_exec")
name = request.form.get('name')
code = request.form.get('code')
app.prog = app.prog_engine.create(name, code)
return json.dumps(app.prog.execute())
@app.route("/program/end", methods=["POST"])
def handle_program_end():
"""
Stop the program execution
"""
logging.debug("program_end")
if app.prog:
app.prog.end()
app.prog = None
return "ok"
@app.route("/program/status", methods=["GET"])
def handle_program_status():
"""
Expose the program status
"""
logging.debug("program_status")
prog = Program("")
if app.prog:
prog = app.prog
return json.dumps({'name': prog.name, "running": prog.is_running(), "log": app.prog_engine.get_log()})
@app.route("/cnnmodels", methods=["GET"])
def handle_cnn_models_list():
cnn = CNNManager.get_instance()
logging.info("cnn_models_list")
return json.dumps(cnn.get_models())
@app.route("/cnnmodels", methods=["POST"])
def handle_cnn_models_new():
cam = Camera.get_instance()
cnn = CNNManager.get_instance()
logging.info("cnn_models_new")
data = json.loads(request.get_data(as_text=True))
cnn.train_new_model(model_name=data["model_name"],
architecture=data["architecture"],
image_tags=data["image_tags"],
photos_meta=cam.get_photo_list(),
training_steps=data["training_steps"],
learning_rate=data["learning_rate"])
return json.dumps({"name": data["model_name"], "status": 0})
@app.route("/cnnmodels/<model_name>", methods=["GET"])
def handle_cnn_models_status(model_name):
cnn = CNNManager.get_instance()
logging.info("cnn_models_status")
model_status = cnn.get_models().get(model_name)
return json.dumps(model_status)
@app.route("/cnnmodels/<model_name>", methods=["DELETE"])
def handle_cnn_models_delete(model_name):
cnn = CNNManager.get_instance()
logging.info("cnn_models_delete")
model_status = cnn.delete_model(model_name=model_name)
return json.dumps(model_status)
# Spawn a sub-process and execute things there
def execute(command):
"""
Spawn a sub-process and execute the program there, then poll it until
it has finished
"""
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() != None:
break
logging.info(nextline)
yield nextline
def button_pushed():
if app.bot_config.get('button_func') == "startstop":
if app.prog and app.prog.is_running():
app.prog.end()
elif app.prog and not app.prog.is_running():
app.prog.execute()
def remove_doreset_file():
try:
os.remove("/home/pi/doreset")
except OSError:
pass
# Finally, get the server running
def run_server():
bot = None
cam = None
try:
try:
app.bot_config = Config.read()
bot = CoderBot.get_instance(motor_trim_factor=float(app.bot_config.get('move_motor_trim', 1.0)),
encoder=bool(app.bot_config.get('encoder')))
if app.bot_config.get('load_at_start') and app.bot_config.get('load_at_start'):
app.prog = app.prog_engine.load(app.bot_config.get('load_at_start'))
app.prog.execute()
except ValueError as e:
app.bot_config = {}
logging.error(e)
bot.set_callback(bot.GPIOS.PIN_PUSHBUTTON, button_pushed, 100)
remove_doreset_file()
app.run(host="0.0.0.0", port=8080, debug=False, use_reloader=False, threaded=True)
finally:
if cam:
cam.exit()
if bot:
bot.exit()
app.shutdown_requested = True