From 9cb6a8e06668bcdb9d297b3f97e2f0cdd89bea5c Mon Sep 17 00:00:00 2001 From: adriankeenan Date: Mon, 6 Jan 2025 22:40:08 +0000 Subject: [PATCH] init --- .gitignore | 3 +++ readme.md | 15 +++++++++++++-- src/epd_mock.py | 0 src/epd_utils.py | 9 ++++++--- src/server.py | 19 ++++++++++++++++--- 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src/epd_mock.py diff --git a/.gitignore b/.gitignore index dd11d42..070dfe4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .idea **.pyc + +# image storage when running locally +src/img.png diff --git a/readme.md b/readme.md index cb79b1b..b957990 100644 --- a/readme.md +++ b/readme.md @@ -19,9 +19,9 @@ add the Waveshare SDK to your path (see [Dockerfile](./Dockerfile)). on a webserver with more than a single worker, should you choose to use a webserver other than the Flask development webserver. -## How to use +## Setup -### Install +### Install on a Raspberry Pi + Display Build and run the container: @@ -35,6 +35,17 @@ Now you're ready to send images to the display! `--restart=always` will ensure that the container is restarted on crash and on system boot. +### Running locally + +You can run also run the API locally using the `EPD_MOCK=true` env var to mock all calls to the display itself. +You can still check that the correct image is produced by looking at `src/img.png`. + +```commandline +EPD_MOCK=true python3 src/server.py +``` + +## API + ### Setting the image `POST http://rpi:5000` diff --git a/src/epd_mock.py b/src/epd_mock.py new file mode 100644 index 0000000..e69de29 diff --git a/src/epd_utils.py b/src/epd_utils.py index 1f4e04b..cccf5ea 100644 --- a/src/epd_utils.py +++ b/src/epd_utils.py @@ -6,9 +6,12 @@ from models import Mode -path.append('lib') -# noinspection PyUnresolvedReferences -from waveshare_epd import epd4in26 +try: + path.append('lib') + # noinspection PyUnresolvedReferences + from waveshare_epd import epd4in26 +except Exception as e: + logging.warning('could not import wavesare lib') def get_epd(): diff --git a/src/server.py b/src/server.py index 5d177ba..170e8ee 100644 --- a/src/server.py +++ b/src/server.py @@ -1,5 +1,7 @@ import json +import os from pathlib import Path +from unittest.mock import Mock from flask import Flask, request, jsonify, send_file, Response @@ -13,7 +15,6 @@ from img_utils import resize_img, image_changed from epd_utils import handle_epd_error, display_clear, display_img, get_epd - IMG_PATH = 'img.png' DISPLAY_RESOLUTION = Resolution(800, 480) @@ -21,7 +22,16 @@ register_heif_opener() -epd = get_epd() + +def str_is_true(value) -> bool: + return value in [True, 'true', '1'] + + +def mock_epd() -> bool: + return str_is_true(os.getenv('EPD_MOCK')) + + +epd = get_epd() if mock_epd() is False else Mock() app = Flask(__name__) @@ -70,7 +80,7 @@ def show_image() -> tuple[Response, int]: except ValueError: return jsonify(message=f'"mode" invalid, must be one of {", ".join([x for x in Mode])}'), 422 - dither = request.form.get('dither', 'true') in ['true', '1'] + dither = str_is_true(request.form.get('dither', True)) loc = locals() image_settings = {i: loc[i] for i in ('mode', 'dither', 'rotate', 'resize', 'background')} @@ -104,3 +114,6 @@ def clear_image(): return jsonify(message='Success'), 200 except Exception as e: return handle_epd_error(e) + +if __name__ == "__main__": + app.run(port=5000) \ No newline at end of file