3Dshock/server/main.py

166 lines
3.6 KiB
Python
Raw Normal View History

2021-03-08 03:38:49 +00:00
import os
import json
2021-03-08 04:43:47 +00:00
import time
from datetime import datetime
2021-03-08 03:38:49 +00:00
from pathlib import Path
2021-03-08 04:43:47 +00:00
from flask import Flask, request, abort, send_from_directory
2021-03-08 02:16:35 +00:00
from flask_cors import CORS
2021-03-11 02:22:21 +00:00
import power, capture, download, settings
import logging
log = logging.getLogger('werkzeug')
if not settings.DEBUG:
log.setLevel(logging.ERROR)
2021-03-08 04:43:47 +00:00
2021-03-08 03:38:49 +00:00
build_folder = Path('../client/build')
output_folder = Path('./output')
app = Flask(__name__, static_folder=str(build_folder), static_url_path='')
2021-03-08 02:16:35 +00:00
CORS(app)
2021-03-11 01:44:32 +00:00
STANDBY = 0
WARMUP = 1
2021-03-11 02:22:21 +00:00
CAPTURING_PHOTO = 2
CAPTURING_GRID = 3
WRITING = 4
DOWNLOADING = 5
2021-03-11 01:44:32 +00:00
status = STANDBY
2021-03-08 05:49:47 +00:00
@app.route('/api/status', methods=['GET'])
def status_get():
return {'status': status}
2021-03-08 03:38:49 +00:00
@app.route('/api/clients', methods=['POST'])
2021-03-08 04:43:47 +00:00
def clients_post():
2021-03-08 03:38:49 +00:00
content = request.json
phone = str(content['phone'])
for i in range(1, 100):
2021-03-11 00:49:49 +00:00
suffix = str(i).zfill(2)
2021-03-08 03:38:49 +00:00
folder = phone + '_' + suffix
path = output_folder / folder
if not path.exists():
break
content['date'] = datetime.now().strftime('%Y-%m-%d')
content['time'] = datetime.now().strftime('%H:%M:%S')
2021-03-08 03:38:49 +00:00
path.mkdir()
info_file = path / 'info.txt'
2021-03-08 03:38:49 +00:00
info_file.touch()
info_file.write_text(json.dumps(content, indent=4))
client_id = folder
2021-03-11 02:22:21 +00:00
print('POST client:', content, 'cid:', client_id)
2021-03-08 03:38:49 +00:00
return {'client_id': client_id}
2021-03-08 04:43:47 +00:00
@app.route('/api/clients/<cid>', methods=['GET'])
def clients_get(cid):
folder = cid
path = output_folder / cid
if not path.exists():
abort(404)
info_file = path / 'info.txt'
2021-03-08 04:43:47 +00:00
info_text = info_file.read_text()
res = json.loads(info_text)
photo_glob = path.glob('*.jpg')
res['has_photos'] = bool(list(photo_glob))
2021-03-11 02:22:21 +00:00
print('GET client:', cid, 'res:', res)
2021-03-08 04:43:47 +00:00
return res
@app.route('/api/clients/<cid>/session', methods=['GET'])
def session_get(cid):
folder = cid
path = output_folder / cid
if not path.exists():
abort(404)
photo_glob = path.glob('*.jpg')
res = {}
2021-03-08 05:49:47 +00:00
res['photos'] = sorted([x.name for x in photo_glob])
2021-03-08 04:43:47 +00:00
return res
@app.route('/api/clients/<cid>/session', methods=['DELETE'])
def session_delete(cid):
folder = cid
path = output_folder / cid
if not path.exists():
abort(404)
photo_glob = path.glob('*.jpg')
for p in photo_glob:
p.unlink()
2021-03-11 02:22:21 +00:00
print('DELETE session:', cid)
2021-03-08 04:43:47 +00:00
return ''
@app.route('/api/clients/<cid>/session', methods=['POST'])
def session_post(cid):
content = request.json
light_time = content.get('light_time', 5000)
2021-03-08 05:49:47 +00:00
global status
2021-03-11 02:22:21 +00:00
print('POST session:', cid)
2021-03-08 04:43:47 +00:00
folder = cid
path = output_folder / cid
if not path.exists():
abort(404)
# go through the photo taking process
2021-03-11 00:47:04 +00:00
try:
# warmup
2021-03-11 02:22:21 +00:00
status = WARMUP
2021-03-11 00:47:04 +00:00
power.lights_on()
2021-03-11 02:22:21 +00:00
time.sleep(2)
2021-03-11 00:47:04 +00:00
power.lights_off()
2021-03-11 02:22:21 +00:00
time.sleep(1)
2021-03-11 00:47:04 +00:00
except BaseException as e:
print('Problem with lights: {} - {}'.format(e.__class__.__name__, str(e)))
print()
print('Are you sure the system is connected?')
print()
abort(500)
2021-03-08 04:43:47 +00:00
# capture
2021-03-11 02:22:21 +00:00
status = CAPTURING_PHOTO
2021-03-08 04:43:47 +00:00
power.lights_on()
time.sleep(0.1)
2021-03-08 04:43:47 +00:00
capture.trigger_capture()
time.sleep(light_time / 1000)
2021-03-11 02:22:21 +00:00
power.lights_off()
2021-03-08 05:49:47 +00:00
2021-03-11 01:44:32 +00:00
status = DOWNLOADING
2021-03-08 04:43:47 +00:00
download.download_all_photos(path)
2021-03-11 02:22:21 +00:00
time.sleep(3)
2021-03-08 04:43:47 +00:00
2021-03-11 01:44:32 +00:00
status = STANDBY
2021-03-11 02:22:21 +00:00
print('Finished.')
2021-03-08 04:43:47 +00:00
return ''
2021-03-08 02:16:35 +00:00
@app.route('/')
def index():
return app.send_static_file('index.html')
2021-03-08 05:49:47 +00:00
@app.errorhandler(404)
def not_found(e):
return app.send_static_file('index.html')
2021-03-08 04:43:47 +00:00
@app.route('/output/<path:filename>')
def output(filename):
return send_from_directory('output/', filename)
app.run(host='0.0.0.0')