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.
# 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
turn_off_bluetooth() {
@ -23,18 +28,18 @@ increase_volume() {
# Function to check the alarm notification
check_notification() {
# 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
if [ -n "$output" ] && echo "$output" | grep -q "AlarmKlaxon"; then
date && echo "Alarm not dismissed yet."
sleep 5
return 1
else
date && echo "Alarm dismissed."
killall mpv
break
#killall /data/data/com.termux/files/usr/bin/mpv
echo "$output"
exit
alarm_dismissed=true
/system/bin/pkill play.sh
/system/bin/pkill mpv
fi
}
@ -43,16 +48,20 @@ check_notification() {
# 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
increase_volume
sleep 2
check_notification
done &
## Play alarm sound
for ((volume = 20; volume <= 100; volume += 10)); do
echo "Actual volume: $volume"
/data/data/com.termux/files/usr/bin/mpv --replaygain=track --volume="$volume" /data/scripts/your-new-morning-alarm.ogg
#check_notification &
# Check if the alarm dismissed flag is set
if [ "$alarm_dismissed" = true ]; then
/system/bin/pkill play.sh
/system/bin/pkill mpv
exit 0 # Exit the script here
fi
done