wecker/A72_alarm_getter.sh
Ratatoskr 63720dafea
Add execution timestamp and echo in A72_alarm_getter.sh
In this commit, an echo statement is added to display the execution timestamp
in the A72_alarm_getter.sh script. The timestamp is printed with the command
'echo 'Ausgeführt um: ''.

Additionally, the script now contains a function called delete_expired_at_jobs
to check and delete expired 'at' jobs. The current time is obtained using the
'date' command and stored in the variable current_time.
2023-12-05 18:00:34 +01:00

101 lines
3.7 KiB
Bash
Executable File

#!/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.
echo "Ausgeführt um: $date"
# Überprüfen, ob alte "at"-Aufträge abgelaufen sind und sie löschen
delete_expired_at_jobs() {
current_time=$(date +"%Y-%m-%d %H:%M:%S")
old_jobs=$(atq | awk -v current_time="$current_time" '$3 < current_time { print $1 }')
if [ -n "$old_jobs" ]; then
echo "Lösche überholte 'at'-Jobs:"
for job_id in $old_jobs; do
atrm "$job_id"
echo "Gelöscht: $job_id"
done
else
echo "Keine überholten 'at'-Jobs gefunden."
fi
}
# Verwende die Funktion, um überholte Jobs zu löschen
delete_expired_at_jobs
# Die IP-Adresse von Lisas-IPhone holen.
check_ip() {
mac_address="e4:cd:d1" # Ersetze dies durch die MAC-Adresse
A72_ip=$(arp-scan --localnet | grep "$mac_address" | awk '{print $1}')
echo "Gefundene IP: $A72_ip"
}
# check_ip -Funktion ausführen.
check_ip
# Datei zum Speichern der geplanten Weckzeiten
weckzeit_datei="/root/iobroker_scripts/generel/.weckzeiten.txt"
# Überprüfen, ob das Ziel über Ping erreichbar ist
function is_target_reachable() {
echo "teste ping $A72_ip"
local ping_output=$(ping -q -w 3 -c 3 $A72_ip 2>&1)
local ping_exit_code=$?
echo "$ping_output" # Ausgabe des Ping-Befehls anzeigen
if [ $ping_exit_code -eq 0 ] && [[ "$ping_output" == *"3 received"* ]]; then
echo "Das Ziel ist erreichbar."
return 0 # Erfolg: Das Ziel ist erreichbar
else
echo "Das Ziel ist nicht erreichbar. Exit Code: $ping_exit_code"
return 1 # Fehler: Das Ziel ist nicht erreichbar
exit
fi
}
# Überprüfen, ob das Ziel erreichbar ist
if is_target_reachable; then
# Ziel ist erreichbar, Alarmzeit über SSH abrufen
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)
# Formatiere das Datum und die Uhrzeit neu
neues_format=$(date -d "$datum $uhrzeit" "+%H:%M %m/%d/%y")
# Subtrahiere 15 Minuten von der Zeit
lightsOn=$(date -d "$neues_format - 15 minutes" "+%H:%M %m/%d/%y")
echo "Nächster Wecker: $neues_format"
echo "Lichter einschalten um: $lightsOn"
# Überprüfen, ob die Weckzeit bereits geplant ist
if grep -Fxq "$lightsOn" "$weckzeit_datei"; then
echo "Weckzeit ist bereits geplant für: $lightsOn"
else
echo "Weckzeit planen und in Datei speichern: $lightsOn"
echo "$lightsOn" > "$weckzeit_datei"
echo "/root/iobroker_scripts/schlafzimmer/wecker.sh" | at "$lightsOn"
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 gestellt um: $(ssh -i /root/.ssh/A72 root@$A72_ip dumpsys alarm | grep triggerTime | cut -d= -f2 | cut -c12-16 | uniq)" 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
rm "$weckzeit_datei"
fi
else
echo "Ziel ist nicht erreichbar."
fi