From 705afb498baee8fdb561ebf7d99b8b737056056e Mon Sep 17 00:00:00 2001
From: Robert Piotrowski <robert.piotrowski1@pg.edu.pl>
Date: Sat, 30 Nov 2024 21:32:00 +0100
Subject: [PATCH] Scrit tocheck tmux session and autostart

---
 .gitignore                                  |  2 ++
 serial_to_mqtt_bridge/check_tmux_session.sh | 37 +++++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 .gitignore
 create mode 100755 serial_to_mqtt_bridge/check_tmux_session.sh

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7818b8f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+backup/
+*.log
diff --git a/serial_to_mqtt_bridge/check_tmux_session.sh b/serial_to_mqtt_bridge/check_tmux_session.sh
new file mode 100755
index 0000000..7fa5968
--- /dev/null
+++ b/serial_to_mqtt_bridge/check_tmux_session.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+#add to crontab -e
+# * * * * *  /home/pi/Sustronics/suspilot3_3/serial_to_mqtt_bridge/check_tmux_session.sh Sus_ACM0 >> /home/pi/Sustronics/suspilot3_3/serial_to_mqtt_bridge/check_tmux_session.log 2>&1
+
+# Function to print messages with timestamp
+log_message() {
+    echo "$(date '+%Y-%m-%d %H:%M:%S') $1"
+}
+
+# Check if a session name is provided
+if [ -z "$1" ]; then
+    log_message "Usage: $0 <session_name>"
+    exit 1
+fi
+
+# Name of the tmux session from the first argument
+SESSION_NAME="$1"
+
+# Check if the session exists
+if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
+    log_message "The tmux session '$SESSION_NAME' is running."
+else
+    log_message "The tmux session '$SESSION_NAME' is not running."
+
+    # Check if /dev/ttyACM0 exists
+    if [ -e /dev/ttyACM0 ]; then
+        log_message "The device '/dev/ttyACM0' exists."
+
+        # Set environment variables and run the script
+        DEVNAME='/dev/ttyACM0' ID_SERIAL_SHORT='ACM0' \
+        /home/pi/Sustronics/suspilot3_3/serial_to_mqtt_bridge/udev_trigger.sh
+        log_message "Script executed with DEVNAME='/dev/ttyACM0' and ID_SERIAL_SHORT='ACM0'."
+    else
+        log_message "The device '/dev/ttyACM0' does not exist."
+    fi
+fi
+
-- 
GitLab