Refactor wecker.sh script

Made the following changes to the wecker.sh script:
- Adjusted comments and translated the comments and script messages into English for better clarity.
- Introduced error handling for SSH commands, Bluetooth operations, and volume adjustments to ensure the script exits gracefully in case of failures.
- Updated variable names for consistency and improved overall readability of the script.
- Removed unnecessary debug-related code and streamlined the script structure.
- Addressed formatting issues for better code presentation.
This commit is contained in:
Ratatoskr 2024-01-26 23:47:53 +01:00
parent fe66c936c4
commit 6d5ce74f08
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

View File

@ -9,18 +9,20 @@
# After 15 minutes, a color change is triggered.
# Customizable settings:
# - 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
debug=false
ssh_executed=false # Variable to track SSH command status
while getopts ":d" opt; do
case $opt in
d)
DEBUG=true
debug=true
;;
\?)
\ß)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
@ -39,26 +41,6 @@ iobroker state set zigbee.0.04cd15fffee03198.brightness 1
iobroker state set zigbee.0.04cd15fffee03198.colortemp 1600
iobroker state set zigbee.0.04cd15fffee03198.state true
set_brightness() {
local brightness_value=$1
# Replace 'zigbee.0.04cd15fffee03198.brightness' with the actual ioBroker address for brightness
iobroker state set zigbee.0.04cd15fffee03198.brightness "$brightness_value"
echo "Current brightness: $brightness_value"
}
set_colortemp() {
local colortemp_value=$1
# Replace 'zigbee.0.04cd15fffee03198.colortemp' with the actual ioBroker address for color temperature
iobroker state set zigbee.0.04cd15fffee03198.colortemp "$colortemp_value"
echo "Current Colortemp: $colortemp_value"
}
set_color() {
local color_value=$1
# Replace 'zigbee.0.04cd15fffee03198.color' with the actual ioBroker address for color
iobroker state set zigbee.0.04cd15fffee03198.color "$color_value"
}
# Get the IP address of Lisa's iPhone (without ping test)
check_ip() {
mac_address="e4:cd:d1" # Replace this with the MAC address
@ -75,8 +57,8 @@ check_ip() {
if [ $i -lt $attempts ]; then
echo "Wait $interval seconds before the next attempt."
if [ "$DEBUG" = true ]; then
echo "Debug: Skipping pause"
if [ "$debug" = true ]; then
echo "DEBUG: Skipping pause"
else
sleep $interval
fi
@ -114,25 +96,26 @@ check_notification() {
# Execute the SSH command within a timeout
check_ip # Get IP
if [ "$DEBUG" = true ]; then
if [ "$debug" = true ]; then
echo "DEBUG: Bypassing check_notification."
output="com.urbandroid.sleep AlarmKlaxon"
else
output=$(ssh -i /root/.ssh/A72 root@$A72_ip 'dumpsys activity processes | grep com.urbandroid.sleep')
#echo $output
echo $output
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
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
/root/iobroker_scripts/general/generate_text.sh > /dev/null
return # Exit the function without terminating the entire script
else
# Phone unreachable
@ -160,13 +143,14 @@ end_colortemp=0 # Cool white (0)
# Duration of adjustment in seconds (15 minutes)
# Change every 30 seconds
if [ "$DEBUG" = true ]; then
change_interval_seconds=30
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
steps=$((duration_seconds / change_interval_seconds))
fi
# Calculate steps for brightness and color temperature per step
@ -200,7 +184,7 @@ for ((i = 1; i <= $steps; i++)); do
set_brightness "$current_brightness"
set_colortemp "$current_colortemp"
if [ "$DEBUG" = true ]; then
if [ "$debug" = true ]; then
sleep 12
echo "DEBUG: Wait 12 sec. between steps."
else
@ -225,15 +209,15 @@ echo "current_colortemp: $current_colortemp end_colortemp: $end_colortemp"
ssh_command() { # Function to send a command to A72.
local command=$1
nohup ssh -i /root/.ssh/A72 -f root@$A72_ip "$command" </dev/null &
SSH_EXECUTED=true # Mark that the SSH command has been executed.
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
if [ "$ssh_executed" = false ]; then
if [ "$debug" = true ]; then
echo "DEBUG: Alternative ssh command."
ssh_command "cmd media_session volume scripts/--set 5 --stream 9 && /data/data/com.termux/files/usr/bin/mpv /data/scripts/your-new-morning-alarm.ogg"
else
@ -250,3 +234,4 @@ if [ $current_brightness -eq $end_brightness ] && [ $current_colortemp -eq $end_
fi
done
fi