Skip to content

Commit 8a6e3a9

Browse files
authored
Update wled-tools.sh
1 parent 0139c34 commit 8a6e3a9

File tree

1 file changed

+55
-40
lines changed

1 file changed

+55
-40
lines changed

tools/wled-tools.sh

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
#!/bin/bash
22

3-
# =====================
4-
# wled-toolbox.sh - A script for managing WLED devices
5-
# =====================
6-
73
# Color Definitions
84
GREEN="\e[32m"
95
RED="\e[31m"
106
BLUE="\e[34m"
117
YELLOW="\e[33m"
128
RESET="\e[0m"
139

14-
# Path to backup directory
15-
backup_dir="./"
16-
17-
# Firmware file (if provided)
18-
firmware_file=""
19-
2010
# Logging function
2111
log() {
2212
local category="$1"
@@ -85,26 +75,29 @@ discover_devices() {
8575
exit 1
8676
fi
8777

88-
mapfile -t hostnames < <(avahi-browse _wled._tcp --terminate -r -p | awk -F';' '/^=/ {print $7}')
89-
if [ "$quiet" = true ]; then
90-
for hostname in "${hostnames[@]}"; do
91-
echo "$hostname"
92-
done
93-
else
94-
printf "%s\n" "${hostnames[@]}" | sort -u
95-
fi
78+
mapfile -t raw_devices < <(avahi-browse _wled._tcp --terminate -r -p | awk -F';' '/^=/ {print $7, $8, $9}')
79+
80+
local devices_array=()
81+
for device in "${raw_devices[@]}"; do
82+
read -r hostname address port <<< "$device"
83+
devices_array+=("$hostname" "$address" "$port")
84+
done
85+
86+
echo "${devices_array[@]}"
9687
}
9788

9889
# Backup one device
9990
backup_one() {
10091
local hostname="$1"
92+
local address="$2"
93+
local port="$3"
10194

102-
log "INFO" "$YELLOW" "Backing up device config/presets: $hostname"
95+
log "INFO" "$YELLOW" "Backing up device config/presets: $hostname ($address:$port)"
10396

10497
mkdir -p "$backup_dir"
10598

106-
local cfg_url="http://$hostname/cfg.json"
107-
local presets_url="http://$hostname/presets.json"
99+
local cfg_url="http://$address:$port/cfg.json"
100+
local presets_url="http://$address:$port/presets.json"
108101
local cfg_dest="${backup_dir}/${hostname}.cfg.json"
109102
local presets_dest="${backup_dir}/${hostname}.presets.json"
110103

@@ -122,16 +115,13 @@ backup_one() {
122115
# Update one device
123116
update_one() {
124117
local hostname="$1"
125-
local firmware="$2"
118+
local address="$2"
119+
local port="$3"
120+
local firmware="$4"
126121

127-
log "INFO" "$YELLOW" "Starting firmware update for device: $hostname"
122+
log "INFO" "$YELLOW" "Starting firmware update for device: $hostname ($address:$port)"
128123

129-
if [ -z "$firmware" ]; then
130-
log "ERROR" "$RED" "Firmware file not specified."
131-
exit 1
132-
fi
133-
134-
local url="http://$hostname/update"
124+
local url="http://$address:$port/update"
135125
local curl_command="curl -s -X POST -F "file=@$firmware" "$url""
136126

137127
curl_handler "$curl_command" "$hostname"
@@ -142,6 +132,8 @@ command=""
142132
target=""
143133
discover=false
144134
quiet=false
135+
backup_dir="./"
136+
firmware_file=""
145137

146138
if [ $# -eq 0 ]; then
147139
show_help
@@ -154,10 +146,6 @@ while [[ $# -gt 0 ]]; do
154146
show_help
155147
exit 0
156148
;;
157-
-v|--verbose)
158-
verbose=true
159-
shift
160-
;;
161149
-t|--target)
162150
target="$2"
163151
shift 2
@@ -193,26 +181,53 @@ done
193181
# Execute the appropriate command
194182
case "$command" in
195183
discover)
196-
discover_devices
184+
read -ra devices <<< "$(discover_devices)"
185+
for ((i=0; i<${#devices[@]}; i+=3)); do
186+
hostname="${devices[$i]}"
187+
address="${devices[$i+1]}"
188+
port="${devices[$i+2]}"
189+
190+
if [ "$quiet" = true ]; then
191+
echo "$hostname"
192+
else
193+
log "INFO" "$BLUE" "Discovered device: Hostname=$hostname, Address=$address, Port=$port"
194+
fi
195+
done
197196
;;
198197
backup)
199198
if [ -n "$target" ]; then
200-
backup_one "$target"
199+
# Assume target is both the hostname and address, with port 80
200+
backup_one "$target" "$target" "80"
201201
elif [ "$discover" = true ]; then
202-
for hostname in $(discover_devices); do
203-
backup_one "$hostname"
202+
read -ra devices <<< "$(discover_devices)"
203+
for ((i=0; i<${#devices[@]}; i+=3)); do
204+
hostname="${devices[$i]}"
205+
address="${devices[$i+1]}"
206+
port="${devices[$i+2]}"
207+
backup_one "$hostname" "$address" "$port"
204208
done
205209
else
206210
log "ERROR" "$RED" "No target specified. Use --target or --discover."
207211
exit 1
208212
fi
209213
;;
210214
update)
215+
# Validate firmware before proceeding
216+
if [ -z "$firmware_file" ] || [ ! -f "$firmware_file" ]; then
217+
log "ERROR" "$RED" "Please provide a file in --firmware that exists"
218+
exit 1
219+
fi
220+
211221
if [ -n "$target" ]; then
212-
update_one "$target" "$firmware_file"
222+
# Assume target is both the hostname and address, with port 80
223+
update_one "$target" "$target" "80" "$firmware_file"
213224
elif [ "$discover" = true ]; then
214-
for hostname in $(discover_devices); do
215-
update_one "$hostname" "$firmware_file"
225+
read -ra devices <<< "$(discover_devices)"
226+
for ((i=0; i<${#devices[@]}; i+=3)); do
227+
hostname="${devices[$i]}"
228+
address="${devices[$i+1]}"
229+
port="${devices[$i+2]}"
230+
update_one "$hostname" "$address" "$port" "$firmware_file"
216231
done
217232
else
218233
log "ERROR" "$RED" "No target specified. Use --target or --discover."

0 commit comments

Comments
 (0)