Enhance morning-alarm.sh script:

- Clear morning-alarm.log before logging execution timestamp.
- Set a flag 'alarm_dismissed' for loop control.
- Modify the check_notification function to continuously check the alarm state and sleep for better efficiency.
- Start play.sh script in the background.
- Replace 'killall' with '/system/bin/pkill' for better compatibility.
- Introduce sleep intervals for smoother execution.
- Adjust the volume increment loop and add sleep before checking the notification.
- Properly exit the script when the alarm is dismissed, killing play.sh and mpv.
- Overall improvements for better readability and functionality in the morning-alarm.sh script.
This commit is contained in:
Ratatoskr 2024-02-06 17:40:40 +01:00
parent 41f3b3a4d2
commit a5076ae046
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

View File

@ -6,7 +6,12 @@
# It turns off Bluetooth, increases the volume, and checks if the alarm has been dismissed. # It turns off Bluetooth, increases the volume, and checks if the alarm has been dismissed.
# After the alarm is dismissed, it plays the alarm sound. # After the alarm is dismissed, it plays the alarm sound.
echo "Script executed at: $(date)" > /data/scripts/morning-alarm.log # clear logs
rm -rf /data/scripts/morning-alarm.log
echo "Script executed at: $(date)"
# Set a flag for the loop
alarm_dismissed=false
# Function to turn off Bluetooth # Function to turn off Bluetooth
turn_off_bluetooth() { turn_off_bluetooth() {
@ -23,18 +28,18 @@ increase_volume() {
# Function to check the alarm notification # Function to check the alarm notification
check_notification() { check_notification() {
# Run the dumpsys command within a timeout # Run the dumpsys command within a timeout
output=$(timeout 5s dumpsys activity processes | grep "com.urbandroid.sleep/") output=$(dumpsys activity processes | grep "com.urbandroid.sleep")
# Check if the result is not empty and contains the alarm # Check if the result is not empty and contains the alarm
if [ -n "$output" ] && echo "$output" | grep -q "AlarmKlaxon"; then if [ -n "$output" ] && echo "$output" | grep -q "AlarmKlaxon"; then
date && echo "Alarm not dismissed yet." date && echo "Alarm not dismissed yet."
sleep 5
return 1
else else
date && echo "Alarm dismissed." date && echo "Alarm dismissed."
killall mpv alarm_dismissed=true
break /system/bin/pkill play.sh
#killall /data/data/com.termux/files/usr/bin/mpv /system/bin/pkill mpv
echo "$output"
exit
fi fi
} }
@ -43,16 +48,20 @@ check_notification() {
# Turn off Bluetooth # Turn off Bluetooth
turn_off_bluetooth turn_off_bluetooth
# Increase volume and check notification # start play.sh script
/data/scripts/play.sh &
# Repeat the check_notification function
while true; do while true; do
increase_volume increase_volume
sleep 2
check_notification check_notification
done &
## Play alarm sound # Check if the alarm dismissed flag is set
for ((volume = 20; volume <= 100; volume += 10)); do if [ "$alarm_dismissed" = true ]; then
echo "Actual volume: $volume" /system/bin/pkill play.sh
/data/data/com.termux/files/usr/bin/mpv --replaygain=track --volume="$volume" /data/scripts/your-new-morning-alarm.ogg /system/bin/pkill mpv
#check_notification & exit 0 # Exit the script here
fi
done done