From fc8c2d292dc8ae8bf0f1117b41c0051b4f303889 Mon Sep 17 00:00:00 2001 From: waltem01 Date: Thu, 14 Mar 2024 14:30:56 +0100 Subject: [PATCH] create instruction list endpoint --- API/main.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/API/main.py b/API/main.py index a7d5798..cf791fe 100755 --- a/API/main.py +++ b/API/main.py @@ -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():