浏览代码

updated media_change.sh

Karl Hudgell 6 月之前
父节点
当前提交
fca38b5819
共有 1 个文件被更改,包括 18 次插入17 次删除
  1. 18 17
      media_change.sh

+ 18 - 17
media_change.sh

@@ -1,26 +1,27 @@
 #!/bin/bash
-exec >> /home/dietpi/mount.log 2>&1
-export LC_ALL=en_GB.utf-8
-export LANG=en_GB.utf-8
 
-echo "$(date) Start."
-echo "$(date) Media change detected on device $1"
-device=${1##*/}
+DEVICE_NAME=$1  # Get the device name passed from udev
 
-lsblk | grep $device
+LOGFILE="/home/dietpi/media_change.log"
 
-if [ $? -eq 0 ]; then
-    echo "$(date) Device exists on machine."
-    echo "$(date) Mounting device $1 to /mnt/floppy."
+# Check if the device exists in the fdisk -l output and log the result
+if sudo fdisk -l | grep -q "$DEVICE_NAME"; then
+    echo "$(date) Floppy disk is present in device $DEVICE_NAME" >> $LOGFILE
+    echo "$(date) Mounting device $1 to /mnt/floppy." >> $LOGFILE
     /usr/bin/systemd-mount --umount /mnt/floppy
-    echo "$(date) Floppy unmounted"
+    echo "$(date) Floppy unmounted"  >> $LOGFILE
     /usr/bin/systemd-mount $1 /mnt/floppy
-    echo "$(date) Floppy mounted"
+    echo "$(date) Floppy mounted"  >> $LOGFILE
+
+    # Read content from diskplayer.contents
     var=$(cat /mnt/floppy/diskplayer.contents)
-    echo python3 /home/pi/pythonDiskPlayer/player.py \"$var\"
-    runuser -l dietpi -c "python3 /home/dietpi/floppy-ytube-player/main.py \"$var\""
+    echo "$(date) Running Python script with argument: '$var'"  >> $LOGFILE
+    # Run the Python script and capture both stdout and stderr to the log
+    /usr/bin/python3 /home/dietpi/floppy-ytube-player/main.py "$var" >> $LOGFILE 2>&1
+    /usr/bin/python3 /home/dietpi/floppy-ytube-player/main.py PLAY >> $LOGFILE 2>&1
 else
-    echo "$(date) Device does not exist on machine."
-    runuser -l dietpi -c "python3 /home/dietpi/floppy-ytube-player/main.py stop"
+    echo "$(date): No floppy disk is present in device $DEVICE_NAME" >> $LOGFILE
+    /usr/bin/systemd-mount --umount /mnt/floppy
+    echo "$(date) Floppy unmounted"  >> $LOGFILE
+    /usr/bin/python3 /home/dietpi/floppy-ytube-player/main.py EJECT >> $LOGFILE 2>&1
 fi
-echo "$(date) End."