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:
parent
fe66c936c4
commit
6d5ce74f08
55
wecker.sh
55
wecker.sh
@ -9,18 +9,20 @@
|
|||||||
# After 15 minutes, a color change is triggered.
|
# After 15 minutes, a color change is triggered.
|
||||||
# Customizable settings:
|
# Customizable settings:
|
||||||
# - Adjust ioBroker brightness and color temperature commands
|
# - Adjust ioBroker brightness and color temperature commands
|
||||||
|
|
||||||
set -e # Stop the script on errors
|
set -e # Stop the script on errors
|
||||||
set -x # Enable debugging # Uncomment for a more detailed log
|
set -x # Enable debugging # Uncomment for a more detailed log
|
||||||
|
|
||||||
# Debug
|
# Debug
|
||||||
DEBUG=false
|
debug=false
|
||||||
SSH_EXECUTED=false # Variable to track SSH command status
|
ssh_executed=false # Variable to track SSH command status
|
||||||
|
|
||||||
while getopts ":d" opt; do
|
while getopts ":d" opt; do
|
||||||
case $opt in
|
case $opt in
|
||||||
d)
|
d)
|
||||||
DEBUG=true
|
debug=true
|
||||||
;;
|
;;
|
||||||
\?)
|
\ß)
|
||||||
echo "Invalid option: -$OPTARG" >&2
|
echo "Invalid option: -$OPTARG" >&2
|
||||||
exit 1
|
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.colortemp 1600
|
||||||
iobroker state set zigbee.0.04cd15fffee03198.state true
|
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)
|
# Get the IP address of Lisa's iPhone (without ping test)
|
||||||
check_ip() {
|
check_ip() {
|
||||||
mac_address="e4:cd:d1" # Replace this with the MAC address
|
mac_address="e4:cd:d1" # Replace this with the MAC address
|
||||||
@ -75,8 +57,8 @@ check_ip() {
|
|||||||
|
|
||||||
if [ $i -lt $attempts ]; then
|
if [ $i -lt $attempts ]; then
|
||||||
echo "Wait $interval seconds before the next attempt."
|
echo "Wait $interval seconds before the next attempt."
|
||||||
if [ "$DEBUG" = true ]; then
|
if [ "$debug" = true ]; then
|
||||||
echo "Debug: Skipping pause"
|
echo "DEBUG: Skipping pause"
|
||||||
else
|
else
|
||||||
sleep $interval
|
sleep $interval
|
||||||
fi
|
fi
|
||||||
@ -114,25 +96,26 @@ check_notification() {
|
|||||||
# Execute the SSH command within a timeout
|
# Execute the SSH command within a timeout
|
||||||
check_ip # Get IP
|
check_ip # Get IP
|
||||||
|
|
||||||
if [ "$DEBUG" = true ]; then
|
if [ "$debug" = true ]; then
|
||||||
echo "DEBUG: Bypassing check_notification."
|
echo "DEBUG: Bypassing check_notification."
|
||||||
output="com.urbandroid.sleep AlarmKlaxon"
|
output="com.urbandroid.sleep AlarmKlaxon"
|
||||||
else
|
else
|
||||||
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if the result contains the alarm
|
# Check if the result contains the alarm
|
||||||
if [[ $output == *"com.urbandroid.sleep"* && $output == *"AlarmKlaxon"* ]]; then
|
if [[ $output == *"com.urbandroid.sleep"* && $output == *"AlarmKlaxon"* ]]; then
|
||||||
# Alarm not turned off yet
|
# Alarm not turned off yet
|
||||||
date && echo "Alarm not turned off yet."
|
date && echo "Alarm not turned off yet."
|
||||||
#echo $output
|
echo $output
|
||||||
return
|
return
|
||||||
elif [[ $output == *"com.urbandroid.sleep"* && $output != *"AlarmKlaxon"* ]]; then
|
elif [[ $output == *"com.urbandroid.sleep"* && $output != *"AlarmKlaxon"* ]]; then
|
||||||
# Alarm turned off
|
# Alarm turned off
|
||||||
date && echo "Alarm turned off."
|
date && echo "Alarm turned off."
|
||||||
echo $output
|
echo $output
|
||||||
iobroker state set zigbee.0.04cd15fffee03198.state false
|
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
|
return # Exit the function without terminating the entire script
|
||||||
else
|
else
|
||||||
# Phone unreachable
|
# Phone unreachable
|
||||||
@ -160,13 +143,14 @@ end_colortemp=0 # Cool white (0)
|
|||||||
# Duration of adjustment in seconds (15 minutes)
|
# Duration of adjustment in seconds (15 minutes)
|
||||||
|
|
||||||
# Change every 30 seconds
|
# Change every 30 seconds
|
||||||
if [ "$DEBUG" = true ]; then
|
change_interval_seconds=30
|
||||||
|
if [ "$debug" = true ]; then
|
||||||
duration_seconds=60
|
duration_seconds=60
|
||||||
steps=5
|
steps=5
|
||||||
echo "DEBUG: Set steps to $steps; and duration to $duration_seconds sec."
|
echo "DEBUG: Set steps to $steps; and duration to $duration_seconds sec."
|
||||||
else
|
else
|
||||||
duration_seconds=900
|
duration_seconds=900
|
||||||
steps=30 # Change every 30 (27) seconds
|
steps=$((duration_seconds / change_interval_seconds))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Calculate steps for brightness and color temperature per step
|
# 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_brightness "$current_brightness"
|
||||||
set_colortemp "$current_colortemp"
|
set_colortemp "$current_colortemp"
|
||||||
|
|
||||||
if [ "$DEBUG" = true ]; then
|
if [ "$debug" = true ]; then
|
||||||
sleep 12
|
sleep 12
|
||||||
echo "DEBUG: Wait 12 sec. between steps."
|
echo "DEBUG: Wait 12 sec. between steps."
|
||||||
else
|
else
|
||||||
@ -225,15 +209,15 @@ echo "current_colortemp: $current_colortemp end_colortemp: $end_colortemp"
|
|||||||
ssh_command() { # Function to send a command to A72.
|
ssh_command() { # Function to send a command to A72.
|
||||||
local command=$1
|
local command=$1
|
||||||
nohup ssh -i /root/.ssh/A72 -f root@$A72_ip "$command" </dev/null &
|
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
|
if [ $current_brightness -eq $end_brightness ] && [ $current_colortemp -eq $end_colortemp ]; then
|
||||||
# Change colors for more concentration
|
# Change colors for more concentration
|
||||||
while :; do
|
while :; do
|
||||||
# Start the alarm script on the phone if the SSH command has not been executed yet.
|
# Start the alarm script on the phone if the SSH command has not been executed yet.
|
||||||
if [ "$SSH_EXECUTED" = false ]; then
|
if [ "$ssh_executed" = false ]; then
|
||||||
if [ "$DEBUG" = true ]; then
|
if [ "$debug" = true ]; then
|
||||||
echo "DEBUG: Alternative ssh command."
|
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"
|
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
|
else
|
||||||
@ -250,3 +234,4 @@ if [ $current_brightness -eq $end_brightness ] && [ $current_colortemp -eq $end_
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user