115 lines
3.1 KiB
Python
Executable File
115 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
from flask import Flask, redirect, request, render_template, send_from_directory, session
|
|
import json
|
|
import os
|
|
from os.path import join
|
|
import sqlite3
|
|
from typing import Optional
|
|
import uuid
|
|
from . import db, config, scanner, calibration
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = os.urandom(20).hex()
|
|
|
|
|
|
def get_calibration(conn: sqlite3.Connection) -> Optional[db.Calibration]:
|
|
calibration_id = session.get('calibration_id', None)
|
|
if calibration_id is None:
|
|
return None
|
|
|
|
return db.Calibration.get_from_id(calibration_id, conn)
|
|
|
|
|
|
@app.route("/")
|
|
def hello_world():
|
|
conn = db.get()
|
|
objects = db.Object.all(conn)
|
|
return render_template('index.html', objects=objects)
|
|
|
|
|
|
@app.route("/create-object/", methods=["POST"])
|
|
def create_object():
|
|
conn = db.get()
|
|
with conn:
|
|
db.Object.create(request.form.get('name'), conn)
|
|
return redirect('/')
|
|
|
|
|
|
@app.route('/object/<id>')
|
|
def object(id: int):
|
|
conn = db.get()
|
|
object = db.Object.get_from_id(id, conn)
|
|
return render_template('object.html', object=object, calibration=get_calibration(conn))
|
|
|
|
|
|
@app.route('/scan/<id>')
|
|
def scan(id: int):
|
|
conn = db.get()
|
|
object = db.Object.get_from_id(id, conn)
|
|
return render_template('object.html', object=object)
|
|
|
|
|
|
@app.route("/calibrate/")
|
|
def calibrate():
|
|
conn = db.get()
|
|
with conn:
|
|
calibration = db.Calibration.create(conn)
|
|
session['calibration_id'] = calibration.id
|
|
return render_template('calibrate.html', calibration=calibration)
|
|
|
|
|
|
@app.route("/api/preview/<id>")
|
|
def preview(id: int):
|
|
conn = db.get()
|
|
db.Object.get_from_id(id, conn)
|
|
capture_uuid = uuid.uuid4()
|
|
if scanner.capture(join(config.DATA_DIR, str(id), 'previews', str(capture_uuid) + '.jpg')):
|
|
return str(capture_uuid)
|
|
else:
|
|
return "Impossible de capturer l'image.", 500
|
|
|
|
|
|
@app.route("/api/scan-for-calibration/<id>")
|
|
def scan_calibration(id: int):
|
|
conn = db.get()
|
|
calibration = db.Calibration.get_from_id(id, conn)
|
|
|
|
def generate():
|
|
length = len(config.LEDS_UUIDS)
|
|
for index, led_uuid in enumerate(scanner.scan(join(config.CALIBRATION_DIR, id))):
|
|
yield f"{led_uuid},{(index+1)/length}\n"
|
|
|
|
with conn:
|
|
calibration.state = db.CalibrationState.HasData
|
|
calibration.save(conn)
|
|
|
|
return app.response_class(generate(), mimetype='text/plain')
|
|
|
|
|
|
@app.route("/api/calibrate/<id>")
|
|
def run_calibration(id: int):
|
|
conn = db.get()
|
|
db.Calibration.get_from_id(id, conn)
|
|
calibration_json = calibration.calibrate(join(config.CALIBRATION_DIR, str(id)))
|
|
with open(join(config.CALIBRATION_DIR, str(id), 'calibration.json'), 'w') as f:
|
|
json.dump(calibration_json, f, indent=4)
|
|
return 'ok'
|
|
|
|
|
|
@app.route("/calibration/<id>")
|
|
def calibration_page(id: int):
|
|
conn = db.get()
|
|
calibration = db.Calibration.get_from_id(id, conn)
|
|
return render_template('calibration.html', calibration=calibration)
|
|
|
|
|
|
@app.route('/static/<path:path>')
|
|
def send_static(path):
|
|
return send_from_directory('static', path)
|
|
|
|
|
|
@app.route('/data/<path:path>')
|
|
def send_data(path):
|
|
return send_from_directory('data', path)
|