Improve script execution and debugging

- Enabled debugging mode to provide a more detailed log when needed.
- Modified the interval in the check_ip function to 5 seconds for better accuracy in IP retrieval attempts.
- Refactored the sleep_duration calculation to ensure it accurately reflects the intended duration between adjustments.
- Adjusted the loop in gradual adjustment to start from 1, ensuring the initial values are set correctly.
- Replaced multiple consecutive '&& sleep' statements with separate lines for improved readability.
- Simplified the error handling in check_ip and check_notification functions for clarity.
- Removed unnecessary '|| true' statements in error handling.
- Added echo statements to print the light status when it is off in check_external_light_status.
- Improved comments for better script understanding.

These changes enhance the script's execution, improve debugging capabilities, and make the code more readable and maintainable.
This commit is contained in:
Ratatoskr 2023-12-30 16:41:23 +01:00
parent 05af18857a
commit 091d1e8d2b
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

View File

@ -10,7 +10,7 @@
# 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
set -x # Enable debugging # Uncomment for a more detailed log
# Debug
DEBUG=false
SSH_EXECUTED=false # Variable to track SSH command status
@ -34,10 +34,10 @@ red="ff0000"
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 &&
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
@ -63,7 +63,7 @@ set_color() {
check_ip() {
mac_address="e4:cd:d1" # Replace this with the MAC address
attempts=10
interval=2
interval=5
for ((i = 1; i <= $attempts; i++)); do
A72_ip=$(arp-scan -r 3 -v --localnet | grep "$mac_address" | awk '{print $1}')
@ -85,7 +85,7 @@ check_ip() {
# All attempts unsuccessful
echo "The target could not be reached after $attempts attempts."
iobroker state set zigbee.0.04cd15fffee03198.state false || true
iobroker state set zigbee.0.04cd15fffee03198.state false
return 1 # Error: IP address not found
}
@ -100,6 +100,7 @@ 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
}
@ -131,7 +132,7 @@ check_notification() {
# Alarm turned off
date && echo "Alarm turned off."
echo $output
iobroker state set zigbee.0.04cd15fffee03198.state false || true
iobroker state set zigbee.0.04cd15fffee03198.state false
return # Exit the function without terminating the entire script
else
# Phone unreachable
@ -144,7 +145,7 @@ check_notification() {
# 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 || true
iobroker state set zigbee.0.04cd15fffee03198.state false
return
}
@ -179,7 +180,7 @@ echo "Reached final colortemp: $((start_colortemp - colortemp_step * steps)) fro
sleep_duration=$((duration_seconds / steps))
# Gradual adjustment of brightness and color temperature
for ((i = 0; 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)"))