Refactor wecker.sh script for better readability and functionality

- Updated comments for better clarity and documentation
- Introduced a DEBUG variable to toggle debugging information
- Utilized set -e to stop the script on errors for better error handling
- Corrected the script flow to ensure proper execution of color changes
- Introduced a new ssh_command function for executing SSH commands
- Improved check_ip function for finding the IP address more reliably
- Added DEBUG checks to bypass certain functions for debugging purposes
- Fixed issues with sleep intervals during script execution
- Improved logging by adding echo statements for important steps
- Adjusted variable names for better readability
- Removed unnecessary comments and debug statements
- Added set -e for better error handling
- Introduced command-line option -d for enabling DEBUG mode
- Improved formatting and indentation for better code readability
This commit is contained in:
Ratatoskr 2023-12-29 21:55:49 +01:00
parent aa04b75e41
commit 3d2605a8f8
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

275
wecker.sh
View File

@ -1,187 +1,242 @@
#!/bin/bash #bin/bash
# #
# Wecker-Skript mit Helligkeits- und Farbtemperaturanpassung für ioBroker # Alarm script with brightness and color temperature adjustment for ioBroker
# #
# Dieses Skript passt die Helligkeit schrittweise an und ändert die Farbtemperatur von warmweiß zu kaltweiß # This script gradually adjusts the brightness and changes the color temperature from warm white to cool white
# über die ioBroker-Steuerung. Es erhöht die Helligkeit von 1 auf 100 und ändert die Farbtemperatur von 1600 (warmweiß) # via ioBroker control. It increases brightness from 1 to 100 and changes color temperature from 1600 (warm white)
# auf 0 (kaltweiß) innerhalb von 15 Minuten. # to 0 (cool white) within 15 minutes.
# #
# Nach den 15 Minuten wird ein Farbwechsel erstellt. # After 15 minutes, a color change is triggered.
# Anpassbare Einstellungen: # Customizable settings:
# - ioBroker-Helligkeits- und Farbtemperatur-Befehle anpassen # - Adjust ioBroker brightness and color temperature commands
set -e # Stop the script on errors
#set -x # Enable debugging # Uncomment for a more detailed log
# Debug
DEBUG=false
SSH_EXECUTED=false # Variable to track SSH command status
# Helligkeit und Farbtemperatur an ioBroker anpassen while getopts ":d" opt; do
case $opt in
d)
DEBUG=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Farben # Adjust brightness and color temperature in ioBroker
rot="ff0000"
gruen="00ff00"
blau="0000ff"
iobroker state set zigbee.0.04cd15fffee03198.state false # Colors
iobroker state set zigbee.0.04cd15fffee03198.brightness 0 red="ff0000"
iobroker state set zigbee.0.04cd15fffee03198.colortemp 1600 green="00ff00"
blue="0000ff"
iobroker state set zigbee.0.04cd15fffee03198.state false && sleep 2 &&
iobroker state set zigbee.0.04cd15fffee03198.brightness 1 && sleep 2 &&
iobroker state set zigbee.0.04cd15fffee03198.colortemp 1600 && sleep 2 &&
iobroker state set zigbee.0.04cd15fffee03198.state true && sleep 2 &&
set_brightness() { set_brightness() {
local brightness_value=$1 local brightness_value=$1
# Ersetze 'zigbee.0.04cd15fffee03198.brightness' durch die tatsächliche ioBroker-Adresse für die Helligkeit # Replace 'zigbee.0.04cd15fffee03198.brightness' with the actual ioBroker address for brightness
iobroker state set zigbee.0.04cd15fffee03198.brightness "$brightness_value" iobroker state set zigbee.0.04cd15fffee03198.brightness "$brightness_value"
echo "Current brightness: $brightness_value"
} }
set_colortemp() { set_colortemp() {
local colortemp_value=$1 local colortemp_value=$1
# Ersetze 'zigbee.0.04cd15fffee03198.colortemp' durch die tatsächliche ioBroker-Adresse für die Farbtemperatur # Replace 'zigbee.0.04cd15fffee03198.colortemp' with the actual ioBroker address for color temperature
iobroker state set zigbee.0.04cd15fffee03198.colortemp "$colortemp_value" iobroker state set zigbee.0.04cd15fffee03198.colortemp "$colortemp_value"
echo "Current Colortemp: $colortemp_value"
} }
set_color() { set_color() {
local color_value=$1 local color_value=$1
# Ersetze 'zigbee.0.04cd15fffee03198.color' durch die tatsächliche ioBroker-Adresse für die Farbe # Replace 'zigbee.0.04cd15fffee03198.color' with the actual ioBroker address for 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 (ohne Ping-Test) # Get the IP address of Lisa's iPhone (without ping test)
check_ip() { 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 attempts=10
interval=5 interval=2
for ((i = 1; i <= $attempts; i++)); do for ((i = 1; i <= $attempts; i++)); do
A72_ip=$(arp-scan -r 5 -v --localnet | grep "$mac_address" | awk '{print $1}') A72_ip=$(arp-scan -r 3 -v --localnet | grep "$mac_address" | awk '{print $1}')
if [ -n "$A72_ip" ]; then if [ -n "$A72_ip" ]; then
echo "Gefundene IP: $A72_ip" echo "Found IP: $A72_ip"
return 0 # Erfolg: IP-Adresse gefunden return 0 # Success: IP address found
fi fi
if [ $i -lt $attempts ]; then if [ $i -lt $attempts ]; then
echo "Warte $interval Sekunden, bevor der nächste Versuch gestartet wird." echo "Wait $interval seconds before the next attempt."
sleep $interval if [ "$DEBUG" = true ]; then
echo "Debug: Skipping pause"
else
sleep $interval
fi
fi fi
done done
# Alle Versuche erfolglos # All attempts unsuccessful
echo "Das Ziel konnte nach $attempts Versuchen nicht erreicht werden." echo "The target could not be reached after $attempts attempts."
iobroker state set zigbee.0.04cd15fffee03198.state false iobroker state set zigbee.0.04cd15fffee03198.state false || true
return 1 # Fehler: IP-Adresse nicht gefunden return 1 # Error: IP address not found
} }
# check_ip -Funktion ausführen. # Execute the check_ip function.
check_ip check_ip
iobroker state set zigbee.0.04cd15fffee03198.state true # Check if it's the first brightness change
# Überprüfung, ob es die erste Änderung der Helligkeit ist
first_brightness_change=true first_brightness_change=true
# Überprüfung, ob das Licht extern ausgeschaltet wurde # Check if the light was externally turned off
check_external_light_status() { check_external_light_status() {
local light_status=$(iobroker state getValue zigbee.0.04cd15fffee03198.state) local light_status=$(iobroker state getValue zigbee.0.04cd15fffee03198.state)
if [ "$light_status" == "false" ]; then if [ "$light_status" == "false" ]; then
echo "Licht ist aus. Das Skript wird beendet." && date echo "Light is off. Exiting the script." && date
exit exit
fi fi
} }
# Checkt, ob der Wecker am Handy ausgeschaltet wurde. # Check if the alarm on the phone has been turned off
check_notification() { check_notification() {
max_attempts=10 max_attempts=10
wait_time=5 wait_time=5
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 # Execute the SSH command within a timeout
check_ip # IP holen check_ip # Get IP
output=$(ssh -i /root/.ssh/A72 root@$A72_ip 'dumpsys activity processes | grep com.urbandroid.sleep')
#echo $output
# Überprüfen, ob das Ergebnis den Wecker enthält if [ "$DEBUG" = true ]; then
if [[ $output == *"com.urbandroid.sleep"* && $output == *"AlarmKlaxon"* ]]; then echo "DEBUG: Bypassing check_notification."
# Wecker noch nicht beendet. output="com.urbandroid.sleep AlarmKlaxon"
date && echo "Wecker noch nicht beendet."
#echo $output
sleep $wait_time # Wartezeit von 10 Sekunden
elif [[ $output == *"com.urbandroid.sleep"* && $output != *"AlarmKlaxon"* ]]; then
# Wecker beendet.
date && echo "Wecker beendet."
echo $output
iobroker state set zigbee.0.04cd15fffee03198.state false
return # Die Funktion verlassen, ohne das gesamte Skript zu beenden
else else
# Handy nicht erreichbar output=$(ssh -i /root/.ssh/A72 root@$A72_ip 'dumpsys activity processes | grep com.urbandroid.sleep')
echo "Handy nicht erreichbar. Warte $wait_time Sekunden, bevor der nächste Versuch gestartet wird." #echo $output
check_ip # IP holen fi
# Check if the result contains the alarm
if [[ $output == *"com.urbandroid.sleep"* && $output == *"AlarmKlaxon"* ]]; then
# Alarm not turned off yet
date && echo "Alarm not turned off yet."
#echo $output
return
elif [[ $output == *"com.urbandroid.sleep"* && $output != *"AlarmKlaxon"* ]]; then
# Alarm turned off
date && echo "Alarm turned off."
echo $output
iobroker state set zigbee.0.04cd15fffee03198.state false || true
return # Exit the function without terminating the entire script
else
# Phone unreachable
echo "Phone unreachable. Waiting $wait_time seconds before the next attempt."
check_ip # Get IP
sleep $wait_time sleep $wait_time
fi fi
done done
# Handy konnte nicht erreicht werden. # Phone could not be reached.
echo "Handy konnte nach $max_attempts Versuchen nicht erreicht werden." echo "Phone could not be reached after $max_attempts attempts."
date && echo "Wecker beendet." date && echo "Alarm turned off."
iobroker state set zigbee.0.04cd15fffee03198.state false iobroker state set zigbee.0.04cd15fffee03198.state false || true
exit return
} }
# Startwerte für Helligkeit und Farbtemperatur definieren # Define starting values for brightness and color temperature
start_brightness=1 # Mindesthelligkeit (1) start_brightness=0 # Minimum brightness (0)
start_colortemp=1600 # Warmweiß (1600) start_colortemp=1600 # Warm white (1600)
# Endwerte für Helligkeit und Farbtemperatur definieren # Define ending values for brightness and color temperature
end_brightness=100 # Höchste Helligkeit (100) end_brightness=100 # Maximum brightness (100)
end_colortemp=0 # Kaltweiß (0) end_colortemp=0 # Cool white (0)
# Dauer der Anpassung in Sekunden (15 Minuten) # Duration of adjustment in seconds (15 minutes)
duration_seconds=900
# Alle 30 Sekunden Änderung # Change every 30 seconds
steps=30 # Alle 30 Sekunden einen Schritt if [ "$DEBUG" = true ]; then
duration_seconds=60
steps=5
echo "DEBUG: Set steps to $steps; and duration to $duration_seconds sec."
else
duration_seconds=900
steps=30 # Change every 30 (27) seconds
fi
# Berechne die Schritte für Helligkeit und Farbtemperatur pro Schritt # Calculate steps for brightness and color temperature per step
brightness_step=$(bc <<< "($end_brightness - $start_brightness) / $steps") brightness_step=$(bc <<< "($end_brightness - $start_brightness) / $steps")
echo "Brightness difference per step: $brightness_step"
echo "Reached final brightness: $((brightness_step * steps)) from $end_brightness"
colortemp_step=$(bc <<< "$start_colortemp / $steps") colortemp_step=$(bc <<< "$start_colortemp / $steps")
echo "Color temperature difference per step: $colortemp_step"
echo "Reached final colortemp: $((start_colortemp - (colortemp_step * steps))) from $end_colortemp"
# Gradual adjustment of brightness and color temperature
for ((i = 1; i <= $steps; i++)); do
current_brightness=$(printf "%.0f" $(bc <<< "$start_brightness + ($brightness_step * $i)"))
current_colortemp=$(printf "%.0f" $(bc <<< "$start_colortemp - ($colortemp_step * $i)"))
# Schrittweise Anpassung von Helligkeit und Farbtemperatur # Debug
for ((i = 1; i < $steps; i++)); do echo "------------------------------------Step: $i------------------------------------"
current_brightness=$(bc <<< "$start_brightness + ($brightness_step * $i)") echo "current brightness= $current_brightness"
current_colortemp=$(bc <<< "$start_colortemp - ($colortemp_step * $i)") echo "current colortemp= $current_colortemp"
# Debugg if [ "$first_brightness_change" = true ]; then
echo "Step: $i" echo "Variable first_brightness_change = true"
echo "current brightness= $current_brightness" check_external_light_status
echo "current colortemp= $current_colortemp" else
echo "Variable first_brightness_change = false"
first_brightness_change=false
fi
if [ "$first_brightness_change" = false ]; then set_brightness "$current_brightness"
check_external_light_status set_colortemp "$current_colortemp"
else
first_brightness_change=false
fi
set_brightness "$current_brightness" if [ "$DEBUG" = true ]; then
set_colortemp "$current_colortemp" sleep 12
echo "DEBUG: Wait 12 sec. between steps."
sleep 27 # Änderungen alle 30 (27) Sekunden else
sleep 27 # Changes every 30 (27) seconds
fi
done done
# check_ip -Funktion ausführen. # Execute the check_ip function.
check_ip check_ip
echo "$A72_ip" echo "$A72_ip"
# Starten des Alarm-Scripts auf dem Handy. # Check if brightness and color temperature have reached their target values
echo "Starten des morning-alarm.sh Scripts am Ziel: $A72_ip" echo "current_brightness: $current_brightness end_brightness: $end_brightness"
nohup ssh -i /root/.ssh/A72 root@$A72_ip "/data/scripts/morning-alarm.sh > /dev/null" & echo "current_colortemp: $current_colortemp end_colortemp: $end_colortemp"
# Farben ändern für mehr konzentration ssh_command() { # Function to send a command to A72.
while :; do local command=$1
if [ "$first_brightness_change" = false ]; then nohup ssh -i /root/.ssh/A72 -f root@$A72_ip "$command" </dev/null &
SSH_EXECUTED=true # Mark that the SSH command has been executed.
}
if [ $current_brightness -eq $end_brightness ] && [ $current_colortemp -eq $end_colortemp ]; then
# Change colors for more concentration
while :; do
# Start the alarm script on the phone if the SSH command has not been executed yet.
if [ "$SSH_EXECUTED" = false ]; then
if [ "$DEBUG" = true ]; then
echo "DEBUG: Alternative ssh command."
ssh_command "cmd media_session volume --set 5 --stream 9 && /data/data/com.termux/files/usr/bin/mpv /product/media/audio/alarms/your-new-morning-alarm.ogg"
else
ssh_command "/data/scripts/morning-alarm.sh > /dev/null"
fi
else
check_external_light_status check_external_light_status
else set_color "$green"
first_brightness_change=false sleep 5
set_color "$gruen" set_color "$red"
sleep 5 sleep 5
set_color "$rot" set_color "$blue"
sleep 5 check_notification
set_color "$blau" fi
done
check_notification fi
fi
done