diff --git a/.DS_Store b/.DS_Store index e5ad8bf..a13b8d2 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/bitmap_service/app.py b/bitmap_service/app.py index 35b5091..75f7b2a 100644 --- a/bitmap_service/app.py +++ b/bitmap_service/app.py @@ -33,9 +33,9 @@ def bitmap(): payload = ''.join([chr(i) for i in bitmap_data]) print(payload) - req = requests.post('http://10.141.142.75/api/tag', data=payload) - - return jsonify({'code': req.status_code}) + # req = requests.post('http://10.141.142.75/api/tag', data=payload) + return jsonify({'payload': payload}) + # return jsonify({'code': req.status_code}) except Exception as e: return jsonify({"error": e}), 500 diff --git a/docker-compose.yml b/docker-compose.yml index c185784..3a3682f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,13 @@ services: container_name: backend_service ports: - "12001:80" + # bitmap_service: + # build: ./bitmap_service + # container_name: bitmap_service + # ports: + # - "12002:80" bitmap_service: - build: ./bitmap_service - container_name: bitmap_service + build: ./trash + container_name: trash ports: - "12002:80" \ No newline at end of file diff --git a/trash/app.py b/trash/app.py new file mode 100644 index 0000000..5391410 --- /dev/null +++ b/trash/app.py @@ -0,0 +1,55 @@ +from flask import Flask, request, jsonify +from flask_cors import CORS +from PIL import Image +import io +from tcp import send_request + +app = Flask(__name__) +CORS(app) + +@app.route('/') +def root(): + return 'ok' + +@app.route('/api/bitmap', methods=['POST']) +def bitmap(): + + if 'file' not in request.files: + return jsonify({"error": "no file"}), 400 + + file = request.files['file'] + + if file.filename == '': + return jsonify({"error": "no file"}), 400 + + if not file.filename.lower().endswith('.png'): + return jsonify({"error": "invalid file format"}), 400 + + try: + image_stream = io.BytesIO(file.read()) + img = Image.open(image_stream) + grayscale_img = img.convert('L') + bitmap_data = list(grayscale_img.getdata()) + + payload = ''.join([chr(i) for i in bitmap_data]) + print(payload) + + chunk_size = 2048 + for i in range(0, len(payload), chunk_size): + data = { + "command": "update_image", + "index": i, + "data": bytes(payload[i:i+chunk_size]) + } + send_request(data) + send_request({"command":"push_image"}) + + # req = requests.post('http://10.141.142.75/api/tag', data=payload) + return jsonify({'payload': payload}) + # return jsonify({'code': req.status_code}) + + except Exception as e: + return jsonify({"error": e}), 500 + +if __name__ == '__main__': + app.run('0.0.0.0', port=80) diff --git a/trash/dockerfile b/trash/dockerfile new file mode 100644 index 0000000..41af328 --- /dev/null +++ b/trash/dockerfile @@ -0,0 +1,9 @@ +FROM python:3.9 + +WORKDIR /app + +COPY . . + +RUN pip3 install pillow flask flask-cors requests + +CMD ["python3", "app.py"] \ No newline at end of file diff --git a/trash/tcp.py b/trash/tcp.py new file mode 100644 index 0000000..120bc43 --- /dev/null +++ b/trash/tcp.py @@ -0,0 +1,28 @@ +from typing import Dict, Any +import socket + +import msgpack + +SERVER_ADDRESS = ('172.16.0.249', 8888) + +def send_request(data:Dict[str, Any]): + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect(SERVER_ADDRESS) + + try: + message:bytes = msgpack.packb(data) + + client_socket.sendall(message) + + rx = client_socket.recv(1024) + if data: + print(f"Received message: {rx.decode('utf-8').strip()}") + else: + print("Connection closed") + return + except ConnectionResetError: + print("Connection reset") + finally: + client_socket.close() + print("Connection closed") +