Fix Weckzeit Calculation and Refactoring
- Corrected the calculation of the variable 'lightsOn' to ensure the correct time is set for turning on lights. - Refactored the script by combining the IP retrieval and ping test into a single function 'check_ip'. - Improved error handling and added comments for better readability. Author: Michael, Haider Date: 22.12.2023
This commit is contained in:
parent
11ae8bfaf6
commit
3dd4f72a0e
@ -39,21 +39,19 @@ delete_expired_at_jobs() {
|
|||||||
# Verwende die Funktion, um überholte Jobs zu löschen
|
# Verwende die Funktion, um überholte Jobs zu löschen
|
||||||
delete_expired_at_jobs
|
delete_expired_at_jobs
|
||||||
|
|
||||||
# Die IP-Adresse von Lisas-IPhone holen.
|
# Die IP-Adresse von Lisas-IPhone holen und Ping-Test durchführen
|
||||||
check_ip() {
|
check_ip() {
|
||||||
mac_address="e4:cd:d1" # Ersetze dies durch die MAC-Adresse
|
mac_address="e4:cd:d1" # Ersetze dies durch die MAC-Adresse
|
||||||
A72_ip=$(arp-scan --localnet | grep "$mac_address" | awk '{print $1}')
|
A72_ip=$(arp-scan --localnet | grep "$mac_address" | awk '{print $1}')
|
||||||
|
|
||||||
|
if [ -z "$A72_ip" ]; then
|
||||||
|
echo "IP nicht gefunden. Ziel ist nicht erreichbar."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Gefundene IP: $A72_ip"
|
echo "Gefundene IP: $A72_ip"
|
||||||
}
|
|
||||||
|
|
||||||
# check_ip -Funktion ausführen.
|
# Ping-Test durchfü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"
|
echo "teste ping $A72_ip"
|
||||||
local ping_output=$(/bin/ping -q -w 3 -c 3 $A72_ip 2>&1)
|
local ping_output=$(/bin/ping -q -w 3 -c 3 $A72_ip 2>&1)
|
||||||
local ping_exit_code=$?
|
local ping_exit_code=$?
|
||||||
@ -66,45 +64,44 @@ function is_target_reachable() {
|
|||||||
else
|
else
|
||||||
echo "Das Ziel ist nicht erreichbar. Exit Code: $ping_exit_code"
|
echo "Das Ziel ist nicht erreichbar. Exit Code: $ping_exit_code"
|
||||||
return 1 # Fehler: Das Ziel ist nicht erreichbar
|
return 1 # Fehler: Das Ziel ist nicht erreichbar
|
||||||
exit
|
fi
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Überprüfen, ob das Ziel erreichbar ist
|
# check_ip -Funktion ausführen.
|
||||||
if is_target_reachable; then
|
check_ip || exit 1 # Beende das Skript, wenn das Ziel nicht erreichbar ist
|
||||||
# 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
|
# Datei zum Speichern der geplanten Weckzeiten
|
||||||
# Zerlege das Datum und die Uhrzeit
|
weckzeit_datei="/root/iobroker_scripts/generel/.weckzeiten.txt"
|
||||||
datum=$(echo "$alarmTime" | cut -d ' ' -f 1)
|
|
||||||
uhrzeit=$(echo "$alarmTime" | cut -d ' ' -f 2)
|
|
||||||
|
|
||||||
# Formatiere das Datum und die Uhrzeit neu
|
# Ziel ist erreichbar, Alarmzeit über SSH abrufen
|
||||||
neues_format=$(date -d "$datum $uhrzeit" "+%H:%M %m/%d/%y")
|
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)
|
||||||
|
|
||||||
# Subtrahiere 15 Minuten von der Zeit
|
if [ -n "$alarmTime" ]; then
|
||||||
lightsOn=$(date -d "$neues_format - 15 minutes" "+%H:%M %m/%d/%y")
|
# Zerlege das Datum und die Uhrzeit
|
||||||
|
datum=$(echo "$alarmTime" | cut -d ' ' -f 1)
|
||||||
|
uhrzeit=$(echo "$alarmTime" | cut -d ' ' -f 2)
|
||||||
|
|
||||||
echo "Nächster Wecker: $neues_format"
|
# Formatiere das Datum und die Uhrzeit neu
|
||||||
echo "Lichter einschalten um: $lightsOn"
|
neues_format=$(date -d "$datum $uhrzeit" "+%H:%M %m/%d/%y")
|
||||||
|
|
||||||
# Überprüfen, ob die Weckzeit bereits geplant ist
|
# Subtrahiere 15 Minuten von der Zeit
|
||||||
if grep -Fxq "$lightsOn" "$weckzeit_datei"; then
|
lightsOn=$(date -d "$neues_format - 15 minutes" "+%H:%M %m/%d/%y")
|
||||||
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 > /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" -H "Priority:High" -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
|
|
||||||
|
|
||||||
rm "$weckzeit_datei"
|
echo "Nächster Wecker: $neues_format"
|
||||||
fi
|
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
|
else
|
||||||
echo "Ziel ist nicht erreichbar."
|
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" -H "Priority:High" -d "Alarm gestellt um: $neues_format" https://ntfy.michaelis.digital/$HOSTNAME
|
||||||
fi
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user