From 58d318faee62e5d4f16aac4d4ba2779595ebd762 Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Tue, 21 Nov 2023 23:45:29 +0100 Subject: [PATCH] now with notify --- deploy/deploy.playbook.yml | 1 + deploy/templates/eink-show.service.j2 | 7 +++++-- src/main.py | 26 +++++++++++++++++++------- src/notify.py | 26 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 src/notify.py diff --git a/deploy/deploy.playbook.yml b/deploy/deploy.playbook.yml index e6b7dc4..c836cf0 100644 --- a/deploy/deploy.playbook.yml +++ b/deploy/deploy.playbook.yml @@ -10,6 +10,7 @@ service_target_dir: /etc/systemd/system/ # adapt to taste own_image_percent: 90 + recipient: "???" tasks: - name: Pull the latest version of the code diff --git a/deploy/templates/eink-show.service.j2 b/deploy/templates/eink-show.service.j2 index 1790156..8e2ea1c 100644 --- a/deploy/templates/eink-show.service.j2 +++ b/deploy/templates/eink-show.service.j2 @@ -6,8 +6,11 @@ Type=oneshot User={{ ansible_user }} WorkingDirectory={{ code_dest }} ExecStart=python main.py -Environment="OWN_IMAGE_PERCENT={{ own_image_percent }}" +Environment="OWN_IMAGE_PERCENT={{ own_image_percent }}"# +Environment="RECIPIENT={{ recipient }}" + [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target +# this ensures that the service is started after the network is up (@reboot) \ No newline at end of file diff --git a/src/main.py b/src/main.py index ee9de0c..4031a76 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,9 @@ import sys from image_convert import ImageShrink from get.combined import ImageGetCombined +from get.template import ImageGetException from image_show import ImageShow +import notify if len(sys.argv) == 2 and sys.argv[1] == "test": print("Running test") @@ -20,11 +22,21 @@ if "reduce" in sys.argv: print("Enabling color reduction") shrink_kwargs["reduce_colors"] = True +try: + getter = ImageGetCombined() + converter = ImageShrink(**shrink_kwargs) + shower = ImageShow() +except Exception as e: + print("Failed to initialize") + notify.notify_status("Failed to initialize") + raise e -getter = ImageGetCombined() -converter = ImageShrink(**shrink_kwargs) -shower = ImageShow() - -image = getter.get_random_image() -image = converter.convert(image) -shower.show_image(image) +try: + image = getter.get_random_image() + image = converter.convert(image) + shower.show_image(image) + notify.notify_status("Success") +except ImageGetException as e: + print("Failed to get image") + notify.notify_status(f"Failed to get image: {e}") + raise e \ No newline at end of file diff --git a/src/notify.py b/src/notify.py new file mode 100644 index 0000000..ad8c8e9 --- /dev/null +++ b/src/notify.py @@ -0,0 +1,26 @@ +import httpx as h +import os +import socket + +RECIPIENT = os.getenv("RECIPIENT", "DEV") + +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(('8.8.8.8', 80)) + (addr, port) = s.getsockname() + IPS = addr + +def notify_status(status): + data = f"Status: {status}\nIP: {IPS}" + + headers = { + "Title" : f"Eink - {RECIPIENT}", + # "Click": "https://home.nest.com/", + # "Attach": "https://nest.com/view/yAxkasd.jpg", + # "Actions": "http, Open door, https://api.nest.com/open/yAxkasd, clear=true", + # "Email": "phil@example.com" + } + h.post( + "https://ntfy.kluster.moll.re/eink", + data=data, + headers=headers + ) \ No newline at end of file