-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·393 lines (354 loc) · 14.2 KB
/
run
File metadata and controls
executable file
·393 lines (354 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env bash
# ESP32 Device Management Script (Main Entry Point)
# Simplifies interactions with ESP32 device by delegating tasks
# Get the script directory (project root)
SCRIPT_DIR_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Fixed &&
# Source common functions and variables from the scripts directory
# shellcheck source=./scripts/common.sh
source "$SCRIPT_DIR_ROOT/scripts/common.sh" || { echo "Error: Unable to source scripts/common.sh" >&2; exit 1; } # Fixed >&2
# --- Argument Parsing for Flags (--ap, --mpremote) ---
USE_AP_IP=false
USE_MPREMOTE=false
declare -a ALL_ARGS=("$@") # Keep original args for passing later
for arg in "$@"; do
if [ "$arg" == "--ap" ]; then
USE_AP_IP=true
elif [ "$arg" == "--mpremote" ]; then # Renamed from --ampy
USE_MPREMOTE=true # Renamed from USE_AMPY
fi
done
# --- Determine Target IP ---
# This logic remains here as it's needed before dispatching commands
if [ "$USE_AP_IP" = true ]; then
ESP_IP="$AP_IP" # AP_IP is sourced from common.sh
echo "Using AP IP address: $ESP_IP"
elif saved_ip=$(read_ip_from_json); then # read_ip_from_json is sourced
ESP_IP="$saved_ip"
# Optionally echo the used IP: echo "Using saved IP address: $ESP_IP"
else
echo "Warning: IP file not found or invalid at $IP_JSON_FILE" # IP_JSON_FILE is sourced
echo "Using AP IP address instead: $AP_IP"
ESP_IP="$AP_IP"
fi
# Export ESP_IP so make_request (used by some commands below) can see it
export ESP_IP
# --- Display Usage Information ---
show_usage() {
# Usage info remains largely the same, but reflects the structure
echo "Usage: ./run [--ap] [--mpremote] <command> [arguments]"
echo ""
echo "Options:"
echo " --ap - Use AP IP address ($AP_IP) instead of configured IP"
echo " --mpremote - Use mpremote (auto-detects port) for serial communication instead of HTTP API"
echo ""
echo "Commands:"
echo " reset - Reset the ESP32 device"
echo " repl - Start mpremote REPL (retries on error)"
echo " log - View device logs"
echo " clear-log - Clear the log file on the device"
echo " log-add-test - Add 1000 test log entries"
echo " la/ls - List all files on the device (internal flash only)"
echo " fs-list - Get JSON file listing for visual tools"
echo " free - Show free memory on the device"
echo " ping - Check if the device is responding"
echo " status - Check device status and update IP file if needed"
echo " rm <path> - Remove a file from the device"
echo " rm-visual - Interactive visual file removal (uses scripts/rm-visual)"
echo " download <path> - Download a file from the device"
echo " view <path> [out_path] - View file contents (optionally save to out_path)" # Updated usage
echo " upload [...] - Upload files (delegated to scripts/upload.sh)"
echo " Args: [--py | --no-compile] <file(s)> [target]"
echo " sync [...] - Sync modified files (delegated to scripts/sync.sh)"
echo " Args: [--py | --no-compile] [--dry-run] [--force]"
echo ""
echo "Examples:"
echo " ./run reset"
echo " ./run --ap log"
echo " ./run status"
echo " ./run rm main.py"
echo " ./run rm-visual"
echo " ./run view main.py"
echo " ./run view main.py main_copy.py" # Added example
echo " ./run upload settings.html"
echo " ./run --mpremote upload main.py"
echo " ./run sync"
echo " ./run sync --force --py"
echo " ./run --mpremote sync"
echo " ./run repl"
}
# --- Command Dispatch ---
# Process arguments to filter out --ap and --mpremote for command/args extraction
declare -a CMD_ARGS=()
for arg in "${ALL_ARGS[@]}"; do
if [ "$arg" != "--ap" ] && [ "$arg" != "--mpremote" ]; then # Renamed from --ampy
CMD_ARGS+=("$arg")
fi
done
# Check if a command was provided after filtering flags
if [ ${#CMD_ARGS[@]} -eq 0 ]; then
echo "Error: No command specified." >&2
show_usage
exit 1
fi
# Extract command and its arguments
COMMAND="${CMD_ARGS[0]}"
if [ ${#CMD_ARGS[@]} -gt 0 ]; then
ARGS=("${CMD_ARGS[@]:1}") # Arguments specific to the command
else
ARGS=()
fi
# Main command logic using case statement
case "$COMMAND" in
reset)
check_sync_needed # Sourced from common.sh
if [ "$USE_MPREMOTE" = true ]; then # Renamed from USE_AMPY
echo "Resetting ESP32 using mpremote..."
mpremote_cmd="mpremote reset"
echo "Running mpremote command: $mpremote_cmd"
eval $mpremote_cmd
else
echo "Resetting ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/reset" "POST" # make_request sourced
echo "Reset command sent."
fi
;;
repl)
check_sync_needed
echo "Starting mpremote REPL..."
max_tries=10000
tries=1
success=false
while [ $tries -le $max_tries ] && [ "$success" = false ]; do
echo "Attempt $tries of $max_tries"
mpremote repl
exit_code=$?
if [ $exit_code -eq 0 ]; then
success=true
echo "REPL session ended successfully."
else
echo "REPL connection failed (exit code: $exit_code)"
if [ $tries -lt $max_tries ]; then
echo "Retrying in 1 second..."
sleep 1
else
echo "Maximum retry attempts reached."
fi
tries=$((tries + 1))
fi
done
if [ "$success" = false ]; then
echo "Failed to establish REPL connection after $max_tries attempts." >&2
exit 1
fi
;;
log)
check_sync_needed
echo "Fetching logs from ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/api/log/chunk"
;;
clear-log)
check_sync_needed
echo "Clearing logs on ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/log/clear" "POST"
;;
clear-data)
check_sync_needed
echo "Clearing data logs on ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/data/clear" "POST"
;;
log-add-test)
check_sync_needed
echo "Adding 1000 test log entries on ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/log/add-test-entries" "POST"
;;
la|ls)
check_sync_needed
FOLDER_ARG=""
if [ ${#ARGS[@]} -gt 0 ]; then
FOLDER_ARG="${ARGS[0]}"
fi
if [ "$USE_MPREMOTE" = true ]; then
echo "Listing files on ESP32 using mpremote..."
# If FOLDER_ARG is provided, use it, otherwise list root.
# Ensure path for mpremote starts with ':', remove leading '/' if present.
mpremote_path=":"
if [ -n "$FOLDER_ARG" ]; then
# Remove leading slash if present, as mpremote ls path is relative to root after ':'
mpremote_path=":${FOLDER_ARG#/}"
fi
mpremote_cmd="mpremote ls \"$mpremote_path\""
echo "Running mpremote command: $mpremote_cmd"
eval $mpremote_cmd
else
if [ -n "$FOLDER_ARG" ]; then
echo "Listing files in '$FOLDER_ARG' on ESP32 at $ESP_IP..."
# URL encode the folder argument? For now, assume it's simple enough.
# The server-side Python code will handle leading slashes.
make_request "https://$ESP_IP/la?folder=$FOLDER_ARG"
else
echo "Listing all files on ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/la"
fi
fi
;;
fs-list)
check_sync_needed
make_request "https://$ESP_IP/fs-list"
;;
free)
check_sync_needed
echo "Checking free memory on ESP32 at $ESP_IP..."
response=$(make_request "https://$ESP_IP/free")
status_code=$?
if [ $status_code -eq 0 ]; then
check_jq
echo "$response" | jq .
fi
;;
ping)
check_sync_needed
echo "Pinging ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/ping"
;;
status)
check_sync_needed
echo "Checking status of ESP32 at $ESP_IP..."
response=$(make_request "https://$ESP_IP/status")
status_code=$?
echo "$response"
# Update IP file logic (uses sourced functions/variables)
# Only attempt to update the IP file if we are NOT using the AP IP
# Update IP file logic only if --ap flag was used
if [ "$USE_AP_IP" = true ]; then
if [ $status_code -eq 0 ] && printf '%s' "$response" | jq -e '.ip' >/dev/null 2>&1; then # Check for .ip
device_ip=$(echo "$response" | jq -r '.ip') # Extract .ip
need_update=false
if [ -f "$IP_JSON_FILE" ]; then
check_jq # Sourced
current_ip=$(jq -r '.ip' "$IP_JSON_FILE")
if [ "$device_ip" != "$current_ip" ]; then
echo "IP has changed from $current_ip to $device_ip. Updating IP file..."
need_update=true
else
echo "IP unchanged: $device_ip"
fi
else
echo "IP file not found at $IP_JSON_FILE. Creating with IP: $device_ip"
need_update=true
fi
if [ "$need_update" = true ]; then
write_ip_to_json "$device_ip" # Sourced
echo "IP file updated."
fi
elif [ $status_code -eq 0 ]; then
echo "WiFi not connected or status information incomplete."
# else make_request already printed an error
fi
fi
;;
rm)
check_sync_needed
if [ ${#ARGS[@]} -lt 1 ]; then
echo "Error: Missing file path" >&2 # Fixed >&2
echo "Usage: ./run rm <path>" >&2 # Fixed >&2
exit 1
fi
FILE_PATH="${ARGS[0]}"
if [ "$USE_MPREMOTE" = true ]; then # Renamed from USE_AMPY
# Ensure the path starts with a colon for mpremote
remote_path=":$FILE_PATH"
# Remove potential double colon if FILE_PATH already starts with one
remote_path="${remote_path//::/:}"
echo "Removing file $remote_path from ESP32 using mpremote..."
mpremote_cmd="mpremote rm \"$remote_path\""
echo "Running mpremote command: $mpremote_cmd"
eval $mpremote_cmd
else
echo "Removing file $FILE_PATH from ESP32 at $ESP_IP..."
make_request "https://$ESP_IP/rm/$FILE_PATH" "DELETE"
fi
;;
rm-visual)
check_sync_needed
# Run the visual rm script from the scripts directory
echo "Launching visual file removal..."
# Pass the --ap flag if it was used
declare -a rm_visual_args=()
if [ "$USE_AP_IP" = true ]; then
rm_visual_args+=("--ap")
fi
"$SCRIPT_DIR_ROOT/scripts/rm-visual" "${rm_visual_args[@]}"
exit $? # Exit with the status of rm-visual
;;
download)
check_sync_needed
if [ ${#ARGS[@]} -lt 1 ]; then
echo "Error: Missing file path" >&2 # Fixed >&2
echo "Usage: ./run download <path>" >&2 # Fixed >&2
exit 1
fi
FILE_PATH="${ARGS[0]}"
DOWNLOAD_TARGET="$(basename "$FILE_PATH")"
echo "Downloading $FILE_PATH from ESP32 at $ESP_IP to $DOWNLOAD_TARGET..."
make_request "https://$ESP_IP/download/$FILE_PATH" "GET" "$DOWNLOAD_TARGET"
if [ $? -eq 0 ]; then
echo "Downloaded to $DOWNLOAD_TARGET"
else
echo "Download failed." >&2 # Fixed >&2
# Optionally remove partially downloaded file
# rm -f "$DOWNLOAD_TARGET"
exit 1
fi
;;
view)
check_sync_needed
if [ ${#ARGS[@]} -lt 1 ] || [ ${#ARGS[@]} -gt 2 ]; then
echo "Error: Incorrect number of arguments for view" >&2
echo "Usage: ./run view <remote_path> [local_output_path]" >&2
exit 1
fi
FILE_PATH="${ARGS[0]}"
OUTPUT_PATH="${ARGS[1]:-}" # Empty if not provided
if [ -n "$OUTPUT_PATH" ]; then
# Output path provided
echo "Viewing file $FILE_PATH from ESP32 at $ESP_IP, saving to $OUTPUT_PATH..." >&2
make_request "https://$ESP_IP/view/$FILE_PATH" "GET" "$OUTPUT_PATH"
else
# No output path, print to stdout
echo "Viewing file $FILE_PATH from ESP32 at $ESP_IP..." >&2
make_request "https://$ESP_IP/view/$FILE_PATH" "GET" # Pass GET explicitly, no output file
fi
;;
upload)
check_sync_needed
echo "Delegating upload to scripts/upload.sh..."
# Prepare args for upload.sh
upload_args=("--ip" "$ESP_IP") # Pass the determined IP
if [ "$USE_MPREMOTE" = true ]; then # Renamed from USE_AMPY
upload_args+=("--mpremote") # Renamed from --ampy
fi
# Pass the specific arguments meant for upload (e.g., --py, files, target)
upload_args+=("${ARGS[@]}")
"$SCRIPT_DIR_ROOT/scripts/upload.sh" "${upload_args[@]}"
exit $? # Exit with the status of upload.sh
;;
sync)
# No need for check_sync_needed here, sync.sh does it
echo "Delegating sync to scripts/sync.sh..."
# Prepare args for sync.sh
sync_args=("--ip" "$ESP_IP") # Pass the determined IP
if [ "$USE_MPREMOTE" = true ]; then # Renamed from USE_AMPY
sync_args+=("--mpremote") # Renamed from --ampy
fi
# Pass the specific arguments meant for sync (e.g., --py, --force, --dry-run)
sync_args+=("${ARGS[@]}")
"$SCRIPT_DIR_ROOT/scripts/sync.sh" "${sync_args[@]}"
exit $? # Exit with the status of sync.sh
;;
*)
echo "Error: Unknown command '$COMMAND'" >&2 # Fixed >&2
show_usage
exit 1
;;
esac
exit 0 # Default exit code for successful command execution