create instruction list endpoint

This commit is contained in:
waltem01 2024-03-14 14:30:56 +01:00
parent 84fb7963d8
commit fc8c2d292d

View File

@ -11,7 +11,7 @@ from flask_cors import CORS, cross_origin
from dotenv import load_dotenv
from waitress import serve
from pathlib import Path
import os, time, threading
import os, time, threading, json
env_path = Path('..') / '.env'
@ -20,6 +20,69 @@ load_dotenv(dotenv_path=env_path)
api = Flask(__name__)
cors = CORS(api)
@api.route('/instructions', methods=['POST'])
@cross_origin()
def instructions():
# prepare response data
response = { 'success': True }
try:
global matrix
assert matrix is not None
# receive client data
data = request.form
# try unpacking instruction list
instructions = data.get('instructions')
# parse json data from instructions string
ins_set = json.loads(instructions)
# call respective matrix methods with corresponding data
for ins in ins_set:
if ins['endpoint'] == 'upload':
url = ins['url']
matrix.set_image(url)
elif ins['endpoint'] == 'image':
x = int(ins['x'])
y = int(ins['y'])
matrix.display_image(x, y)
elif ins['endpoint'] == 'text':
x = int(ins['x'])
y = int(ins['y'])
matrix.text(x, y, ins['text'])
elif ins['endpoint'] == 'pixel':
x = int(ins['x'])
y = int(ins['y'])
matrix.pixel(x, y)
elif ins['endpoint'] == 'circle':
x = int(ins['x'])
y = int(ins['y'])
r = int(ins['r'])
matrix.circle(x, y, r)
elif ins['endpoint'] == 'rectangle':
x = int(ins['x'])
y = int(ins['y'])
w = int(ins['w'])
h = int(ins['h'])
matrix.rectangle(x, y, w, h)
elif ins['endpoint'] == 'line':
x1 = int(ins['x1'])
y1 = int(ins['y1'])
x2 = int(ins['x2'])
y2 = int(ins['y2'])
matrix.line(x1, y1, x2, y2)
elif ins['endpoint'] == 'clear':
matrix.clear()
elif ins['endpoint'] == 'update':
matrix.update()
except Exception as e:
# error handling
print(e)
response['success'] = False
# respond to client
return jsonify(response)
@api.route('/upload', methods=['POST'])
@cross_origin()
def set_image():