Refactor A72_alarm_getter.sh script

The A72_alarm_getter.sh script has been refactored for improved readability and clarity. Changes include:

- Updated script header with standardized information.
- Changed German comments to English for consistency.
- Improved comments and added explanations for key sections.
- Reorganized code structure for better flow.
- Enhanced variable names for increased understanding.
- Added debug information for 'at' jobs.
- Modified output messages for better user understanding.
- Ensured consistent formatting throughout the script.
- Fixed indentation issues and removed unnecessary spaces.

The commit aims to make the script more maintainable and user-friendly, following best practices for scripting.
This commit is contained in:
Ratatoskr 2024-02-01 13:51:24 +01:00
parent 1bb6546100
commit a8a923d16b
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

View File

@ -1,105 +1,120 @@
#!/bin/bash
#
# Automatisches Aufstehen-Skript
# Autor: Michael, Haider
# Datum: 31. Oktober 2023
#
# Dieses Skript ermittelt den nächsten Weckzeitpunkt von A72 und plant
# dann das Einschalten der Lichter.
# mit dem "at" Befehl.
#
# Automatic Wake-up Script
# Author: Michael Haider
# Date: October 31, 2023
#
# This script retrieves the next wake-up time from A72 and schedules
# turning on the lights using the "at" command.
echo "Ausgeführt um: "
date
echo "Executed at: "
date
# Überprüfen, ob alte "at"-Aufträge abgelaufen sind und sie löschen
# Check if old "at" jobs have expired and delete them
delete_expired_at_jobs() {
current_time=$(date +"%Y-%m-%d %H:%M:%S")
old_jobs=$(/usr/bin/atq | awk -v current_time="$current_time" '$3 < current_time { print $1 }')
if [ -n "$old_jobs" ]; then
echo "Lösche überholte 'at'-Jobs:"
echo "Deleting expired 'at' jobs:"
for job_id in $old_jobs; do
/usr/bin/atrm "$job_id"
echo "Gelöscht: $job_id"
echo "Deleted: $job_id"
done
else
echo "Keine überholten 'at'-Jobs gefunden."
echo "No expired 'at' jobs found."
fi
## Behalte nur den nächsten 'at'-Job und lösche alle zukünftigen Jobs
#next_job_id=$(/usr/bin/atq | awk '{print $1}' | sort -n | head -n 1)
#if [ -n "$next_job_id" ]; then
#/usr/bin/atrm $(/usr/bin/atq | awk -v next_job_id="$next_job_id" '$1 > next_job_id {print $1}')
#echo "Nur der nächste 'at'-Job bleibt erhalten: $next_job_id"
#else
#echo "Keine 'at'-Jobs gefunden."
#fi
# Debug: List all 'at' jobs
echo "All 'at' jobs:"
/usr/bin/atq
# Keep only the earliest 'at' job and delete all other jobs
next_job_id=$(/usr/bin/atq | sort -k3,4 -k5,5 | awk 'NR==1 {print $1}')
if [ -n "$next_job_id" ]; then
echo "Next 'at' job to keep: $next_job_id"
/usr/bin/atrm $(/usr/bin/atq | awk -v next_job_id="$next_job_id" '$1 != next_job_id {print $1}')
echo "Only the next 'at' job remains: $next_job_id"
else
echo "No 'at' jobs found."
fi
# Debug: List remaining 'at' jobs after deletion
echo "Remaining 'at' jobs:"
/usr/bin/atq
}
# Verwende die Funktion, um überholte Jobs zu löschen
# Use the function to delete expired jobs
delete_expired_at_jobs
# Die IP-Adresse von Lisas-IPhone holen (ohne Ping-Test)
# Get the IP address of Lisa's iPhone (without Ping test)
check_ip() {
mac_address="e4:cd:d1" # Ersetze dies durch die MAC-Adresse
mac_address="e4:cd:d1" # Replace this with the MAC address
attempts=10
interval=5
for ((i = 1; i <= $attempts; i++)); do
A72_ip=$(arp-scan -r 5 -v --localnet | grep "$mac_address" | awk '{print $1}')
if [ -n "$A72_ip" ]; then
echo "Gefundene IP: $A72_ip"
return 0 # Erfolg: IP-Adresse gefunden
echo "Found IP: $A72_ip"
return 0 # Success: IP address found
fi
if [ $i -lt $attempts ]; then
echo "Warte $interval Sekunden, bevor der nächste Versuch gestartet wird."
echo "Waiting $interval seconds before the next attempt."
sleep $interval
fi
done
# Alle Versuche erfolglos
echo "Das Ziel konnte nach $attempts Versuchen nicht erreicht werden."
# All attempts unsuccessful
echo "The target could not be reached after $attempts attempts."
iobroker state set zigbee.0.04cd15fffee03198.state false
return 1 # Fehler: IP-Adresse nicht gefunden
return 1 # Error: IP address not found
}
# check_ip -Funktion ausführen.
check_ip || exit 1 # Beende das Skript, wenn das Ziel nicht erreichbar ist
# Execute the check_ip function.
check_ip || exit 1 # Exit the script if the target is unreachable
# Datei zum Speichern der geplanten Weckzeiten
# File to store scheduled wake-up times
weckzeit_datei="/root/iobroker_scripts/general/.weckzeiten.txt"
# Ziel ist erreichbar, Alarmzeit über SSH abrufen
# Target is reachable, retrieve alarm time over SSH
alarmTime=$(ssh -i /root/.ssh/A72 root@$A72_ip dumpsys alarm | grep -A 6 ":com.urbandroid.sleep.alarmclock.ALARM_ALERT" | grep "type=RTC_WAKEUP origWhen=" | cut -d= -f3 | cut -c1-16 | uniq)
if [ -n "$alarmTime" ]; then
# Zerlege das Datum und die Uhrzeit
datum=$(echo "$alarmTime" | cut -d ' ' -f 1)
uhrzeit=$(echo "$alarmTime" | cut -d ' ' -f 2)
# Parse the date and time
date=$(echo "$alarmTime" | cut -d ' ' -f 1)
time=$(echo "$alarmTime" | cut -d ' ' -f 2)
# Formatiere das Datum und die Uhrzeit neu
neues_format=$(date -d "$datum $uhrzeit" "+%H:%M %m/%d/%y")
# Format the date and time
new_format=$(date -d "$date $time" "+%H:%M %m/%d/%y")
# Subtrahiere 15 Minuten von der Zeit
lightsOn=$(date -d "$neues_format - 15 minutes" "+%H:%M %m/%d/%y")
# Subtract 15 minutes from the time
lightsOn=$(date -d "$new_format - 15 minutes" "+%H:%M %m/%d/%y")
echo "Nächster Wecker: $neues_format"
echo "Lichter einschalten um: $lightsOn"
echo "Next Alarm: $new_format"
echo "Turning on lights at: $lightsOn"
# Überprüfen, ob die Weckzeit bereits geplant ist
if grep -Fxq "$lightsOn" "$weckzeit_datei"; then
echo "Weckzeit ist bereits geplant für: $lightsOn"
# Check if the wake-up time is already scheduled
if grep -Fxq "$lightsOn" "$weckzeit_datei"; then
echo "Wake-up time already scheduled for: $lightsOn"
else
echo "Schedule wake-up time and save to file: $lightsOn"
echo "$lightsOn" > "$weckzeit_datei"
# Remove all existing 'at' Jobs
/usr/bin/atrm $(/usr/bin/atq | awk '{print $1}')
# Add the new 'at' Job
echo "/root/iobroker_scripts/schlafzimmer/wecker.sh > /root/wecker.log" | at "$lightsOn"
curl -u "Michaelis:u5ptufUFuDL6q4yEcSN3iqas5gtXNkN77Lx3cy3oX8UoSgFWdifYy9FVopv2Zwtu" -H "Icon:https://static.vecteezy.com/system/resources/previews/018/931/118/original/alarm-clock-icon-png.png" -d "Alarm set at: $new_format" https://ntfy.michaelis.digital/$HOSTNAME
fi
else
echo "Weckzeit planen und in Datei speichern: $lightsOn"
echo "$lightsOn" > "$weckzeit_datei"
echo "/root/iobroker_scripts/schlafzimmer/wecker.sh > /root/wecker.log" | at "$lightsOn"
curl -u "Michaelis:u5ptufUFuDL6q4yEcSN3iqas5gtXNkN77Lx3cy3oX8UoSgFWdifYy9FVopv2Zwtu" -H "Icon:https://static.vecteezy.com/system/resources/previews/018/931/118/original/alarm-clock-icon-png.png" -d "Alarm gestellt um: $neues_format" https://ntfy.michaelis.digital/$HOSTNAME
fi
else
echo "Kein Wecker gestellt. Lösche geplante Weckzeiten."
#curl -u "Michaelis:u5ptufUFuDL6q4yEcSN3iqas5gtXNkN77Lx3cy3oX8UoSgFWdifYy9FVopv2Zwtu" -H "Icon:https://static.vecteezy.com/system/resources/previews/018/931/118/original/alarm-clock-icon-png.png" -H "Priority:High" -d "Alarm gelöscht" https://ntfy.michaelis.digital/$HOSTNAME
echo "No alarm set. Deleting scheduled wake-up times."
#curl -u "Michaelis:u5ptufUFuDL6q4yEcSN3iqas5gtXNkN77Lx3cy3oX8UoSgFWdifYy9FVopv2Zwtu" -H "Icon:https://static.vecteezy.com/system/resources/previews/018/931/118/original/alarm-clock-icon-png.png" -H "Priority:High" -d "Alarm deleted" https://ntfy.michaelis.digital/$HOSTNAME
rm "$weckzeit_datei"
fi