The wecker.sh script has been modified with the following changes: 1. Modified the 'check_notification()' function to exit the entire script using 'exit' instead of 'return' when the alarm is turned off or the phone is unreachable. This ensures the script terminates completely in these scenarios. 2. Adjusted the 'sleep_duration' calculation to subtract 3 seconds, enhancing the script's timing accuracy during gradual adjustment of brightness and color temperature. 3. Changed the ssh command in the loop to use 'nohup' and 'tee' for logging the morning-alarm.sh output to a file named 'morning-alarm.log'. This modification improves logging and captures script output for later analysis. These changes enhance the wecker.sh script by improving script termination logic, adjusting sleep duration for better timing accuracy, and enhancing logging during the execution of the morning-alarm.sh script.
258 lines
8.3 KiB
Bash
Executable File
258 lines
8.3 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Alarm script with brightness and color temperature adjustment for ioBroker
|
|
#
|
|
# This script gradually adjusts the brightness and changes the color temperature from warm white to cool white
|
|
# via ioBroker control. It increases brightness from 1 to 100 and changes color temperature from 1600 (warm white)
|
|
# to 0 (cool white) within 15 minutes.
|
|
#
|
|
# 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
|
|
|
|
while getopts ":d" opt; do
|
|
case $opt in
|
|
d)
|
|
debug=true
|
|
;;
|
|
\ß)
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Adjust brightness and color temperature in ioBroker
|
|
|
|
# Colors
|
|
red="ff0000"
|
|
green="00ff00"
|
|
blue="0000ff"
|
|
|
|
iobroker state set zigbee.0.04cd15fffee03198.state false
|
|
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
|
|
attempts=10
|
|
interval=5
|
|
|
|
for ((i = 1; i <= $attempts; i++)); do
|
|
A72_ip=$(arp-scan -r 3 -v --localnet | grep "$mac_address" | awk '{print $1}')
|
|
|
|
if [ -n "$A72_ip" ]; then
|
|
echo "Found IP: $A72_ip"
|
|
return 0 # Success: IP address found
|
|
fi
|
|
|
|
if [ $i -lt $attempts ]; then
|
|
echo "Wait $interval seconds before the next attempt."
|
|
if [ "$debug" = true ]; then
|
|
echo "DEBUG: Skipping pause"
|
|
else
|
|
sleep $interval
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# All attempts unsuccessful
|
|
echo "The target could not be reached after $attempts attempts."
|
|
iobroker state set zigbee.0.04cd15fffee03198.state false
|
|
return 1 # Error: IP address not found
|
|
}
|
|
|
|
# Execute the check_ip function.
|
|
check_ip
|
|
|
|
# Check if it's the first brightness change
|
|
first_brightness_change=true
|
|
|
|
# Check if the light was externally turned off
|
|
check_external_light_status() {
|
|
local light_status=$(iobroker state getValue zigbee.0.04cd15fffee03198.state)
|
|
if [ "$light_status" == "false" ]; then
|
|
echo "Light is off. Exiting the script." && date
|
|
echo $light_status
|
|
exit
|
|
fi
|
|
}
|
|
|
|
# Check if the alarm on the phone has been turned off
|
|
check_notification() {
|
|
max_attempts=10
|
|
wait_time=5
|
|
|
|
for ((i = 1; i <= $max_attempts; i++)); do
|
|
# Execute the SSH command within a timeout
|
|
check_ip # Get IP
|
|
|
|
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
|
|
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
|
|
/root/iobroker_scripts/general/generate_text.sh > /dev/null
|
|
exit # Beende das Skript, wenn der Alarm ausgeschaltet wurde
|
|
else
|
|
# Phone unreachable
|
|
echo "Phone unreachable. Waiting $wait_time seconds before the next attempt."
|
|
check_ip # Get IP
|
|
sleep $wait_time
|
|
fi
|
|
done
|
|
|
|
# Phone could not be reached.
|
|
echo "Phone could not be reached after $max_attempts attempts."
|
|
date && echo "Alarm turned off."
|
|
iobroker state set zigbee.0.04cd15fffee03198.state false
|
|
exit # Beende das Skript, wenn das Telefon nicht erreicht werden konnte
|
|
}
|
|
|
|
# Define starting values for brightness and color temperature
|
|
start_brightness=0 # Minimum brightness (0)
|
|
start_colortemp=1600 # Warm white (1600)
|
|
|
|
# Define ending values for brightness and color temperature
|
|
end_brightness=100 # Maximum brightness (100)
|
|
end_colortemp=0 # Cool white (0)
|
|
|
|
# Duration of adjustment in seconds (15 minutes)
|
|
|
|
# Change every 30 seconds
|
|
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=$((duration_seconds / change_interval_seconds))
|
|
fi
|
|
|
|
# Calculate steps for brightness and color temperature per step
|
|
brightness_step=$(( (end_brightness - start_brightness) / steps ))
|
|
echo "Brightness difference per step: $brightness_step"
|
|
echo "Reached final brightness: $((brightness_step * steps + start_brightness)) from $end_brightness"
|
|
|
|
colortemp_step=$(( start_colortemp / steps ))
|
|
echo "Color temperature difference per step: $colortemp_step"
|
|
echo "Reached final colortemp: $((start_colortemp - colortemp_step * steps)) from $end_colortemp"
|
|
sleep_duration=$((duration_seconds / steps - 3))
|
|
|
|
# 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)"))
|
|
|
|
# Debug
|
|
echo "------------------------------------Step: $i------------------------------------"
|
|
echo "current brightness= $current_brightness"
|
|
echo "current colortemp= $current_colortemp"
|
|
|
|
if [ "$first_brightness_change" = true ]; then
|
|
echo "Variable first_brightness_change = true"
|
|
check_external_light_status
|
|
else
|
|
echo "Variable first_brightness_change = false"
|
|
first_brightness_change=false
|
|
fi
|
|
|
|
set_brightness "$current_brightness"
|
|
set_colortemp "$current_colortemp"
|
|
|
|
if [ "$debug" = true ]; then
|
|
sleep 12
|
|
echo "DEBUG: Wait 12 sec. between steps."
|
|
else
|
|
sleep $sleep_duration # Changes every 30 seconds
|
|
fi
|
|
done
|
|
|
|
# Set to end values.
|
|
current_brightness="$end_brightness"
|
|
current_colortemp="$end_colortemp"
|
|
set_brightness "$end_brightness"
|
|
set_colortemp "$end_colortemp"
|
|
|
|
# Execute the check_ip function.
|
|
check_ip
|
|
echo "$A72_ip"
|
|
|
|
# Check if brightness and color temperature have reached their target values
|
|
echo "current_brightness: $current_brightness end_brightness: $end_brightness"
|
|
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.
|
|
}
|
|
|
|
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 scripts/--set 5 --stream 9 && /data/data/com.termux/files/usr/bin/mpv /data/scripts/your-new-morning-alarm.ogg"
|
|
else
|
|
nohup ssh a72 '/data/scripts/morning-alarm.sh 2>&1 | /data/data/com.termux/files/usr/bin/tee /data/scripts/morning-alarm.log'
|
|
fi
|
|
else
|
|
check_external_light_status
|
|
set_color "$green"
|
|
sleep 5
|
|
set_color "$red"
|
|
sleep 5
|
|
set_color "$blue"
|
|
check_notification
|
|
fi
|
|
done
|
|
fi
|
|
|