Refactor generate_text.sh script:

- Replace HTML file creation with Markdown content generation for improved simplicity and readability.
- Construct Markdown content with a header, table, and row entries based on the generated text components.
- Introduce a curl_command variable to construct and echo the curl command for sending a notification to A72.
- Display the constructed Markdown content for debugging purposes.
- Transfer text files to A72's sdcard instead of the scripts directory.
- Start generate_mp3.sh on A72 using nohup and ssh.
- Adjust comments and remove unnecessary HTML file display section.
- Overall, enhance the script structure, content creation, and notification handling in generate_text.sh.
This commit is contained in:
Ratatoskr 2024-02-06 17:41:48 +01:00
parent a5076ae046
commit 4fe4ef7de7
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

View File

@ -13,6 +13,9 @@
#set -e # Stop the script on errors
#set -x # Enable debugging # Uncomment for a more detailed log
# Variables
alarm_dismissed=false # Variable to track alarm status
# Debug
debug=false
ssh_executed=false # Variable to track SSH command status
@ -107,12 +110,12 @@ check_external_light_status() {
fi
}
# Check if the alarm on the phone has been turned off
# Function to check the alarm notification
check_notification() {
max_attempts=10
wait_time=5
for ((i = 1; i <= $max_attempts; i++)); do
for ((i = 1; i <= $max_attempts;)); do
# Execute the SSH command within a timeout
check_ip # Get IP
@ -121,26 +124,25 @@ 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
return
#echo $output
elif [[ $output == *"com.urbandroid.sleep"* && $output != *"AlarmKlaxon"* ]]; then
# Alarm turned off
date && echo "Alarm turned off."
echo $output
ssh -i /root/.ssh/A72 root@$A72_ip 'killall mpv'
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
exit # Exit the script when the alarm is turned off
else
# Phone unreachable
echo "Phone unreachable. Waiting $wait_time seconds before the next attempt."
check_ip # Get IP
i++
sleep $wait_time
fi
done
@ -148,8 +150,9 @@ check_notification() {
# Phone could not be reached.
echo "Phone could not be reached after $max_attempts attempts."
date && echo "Alarm turned off."
ssh -i /root/.ssh/A72 root@$A72_ip 'killall mpv'
iobroker state set zigbee.0.04cd15fffee03198.state false
exit # Beende das Skript, wenn das Telefon nicht erreicht werden konnte
exit # Exit the script when the phone is unreachable
}
# Define starting values for brightness and color temperature
@ -184,7 +187,7 @@ echo "Reached final colortemp: $((start_colortemp - colortemp_step * steps)) fro
sleep_duration=$((duration_seconds / steps - 3))
# Gradual adjustment of brightness and color temperature
for ((i = 1; i <= $steps; i++)); do
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)"))
@ -193,6 +196,7 @@ for ((i = 1; i <= $steps; i++)); do
echo "current brightness= $current_brightness"
echo "current colortemp= $current_colortemp"
# Check if it's the first brightness change
if [ "$first_brightness_change" = true ]; then
echo "Variable first_brightness_change = true"
check_external_light_status
@ -210,6 +214,11 @@ for ((i = 1; i <= $steps; i++)); do
else
sleep $sleep_duration # Changes every 30 seconds
fi
# Check if the alarm is dismissed
if [ "$alarm_dismissed" = true ]; then
exit # Exit the script when the alarm is dismissed
fi
done
# Set to end values.
@ -226,22 +235,38 @@ echo "$A72_ip"
echo "current_brightness: $current_brightness end_brightness: $end_brightness"
echo "current_colortemp: $current_colortemp end_colortemp: $end_colortemp"
# Check if the alarm is dismissed
if [ "$alarm_dismissed" = true ]; then
exit # Exit the script when the alarm is dismissed
fi
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.
# Start the SSH command in the background and store the PID.
nohup ssh -i /root/.ssh/A72 root@$A72_ip "$command" > /dev/null 2>&1 &
echo "$ssh_executed"
ssh_pid=$! # Store the PID of the SSH process.
echo "ssh_pid: $ssh_pid"+
# Wait briefly to ensure that the process has started.
sleep 1
}
if [ $current_brightness -eq $end_brightness ] && [ $current_colortemp -eq $end_colortemp ]; then
# Change colors for more concentration
# Continue with the color-changing loop
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"
ssh_command "cmd media_session volume --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'
sleep 1
ssh_command "/data/scripts/morning-alarm.sh >> /data/scripts/morning-alarm.log" &
ssh_executed=true
fi
else
check_external_light_status
@ -250,7 +275,12 @@ if [ $current_brightness -eq $end_brightness ] && [ $current_colortemp -eq $end_
set_color "$red"
sleep 5
set_color "$blue"
check_notification
check_notification #&
fi
if [ "$alarm_dismissed" = true ]; then
echo "Alarm dismissed"
/root/iobroker_scripts/general/generate_text.sh # Call generate_text.sh script
exit # Exit the script when the alarm is dismissed
fi
done
fi