From e8822a8d3a0069c1f38bfb3c010d08d4566f8b06 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 7 Mar 2021 20:38:49 -0700 Subject: [PATCH 1/3] Add route to create client --- server/main.py | 33 ++++++++++++++++++++++++++++++--- server/output/.gitkeep | 0 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 server/output/.gitkeep diff --git a/server/main.py b/server/main.py index aabbdd4..cc77a02 100644 --- a/server/main.py +++ b/server/main.py @@ -1,10 +1,37 @@ -from flask import Flask +import os +import json +from pathlib import Path +from flask import Flask, request from flask_cors import CORS -build_folder = '../client/build' -app = Flask(__name__, static_folder=build_folder, static_url_path='') +build_folder = Path('../client/build') +output_folder = Path('./output') +app = Flask(__name__, static_folder=str(build_folder), static_url_path='') CORS(app) +@app.route('/api/clients', methods=['POST']) +def clients(): + content = request.json + print('Recieved:', content) + + phone = str(content['phone']) + + for i in range(1, 100): + suffix = str(i).zfill(3) + folder = phone + '_' + suffix + path = output_folder / folder + if not path.exists(): + break + + path.mkdir() + info_file = path / 'info.json' + info_file.touch() + info_file.write_text(json.dumps(content, indent=4)) + + client_id = folder + + return {'client_id': client_id} + @app.route('/') def index(): return app.send_static_file('index.html') diff --git a/server/output/.gitkeep b/server/output/.gitkeep new file mode 100644 index 0000000..e69de29 From 0f0f6b1f7ff8e6e72efa25aab08b9e6a069f8d96 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 7 Mar 2021 20:39:17 -0700 Subject: [PATCH 2/3] Ignore output directory --- server/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/server/.gitignore b/server/.gitignore index 947f30e..0a55b27 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -103,3 +103,4 @@ ENV/ *.swo paramiko.log +output/ From b1aaffa4dbc2b67afc8fb7003d92380057e78bc0 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 7 Mar 2021 21:43:47 -0700 Subject: [PATCH 3/3] Add session API routes --- server/download.py | 13 +++---- server/main.py | 90 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/server/download.py b/server/download.py index 5ad784b..b72b748 100644 --- a/server/download.py +++ b/server/download.py @@ -18,10 +18,10 @@ def download(ip, dest): for f in files: source_file = '/3dscan/' + f - dest_file = dest + f + dest_file = dest / f print('Grabbing file', source_file) sftp.get(source_file, dest_file) - sftp.remove(source_file) + #sftp.remove(source_file) if sftp: sftp.close() if transport: transport.close() @@ -29,10 +29,7 @@ def download(ip, dest): print('Finished downloading from', ip) def download_all_photos(dest): - if not dest.endswith('/'): - raise Exception('Destination must end with /') - - if not os.path.exists(dest): + if not dest.exists(): raise Exception('Destination does not exist') print('Downloading all photos to', dest) @@ -40,3 +37,7 @@ def download_all_photos(dest): for ip in settings.RASPBERRY_IPS: t = threading.Thread(target=download, args=(ip, dest)) t.start() + +if __name__ == '__main__': + from pathlib import Path + download_all_photos(Path('test/')) diff --git a/server/main.py b/server/main.py index cc77a02..b9dae17 100644 --- a/server/main.py +++ b/server/main.py @@ -1,16 +1,19 @@ import os import json +import time from pathlib import Path -from flask import Flask, request +from flask import Flask, request, abort, send_from_directory from flask_cors import CORS +import power, capture, download + build_folder = Path('../client/build') output_folder = Path('./output') app = Flask(__name__, static_folder=str(build_folder), static_url_path='') CORS(app) @app.route('/api/clients', methods=['POST']) -def clients(): +def clients_post(): content = request.json print('Recieved:', content) @@ -32,8 +35,89 @@ def clients(): return {'client_id': client_id} +@app.route('/api/clients/', methods=['GET']) +def clients_get(cid): + folder = cid + path = output_folder / cid + + if not path.exists(): + abort(404) + + info_file = path / 'info.json' + info_text = info_file.read_text() + res = json.loads(info_text) + photo_glob = path.glob('*.jpg') + res['has_photos'] = bool(list(photo_glob)) + + return res + +@app.route('/api/clients//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 = {} + res['photos'] = [x.name for x in photo_glob] + + return res + +@app.route('/api/clients//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() + + return '' + +@app.route('/api/clients//session', methods=['POST']) +def session_post(cid): + folder = cid + path = output_folder / cid + + if not path.exists(): + abort(404) + + # go through the photo taking process + + # warmup + power.lights_on() + time.sleep(4) + power.lights_off() + time.sleep(0.5) + + # capture + power.lights_on() + time.sleep(0.25) + capture.trigger_capture() + time.sleep(1) + power.grid_on() + time.sleep(0.25) + capture.trigger_capture() + time.sleep(1) + power.grid_off() + power.lights_off() + + download.download_all_photos(path) + + return '' + @app.route('/') def index(): return app.send_static_file('index.html') -app.run() +@app.route('/output/') +def output(filename): + return send_from_directory('output/', filename) + +app.run(host='0.0.0.0')