-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwebrtc_test_server.sh
More file actions
executable file
·109 lines (102 loc) · 3.2 KB
/
Copy pathwebrtc_test_server.sh
File metadata and controls
executable file
·109 lines (102 loc) · 3.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
#!/usr/bin/env bash
#
# webrtc_test_server.sh — start/stop the Creality test server in the background
# for WebRTC camera testing.
#
# Uses a WebRTC-capable model (k2plus) and an absurdly long print duration so the
# simulated job never finishes on its own during a testing session.
#
# Usage:
# ./webrtc_test_server.sh on # start in background
# ./webrtc_test_server.sh off # stop
# ./webrtc_test_server.sh status # show whether it's running
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVER="$SCRIPT_DIR/tools/creality_printer_test_server.py"
PID_FILE="$SCRIPT_DIR/.webrtc_test_server.pid"
LOG_FILE="$SCRIPT_DIR/.webrtc_test_server.log"
# WebRTC-capable model + a ~1 year print so the job never ends mid-session.
MODEL="${MODEL:-k2plus}"
PRINT_SECONDS="${PRINT_SECONDS:-31536000}"
# Default to the project's .venv interpreter; override with PYTHON=... if needed.
PYTHON="${PYTHON:-$SCRIPT_DIR/.venv/bin/python}"
is_running() {
[[ -f "$PID_FILE" ]] || return 1
local pid
pid="$(cat "$PID_FILE")"
[[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null
}
start() {
if is_running; then
echo "Already running (PID $(cat "$PID_FILE"))."
return 0
fi
# Validate the interpreter up front so a missing venv doesn't fail cryptically.
if [[ ! -x "$PYTHON" ]] && ! command -v "$PYTHON" >/dev/null 2>&1; then
local resolved
resolved="$(command -v python3 || command -v python || true)"
if [[ -z "$resolved" ]]; then
echo "Error: Python interpreter not found at '$PYTHON' and no python3/python on PATH." >&2
echo " Set PYTHON=/path/to/python and retry." >&2
return 1
fi
echo "Note: '$PYTHON' not found; falling back to '$resolved'." >&2
PYTHON="$resolved"
fi
echo "Starting WebRTC test server (model=$MODEL, print-seconds=$PRINT_SECONDS)..."
nohup "$PYTHON" "$SERVER" \
--model "$MODEL" \
--simulate-print \
--print-seconds "$PRINT_SECONDS" \
>"$LOG_FILE" 2>&1 &
echo $! >"$PID_FILE"
sleep 1
if is_running; then
echo "Started (PID $(cat "$PID_FILE")). Logs: $LOG_FILE"
echo " WebRTC signaling: POST http://localhost:8000/call/webrtc_local"
echo " WS telemetry: ws://localhost:9999"
else
echo "Failed to start. Check $LOG_FILE:"
tail -n 20 "$LOG_FILE" || true
rm -f "$PID_FILE"
return 1
fi
}
stop() {
if ! is_running; then
echo "Not running."
rm -f "$PID_FILE"
return 0
fi
local pid
pid="$(cat "$PID_FILE")"
echo "Stopping (PID $pid)..."
kill "$pid" 2>/dev/null || true
for _ in $(seq 1 10); do
is_running || break
sleep 0.5
done
if is_running; then
echo "Still alive, sending SIGKILL..."
kill -9 "$pid" 2>/dev/null || true
fi
rm -f "$PID_FILE"
echo "Stopped."
}
status() {
if is_running; then
echo "Running (PID $(cat "$PID_FILE")). Logs: $LOG_FILE"
else
echo "Not running."
fi
}
case "${1:-}" in
on|start) start ;;
off|stop) stop ;;
status) status ;;
*)
echo "Usage: $0 {on|off|status}"
exit 1
;;
esac