feat: server state update API

This commit is contained in:
Tony Yang 2023-01-09 21:05:19 +08:00
parent 2b89a4d1fc
commit 458b8805ff
Signed by: t510599
GPG Key ID: D88388851C28715D

View File

@ -1,8 +1,60 @@
from flask import Flask, render_template, jsonify, send_file import os
from flask import Blueprint, Flask, render_template, jsonify, send_file, request
default_image_path = "./static/photo.jpg"
image_path = ""
state = "put"
trash_type = ""
api = Blueprint('api', __name__)
# 放下垃圾
@api.route("/putdown")
def putdown():
global state
state = "camera"
return "ok"
# 拍好照
@api.route("/pic")
def pic():
global state, image_path
path = request.args.get("path")
if not path or not os.path.isfile(path):
return "sad", 400
image_path = path
state = "identify"
return "ok"
# 辨識好
@api.route("/result")
def result():
global state, trash_type
trash = request.args.get("type")
if not trash:
return "sad", 400
trash_type = trash
state = "identified"
return "ok"
# 準備好下一個
@api.route("/ready")
def ready():
global state
state = "put"
return "ok"
app = Flask(__name__) app = Flask(__name__)
app.register_blueprint(api, url_prefix="/api")
imagePath = "./static/photo.jpg"
@app.route("/") @app.route("/")
def index(): def index():
@ -11,19 +63,20 @@ def index():
@app.route("/poll") @app.route("/poll")
def poll(): def poll():
data = { data = {
"state": "camera" "state": state
} }
if state == "identified":
data |= {"type": trash_type}
return jsonify(data) return jsonify(data)
@app.route("/photo") @app.route("/photo")
def photo(): def photo():
return send_file(imagePath) if not image_path:
return send_file(default_image_path)
# 放下垃圾 return send_file(image_path)
# 拍好照
# 辨識好
# 準備好下一個
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True)