From 7b1ecb13fd2103cc659d88a1110f5bf27d6c861f Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Thu, 4 Jan 2024 21:54:53 +0100
Subject: [PATCH] Add Script to run the Kiosk on hobbes

---
 templates/hobbes/kiosk.sh.j2 | 48 ++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100755 templates/hobbes/kiosk.sh.j2

diff --git a/templates/hobbes/kiosk.sh.j2 b/templates/hobbes/kiosk.sh.j2
new file mode 100755
index 0000000..e9c7944
--- /dev/null
+++ b/templates/hobbes/kiosk.sh.j2
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+# Check if the script is run by root
+if [ "$EUID" -ne 0 ]
+  then echo "Please run as root"
+  exit
+fi
+
+# MQTT broker details
+BROKER="{{ kiosk_mqtt_host }}"
+TOPIC="{{ kiosk_mqtt_topic }}"
+
+# Variable to store the PID of the fbi process
+fbi_pid=0
+
+# Function to be executed on SIGTERM
+on_sigterm() {
+    echo "SIGTERM received, exiting..."
+    # Kill the fbi process with the stored PID
+    if [ $fbi_pid -ne 0 ]; then
+        sudo kill $fbi_pid
+    fi
+    # Remove the temporary file
+    rm -f /tmp/grafana.png
+    exit 0
+}
+
+# Trap SIGTERM and call on_sigterm() when it is received
+trap 'on_sigterm' SIGTERM
+
+while true
+do
+    # Subscribe to the topic and save received data to a file
+    mosquitto_sub -h $BROKER -t $TOPIC -C 1 > /tmp/grafana.png
+
+    # Kill the previous fbi process
+    if [ $fbi_pid -ne 0 ]; then
+        sudo kill $fbi_pid
+    fi
+
+    # Display the image using fbi
+    fbi -T 1 -noverbose -a /tmp/grafana.png &
+    fbi_pid=$!
+
+    # Wait to avoid a race condition between
+    # fbi starting and mosquitto truncating the file
+    sleep 1
+done