Compare commits

...

2 Commits

Author SHA1 Message Date
4761bfea0f
Refactor Wecker Script for Improved Readability and Functionality
- Combined IP retrieval and ping test into a single function 'check_ip'.
- Improved error handling for better script robustness.
- Removed unnecessary commented-out code and added comments for clarity.
- Ensured consistency in indentation and formatting.

Author: Michael, Haider
Date: 22.12.2023
2023-12-22 15:07:05 +01:00
3dd4f72a0e
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
2023-12-22 15:05:00 +01:00
2 changed files with 64 additions and 45 deletions

View File

@ -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,12 +64,15 @@ 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
# Datei zum Speichern der geplanten Weckzeiten
weckzeit_datei="/root/iobroker_scripts/generel/.weckzeiten.txt"
# Ziel ist erreichbar, Alarmzeit über SSH abrufen # 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) 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)
@ -104,7 +105,3 @@ if is_target_reachable; then
rm "$weckzeit_datei" rm "$weckzeit_datei"
fi fi
else
echo "Ziel ist nicht erreichbar."
fi

View File

@ -39,11 +39,32 @@ set_color() {
iobroker state set zigbee.0.04cd15fffee03198.color "$color_value" iobroker state set zigbee.0.04cd15fffee03198.color "$color_value"
} }
# 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"
# Ping-Test durchführen
echo "teste ping $A72_ip"
local ping_output=$(/bin/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
fi
} }
# check_ip -Funktion ausführen. # check_ip -Funktion ausführen.
@ -70,6 +91,7 @@ check_notification() {
for ((i = 1; i <= $max_attempts; i++)); do for ((i = 1; i <= $max_attempts; i++)); do
# Führe den SSH-Befehl innerhalb eines Timeout aus # Führe den SSH-Befehl innerhalb eines Timeout aus
check_ip # IP holen
output=$(ssh -i /root/.ssh/A72 root@$A72_ip 'dumpsys activity processes | grep com.urbandroid.sleep') output=$(ssh -i /root/.ssh/A72 root@$A72_ip 'dumpsys activity processes | grep com.urbandroid.sleep')
echo $output echo $output
@ -140,13 +162,13 @@ colortemp_step=$(bc <<< "$start_colortemp / $steps")
set_brightness "$current_brightness" set_brightness "$current_brightness"
set_colortemp "$current_colortemp" set_colortemp "$current_colortemp"
sleep 27 # Änderungen alle 30 (27) Sekunden sleep 30 # Änderungen alle 30 Sekunden
done done
# check_ip -Funktion ausführen. # check_ip -Funktion ausführen.
check_ip check_ip
echo "$A72_ip" echo "$A72_ip"
# Starten des Alarm-Scripts auf dem Handy. # Starten des Alarm-Scripts auf dem Handy.
ssh -i /root/.ssh/A72 root@$A72_ip '/data/scripts/morning-alarm.sh' > /root/morning_alarm.log ssh -i /root/.ssh/A72 root@$A72_ip '/data/scripts/morning-alarm.sh' > /root/morning_alarm.log